Simple boilerplate code to get started with building and deploying a serverless CRUD API

Go Mongo Serverless CRUD Boilerplate

stackshare

Simple boilerplate code to get started with building and deploying a serverless CRUD API
Uses Go, MongoDB and Serverless functions integrated and deployed with Netlify
Made with ❤️ by Brian Min


Why?

The reason I started this project is to provide an easy-to-use boilerplate code for building and deploying a simple CRUD API with serverless. I built this in the process of creating a simple API to manage content on my portfolio website.
The project is tightly coupled to the following tech stack: Go, MongoDB, AWS Lambda functions so it is not recommended to change one of the components (e.g. use a different database such as MySQL) as that would require a lot of refactoring.

Project Structure

.
├── db                        # Database abstraction - CRUD implementations
│   ├── models                # Contains the DB schemas
│   │   ├── Task.go           # Example model for Task
│   ├── services              # Contains CRUD implementations for each model
│   │   ├── TaskService.go
│   ├── db.go                 # Initialises the database connection and services
├── functions                 # Contains serverless functions
│   ├── src
│   │   ├── helloworld        # Example function that returns "Hello World" endpoint
│   │   ├── tasks             # Example function that handles CRUD endpoints for tasks
│   │   ├── utils             # Contains utility functions (e.g. constructing an API response)
├── Makefile                  # Build instructions
├── netlify.toml              # Netlify configuration for building and deploying
└── ...

Example API Endpoints

The route prefix is configured as /api, but you can change this in the netlify.toml config file under the [[redirects]] section.

Route Method Description
/api/helloworld GET Returns "Hello World"
/api/tasks GET Example tasks endpoint - returns all tasks
/api/tasks?id=1 GET Example tasks endpoint - returns a task by id
/api/tasks POST Example tasks endpoint - creates a task (parameters parsed from body)
/api/tasks?id=1 PUT Example tasks endpoint - updates a task by id(parameters parsed from body)
/api/tasks?id=1 DELETE Example tasks endpoint - deletes a task by id

Prerequisites

  1. Fork or clone this repository

  2. Connect your cloned repository with Netlify

  3. Get a free DB cluster on MongoDB Atlas

Configuration

Update module imports

Inside the functions and db code, you will need to update the import paths to your new repository.
I.e. Globally find and replace: "github.com/bymi15/go-mongo-serverless-crud-boilerplate/" with your new repository.

Netlify config

Modify and replace the GO_IMPORT_PATH variable with your new repository in netlify.toml

Environment variables

Rename the .env.example file to .env and fill in the appropriate details.
(You can create a database on MongoDB Atlas)

In Netlify, go to Site Settings -> Build & Deploy -> Environment
and add the variables (same as .env): CONNECTION_URI and DB_NAME
(You can also add this in the [build.environment] section in netlify.toml)

Running functions locally

Unfortunately Netlify Dev does not support Go functions.
In order to test the functions locally, we can run the following command:

go run functions/src/<FUNCTION_NAME>/<FUNCTION_NAME>.go -port <PORT>

For example:

go run functions/src/tasks/tasks.go -port 8000

and the function endpoint will be: http://localhost:8000/api/tasks

Continuous Deployment

Simply commit and push the code to Github.

Netlify will automatically handle the deployment process.
(Make sure you have set up and connected your Github repository and branch with Netlify in order for this to work)

License

MIT

Similar Resources

Example code for my Cadence Intro Workshop

Temporal Intro Workshop This repository contains example code for my Temporal Intro Workshop. Prerequisites Git, Make, etc. Make sure you have the lat

Dec 18, 2022

Sample code for my Go Deadlocks talk

Go Deadlocks Talk This is sample code for my Go Deadlocks talk. Simple simple - a super simple deadlock simple2 - the same but with an extra Go deadlo

Jul 30, 2022

Test-Driven Development code kata in Golang

