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
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.
.
├── 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
└── ...
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 |
-
Fork or clone this repository
-
Connect your cloned repository with Netlify
-
Get a free DB cluster on MongoDB Atlas
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.
Modify and replace the GO_IMPORT_PATH
variable with your new repository in netlify.toml
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
)
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
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)