# README
Go Backend Template
This is a basic template for a Go backend with PostgreSQL. It includes best practices and boilerplate code to get you started quickly.
To use this template repo, you will need to change all instances of github.com/danyeric123/backend-service
to your new name github.com/$USERNAME/$REPO_NAME
where USERNAME
is your username and REPO_NAME
is the new name of the repo
Features
- Dockerized Setup: Easily set up and run the application using Docker and Docker Compose.
- PostgreSQL Integration: Pre-configured to connect to a PostgreSQL database.
- Structured Logging: Uses
logrus
for structured logging. - Environment Variables: Uses environment variables for configuration.
- Dependency Injection: Makes the code more testable and modular.
Project Structure
go-backend-template/
├── cmd/
│ └── main.go
├── db/
│ └── db.go
├── handlers/
│ └── handler.go
├── Dockerfile
├── docker-compose.yaml
├── go.mod
├── go.sum
├── init.sql
├── Makefile
└── README.md
Getting Started
To use this template repo, follow these steps:
-
Clone the Repository: Clone this repository to your local machine.
-
Replace Module Path: Change all instances of
github.com/danyeric123/backend-service
to your new module pathgithub.com/$USERNAME/$REPO_NAME
whereUSERNAME
is your GitHub username andREPO_NAME
is the new name of the repository. -
Set Up Environment Variables: Create a
.env
file in the root directory with the following variables:POSTGRES_USER=your_postgres_user POSTGRES_PASSWORD=your_postgres_password POSTGRES_DB=your_postgres_db POSTGRES_HOST=db
-
Build and Run: Use the provided
Makefile
commands to build and run the application.
Prerequisites
- Docker
- Docker Compose
Running the Application
NOTE: You will need to have an .env
file for POSTGRES_DB
POSTGRES_USER
, POSTGRES_PASSWORD
, POSTGRES_HOST
(see below)
-
Build the Docker images:
make build
-
Run the application:
make run
-
Access the application at
http://localhost:8080
.
Cleaning Up
To stop and remove the Docker containers, run:
make clean
Commands
make help
- Show this help messagemake check_env
- Check if .env file existsmake build
- Build docker containersmake run
- Run docker containers, rebuild if neededmake clean
- Stop and remove docker containersmake fmt
- Format codemake lint
- Lint code
Database Initialization
The init.sql file contains SQL statements to initialize the database with some boilerplate data. This file is automatically executed when the PostgreSQL container is started.
Environment Variables
Set the following environment variables in your .env
file or in your environment:
POSTGRES_USER
: The PostgreSQL userPOSTGRES_PASSWORD
: The PostgreSQL passwordPOSTGRES_DB
: The PostgreSQL database namePOSTGRES_HOST
: The PostgreSQL host (usually db when using Docker Compose)
Explanation of Chosen Packages
- logrus:
logrus
is used for structured logging. It provides log levels, hooks, and formatters, making it a powerful and flexible logging library compared to the standardlog
package. - mux:
mux
is a powerful URL router and dispatcher for matching incoming requests to their respective handler. Though the standard library'shttp
package would suffice,mux
adds more flexibility and features such as variables in routes, middleware support, and more. - sqlx:
sqlx
is an extension of the standarddatabase/sql
package. It has become a standard for working with SQL databases in Go due to its additional functionalities. - pq:
pq
is a pure Go Postgres driver for thedatabase/sql
package. PostgreSQL is a powerful, open-source object-relational database system, andpq
provides reliable and efficient connectivity to PostgreSQL databases.