Bowling Kata Golang Test-Driven Development code kata in Golang Bowling Rules The game consists of 10 frames. In each frame the player has two rolls t

Jan 5, 2022

A series of small code snipppets & exercises to learn Go.

Learning-Go A series of small code snipppets & exercises to learn Go. WARNING: During this excercise you will be learning along with me, I am not your

Dec 18, 2021

Source code of the 100 Go Mistakes book

100 Go Mistakes and How to Avoid Them Source code of 100 Go Mistakes and How to Avoid Them. Table of Contents Chapter 1 - Introduction Chapter 2 - Cod

Dec 30, 2022

VHDL Simulator, code VHDL in your browser! 🤖

VHDL Simulator, code VHDL in your browser! 🤖

VHDL Runner 🤖 VHDL Simulator, code VHDL in your browser! 🤖 Usage Go to https://vhdl.mateuszwozniak.com/ Next, type some VHDL code, with appropriate

Feb 6, 2022

Code solution for question against internship application at Eluvio

Eluvio Problem Statement Imagine you have a program that needs to look up information about items using their item ID, often in large batches. Unfortu

Feb 5, 2022

Mastering-go-exercises - Some code examples from Mihalis Tsoukalos book titled Mastering GO

Mastering go exercises This is a training repository. Here are some code example

Feb 16, 2022

📖 Build a RESTful API on Go: Fiber, PostgreSQL, JWT and Swagger docs in isolated Docker containers.

📖 Build a RESTful API on Go: Fiber, PostgreSQL, JWT and Swagger docs in isolated Docker containers.

📖 Tutorial: Build a RESTful API on Go Fiber, PostgreSQL, JWT and Swagger docs in isolated Docker containers. 👉 The full article is published on Marc

Dec 27, 2022
Simples exemplo de CRUD para armazenar em memoria os dados vindo do JSON.

API Growth Este repositório foi criado para colocarmos projetos em diversas linguagens com intúito totalmente didático para colaborar com a comunidade

Dec 14, 2022
Crud with Go Language!

CRUD With GO! CRUD with Go Language! How to use with Docker-Compose git clone this project open the cloned folder in the terminal and cd backend/ run

Oct 12, 2021
`go-redash-query` is a simple library to get structed data from `redash query` sources

go-redash-query go-redash-query is a simple library to get structed data from redash query sources Example Source table id name email 1 Dannyhann rhrn

May 22, 2022
Building a shoe store with golang to learn more about this language :)

shoestore-go Building a shoe store with golang to learn more about this language :) TODO Create a basic webpage with the pages: home : to show homepag

Jan 24, 2022
Dec 7, 2021
Learning GO language by building projects
Learning GO language by building projects

GoLangProjects Projects list Helloworld Variables Userinput Conversion Math, crypto & random Time & Date Pointers Arrays Slices Maps Structures If-Els

Dec 5, 2022
Assert your Go code is inlined and bounds-check eliminated

gcassert gcassert is a program for making assertions about compiler decisions in Golang programs, via inline comment directives like //gcassert:inline

Oct 27, 2022
An open source, online coding platform that offers code practice and tutoring in 50 different programming languages
An open source, online coding platform that offers code practice and tutoring in 50 different programming languages

Exercism Golang En este repositorio voy a subir los ejercicios de la plataforma

Jan 7, 2022
Markdown version of Reverse Engineering the source code of the BioNTech/Pfizer SARS-CoV-2 Vaccine

The big BNT162b2 archive All vaccine data here is sourced from this World Health Organization document. This describes the RNA contents of the BNT162b

Dec 2, 2022
Go-Notebook is inspired by Jupyter Project (link) in order to document Golang code.
Go-Notebook is inspired by Jupyter Project (link) in order to document Golang code.

Go-Notebook Go-Notebook is an app that was developed using go-echo-live-view framework, developed also by us. GitHub repository is here. For this proj

Jan 9, 2023