Server
Database
DevOps
- Basic CRUD operations.
- Swagger UI for API documentation.
- Simple CLI for running the server.
- gRPC Gateway server.
Note
All the environment variables file are required to run this project.
To run this project, you will need to add the following environment variables file:
-
internal/gateway/configs/.env: Gateway service environment variables.-
HOST: The host of the server. Default is0.0.0.0. -
PORT: The port of the server. Default is8081. -
APP_ENV: The environment of the application. Default isdevelopment. It can beproductionordevelopment. -
LOG_LEVEL: The log level of the application. Default is0. See available level in zerolog. -
LOG_SAMPLE_RATE: The sample rate of the log. Default is5. -
TASK_SERVER_ADDRESS: The address of the task service. Example:localhost:8080.
E.g:
# internal/gateway/configs/.env HOST="0.0.0.0" PORT=8081 APP_ENV="development" LOG_LEVEL=0 LOG_SAMPLE_RATE=5 TASK_SERVER_ADDRESS="localhost:9000"You can also check out the file
internal/gateway/configs/.env.exampleto see all required environment variables. -
-
internal/task/configs/.env: Task service environment variables.-
PORT: The port of the server. Default is8080. -
APP_ENV: The environment of the application. Default isdevelopment. It can beproductionordevelopment. -
LOG_LEVEL: The log level of the application. Default is0. See available level in zerolog. -
LOG_SAMPLE_RATE: The sample rate of the log. Default is5. -
DB_URL: The URL of the database. Example:postgres://postgres:postgres@localhost:5432/task?sslmode=disable.
E.g:
# internal/task/configs/.env PORT=9000 APP_ENV="development" LOG_LEVEL=0 LOG_SAMPLE_RATE=5 DB_URL="postgresql://postgres:postgres@localhost:5432/task?sslmode=disable"You can also check out the file
internal/task/configs/.env.exampleto see all required environment variables. -
-
Go:
1.22.1. -
Docker:
26.1.2. -
Brew tools:
All required
brewtools is placed ininternal/tools/Brewfile:make download-deps
-
Go tools:
All required Go tools is placed in
internal/tools/tools.go:make download-deps
Note
These dependencies are not included during build.
Update the environment variables files:
Please check the Environment Variables section to see all required environment variables.
Run the server:
docker-compose up -dAccess the Swagger UI at http://localhost/docs.
Access the API at http://localhost/api.
Clone the project:
git clone https://github.com/DuckyMomo20012/go-todo.gitGo to the project directory:
cd go-todoInstall dependencies:
go mod downloadUpdate the environment variables files:
Please check the Environment Variables section to see all required environment variables.
Start the Postgres database:
docker-compose up -dStart the server:
go run ./main.go gateway start
go run ./main.go task startOr with wgo for live reload:
wgo run ./main.go gateway start
wgo run ./main.go task startNote
The gateway API is mount to localhost:<PORT>/api not localhost:<PORT>.
-
REST API:
You can test the REST API with Postman by importing files from
/api/openapi/to your Postman.
Note
You may have to add the Authorization header to your requests. You can get a
token by logging in with the login API.
-
gRPC API:
You can test the gRPC API with Postman by using Postman reflection feature.
Open your browser and go to http://localhost/docs.
Note
Environment variables files are required to build the Docker image. Check the Environment Variables section for more information.
docker build -t go-todo -f ./docker/tasks/Dockerfile .Or build ghcr image:
make docker-buildMakefile provides some useful targets to help you work with this project:
-
init: Download tool dependencies and setupGOPRIVATEenvironment variable.make init
Note
Setup GOPRIVATE env for vscode not automatically going to
pkg.go.dev for private modules.
-
download-deps: Download all tool dependencies.make download-deps
-
gen-proto: Generate gRPC and gRPC gateway from proto files.make gen-proto
-
lint: Run lint withgolangci-lint.make lint
-
docker-build: Build Docker image for ghcr.io registry.make docker-build
-
docker-push: Push Docker image to ghcr.io registry.make docker-push
- gRPC support.
- gRPC Gateway support.
- CLI support.
Contributions are always welcome!
Please read the Code of Conduct.
-
I can't query requests using the Swagger UI.
-
Currently with the Buf plugin
openapiv2can only generate the OpenAPI v2 spec. Therefore, I can't set hostname using variable which only available in OpenAPI v3. -
The
swaggerservice defined in filedocker-compose.yamlis running with port8082and sending requests usinglocalhost:8082. However, the gRPC gateway service is running with port8081.
-
-
Why do you migrate to
protoc-gen-openapiv2?-
The original port for REST API is removed in PR#8.
-
Because I decided to switch to
grpc-gatewayso I can automatically generate REST API from proto files. -
For the OpenAPI spec, I used to use
protoc-gen-openapiv2(Using buf plugin:openapiv2) to generate the OpenAPI v2 spec. However, it can only generate the OpenAPI v2 so I may have to switch to another plugin that can generate OpenAPI v3 spec later.
-
-
Why do you rename all vars and files from
taskstotask?- Bad naming convention.
-
How can test the gRPC server with Postman?
- Since the gRPC server enabled
reflectionservice in PR#9, you can use optionUsing server reflectionfrom tabService definitionin your gRPC requests in Postman.
- Since the gRPC server enabled
Distributed under MIT license. See LICENSE for more information.
Duong Vinh - @duckymomo20012 - tienvinh.duong4@gmail.com
Project Link: https://github.com/DuckyMomo20012/go-todo.
Here are useful resources and libraries that we have used in our projects:
- Clean Architecture: A detailed explanation of Clean Architecture by Three Dots Labs.
- grpc-gateway: gRPC to JSON proxy generator following the gRPC HTTP spec.
- Buf CLI: A new way to work with Protocol Buffers.
