https://github.com/perfectgentlemande/go-mongodb-crud-example
Example of REST API. Includes such things as MongoDB, Zap logger, chi router etc...
https://github.com/perfectgentlemande/go-mongodb-crud-example
docker docker-compose go golang mongodb oapi-codegen openapi rest-api
Last synced: 5 months ago
JSON representation
Example of REST API. Includes such things as MongoDB, Zap logger, chi router etc...
- Host: GitHub
- URL: https://github.com/perfectgentlemande/go-mongodb-crud-example
- Owner: perfectgentlemande
- Created: 2022-03-27T00:36:29.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-12-13T10:20:21.000Z (over 3 years ago)
- Last Synced: 2025-04-05T00:01:37.737Z (about 1 year ago)
- Topics: docker, docker-compose, go, golang, mongodb, oapi-codegen, openapi, rest-api
- Language: Go
- Homepage:
- Size: 85 KB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# go-mongodb-crud-example
Example of REST API.
Includes:
- oapi-codegen;
- go-chi;
- Zap logger;
- MongoDB;
- Docker;
- Docker Compose;
- gomock.
## Description
Sample project for educational purposes.
There are 2 ideas:
- checking and glueing together technologies mentioned above;
- sharing my own experience for the ones who want to glue the same technologies.
## Package Diagram

## Generate
API boilerplate code is generated using `oapi-codegen` tool from the `openapi.yaml` file.
It's great tool that makes your actual API reflect the documentation.
Get it there:
`https://github.com/deepmap/oapi-codegen`
And make sure that your `GOPATH/bin` path presents in `PATH` variable.
Use this command to generate the `api.go` file:
- `oapi-codegen --package=api --generate=types,chi-server openapi/openapi.yaml > internal/api/api.go`
## Running
### Running
Use `go run .` from the folder that contains `main.go`.
### Running via Docker (no compose)
Get the image here:
`https://hub.docker.com/_/mongo`
Create network and volume:
- `docker network create db_network`
- `docker volume create mongo-vol`
- `docker volume create mongo-config-vol`
Run db container:
- `docker run -it --rm -p 27017:27017 --name mongodb-0 --network db_network --mount source=mongo-vol,target=/data/db --mount source=mongo-config-vol,target=/data/configdb mongo:5.0.6`
After that you can run your using `go run` if you need.
Build the app image and run:
- `docker build -t go-mongodb-app:v0.1.0 .`
- `docker run -it -p 8080:80 --name go-mongodb-app-0 --network db_network go-mongodb-app:v0.1.0`
### Running via Docker Compose
As simple as:
- `docker-compose build`
- `docker-compose up`