/go-example

go-template example

Primary LanguageGo

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.

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

  1. Make sure you have Go and Buf installed.
  2. Start Postgres database locally on port :5432.
    The easiest way is to start Postgres container in Docker.
    To do it, install Docker first, then run docker 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 in configs/*.env files). To stop the container afterwards, run docker stop go-example_db.
  3. Clone this repository.
  4. Run make init command to install required dependencies.
  5. Run go mod tidy command to install libraries used in this project.
  6. Build the project using make build command
  7. 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 in config/ 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