Example go project
Introduction
This is a server application built using go-template to showcase how to use it.
You might find this project over-engineered as it is just a simple API which lets you signup, login and get a greeting from it. Yet it uses three-layer architecture and can be called using gRPC, RESTful HTTP/1.1 and HTTP/2.
Why is it like that? It is, as the name suggests, an example. Its goal is to not only show how to use go-template, but to show my approach to building real-world backend apps using Go, which you might find useful.
The project follows standard Go project layout and is directly written to be a guide to writing more maintainable and easily extensible code in Go.
Dependencies
Let's move on to libraries, which I used in this project, and which this application is build around.
- grpc-go - building gRPC server
- grpc-gateway - building RESTful HTTP proxy on top of gRPC server
- wire - dependency injection
- migrate - working with database migrations
- viper - reading configuration files
- jwt-go - JWT authorization
- zap - logging
- optimus-go - hiding database ids from end user
- sqlx - better SQL experience
- ginkgo and gomega - BDD testing
- gomock and go-sqlmock - mocking dependencies for testing
Feel free to explore if you are interested in learning any one of these libraries or if you want to build highly decoupled apps but have not found your best approach yet.
How to run
- Make sure you have Go and Buf installed.
- Start Postgres database locally on port
:5432
.
The easiest way is to start Postgres container in Docker.
To do it, install Docker first, then rundocker run -p 5432:5432 --name go-example_db -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password postgres
in your terminal (username and password are set to defaults which match the values inconfigs/*.env
files). To stop the container afterwards, rundocker stop go-example_db
. - Clone this repository.
- Run
make init
command to install required dependencies. - Run
go mod tidy
command to install libraries used in this project. - Build the project using
make build
command - Run compiled binary with
bin/server -dev
command to run it in development mode or change-dev
flag to-stage
or-prod
to run in staging and production environments accordingly (for simplicity, the configuration is the same for each environment, feel free to change*.yaml
and*.env
files inconfig/
directory)
More to come
Currently, this is not final form of this project, here are some things you will see in the future:
- Github CI/CD pipeline
- Containerization using Docker
- More test coverage (mw, server and pkg tests)
Might be implemented:
- TLS configuration for HTTPS support
- Kubernetes cluster
- NGINX proxy