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 beproduction
ordevelopment
. -
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.example
to 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 beproduction
ordevelopment
. -
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.example
to see all required environment variables. -
-
Go:
1.22.1
. -
Docker:
26.1.2
. -
Brew tools:
All required
brew
tools 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 -d
Access 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.git
Go to the project directory:
cd go-todo
Install dependencies:
go mod download
Update the environment variables files:
Please check the Environment Variables section to see all required environment variables.
Start the Postgres database:
docker-compose up -d
Start the server:
go run ./main.go gateway start
go run ./main.go task start
Or with wgo for live reload:
wgo run ./main.go gateway start
wgo run ./main.go task start
Note
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-build
Makefile
provides some useful targets to help you work with this project:
-
init
: Download tool dependencies and setupGOPRIVATE
environment 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
openapiv2
can only generate the OpenAPI v2 spec. Therefore, I can't set hostname using variable which only available in OpenAPI v3. -
The
swagger
service defined in filedocker-compose.yaml
is running with port8082
and 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-gateway
so 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
tasks
totask
?- Bad naming convention.
-
How can test the gRPC server with Postman?
- Since the gRPC server enabled
reflection
service in PR#9, you can use optionUsing server reflection
from tabService definition
in 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.