https://github.com/jakecoffman/go-mongo-rest
Example of a REST server with Go and Mongo
https://github.com/jakecoffman/go-mongo-rest
gin-gonic go mongodb rest-api
Last synced: about 2 months ago
JSON representation
Example of a REST server with Go and Mongo
- Host: GitHub
- URL: https://github.com/jakecoffman/go-mongo-rest
- Owner: jakecoffman
- Created: 2021-11-19T17:25:11.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-12-03T14:43:43.000Z (over 4 years ago)
- Last Synced: 2025-02-07T12:28:17.242Z (over 1 year ago)
- Topics: gin-gonic, go, mongodb, rest-api
- Language: Go
- Homepage:
- Size: 25.4 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# go-mongo-rest
Example of how to make REST endpoints with Go and Mongodb.
## How to run
Copy/paste this into your terminal:
```sh
git clone git@github.com:jakecoffman/go-mongo-rest.git
cd go-mongo-rest
go run cmd/server/server.go
```
## Dependencies
- [gin](https://github.com/gin-gonic/gin)
- This is a good router that has been around for a while. Feel free to pick your own. The builtin http.ServeMux doesn't support path variables and method routing so doing a deep path with many variables (/authors/{authorId}/books/{bookId}) is unruly.
- [crud](https://github.com/jakecoffman/crud)
- Provides an easy way to get OpenAPI docs and validation middleware.
- [mongo-driver](https://github.com/mongodb/mongo-go-driver)
- Official Go Mongodb driver.
# Project layout
- cmd
- Typical pattern in Go to store the main packages
- lib
- It's common to separate the rest of the code into another package. I use lib, you can use pkg, it doesn't matter.
- lib/db
- The mongo.Client and collections are exported from this package since they are thread safe. Makes things easier to manage than passing it down through Request Contexts.
- lib/endpoonts
- All of our handlers and route definitions under this directory.
- lib/models
- Models live in the same package since they often depend on each other, but we've separated concerns from the handlers.
## TODO
- With generics around the corner, we can remove all the boilerplate in author_handlers.go