Project Template

This is a general Go project template. You can use it as a start for your projects.

Here you should describe your project description.

Dependencies

As a database for the project we use Postgres 13.

For monitoring and error tracking we use Sentry.io.

Development tools installation

Golang

Golang is a main language for the project.

You can install it with instructions from official website: https://go.dev/doc/install.

Migration

We use migration tool to update scheme of the database. You can find instruction for installation here: https://github.com/golang-migrate/migrate/tree/master/cmd/migrate .

Mocking

For mocking we use https://github.com/vektra/mockery.

You can download it with this command:

go install github.com/vektra/mockery/v2@latest

Service configuration

For the correct service work, you need to use environment variables or the .env file.

In order to use the file, download the godotenv binary file. General command to run .env file:

godotenv -f .env [command]

Environment Variables

Variable Default Description
LOG_LEVEL debug Log level for logger. Possible options: trace, debug, info, warning, error, fatal and panic.
SERVER_PORT 8080 Port on which app will run.
SERVER_READ_TIMEOUT 15s App read response time.
SERVER_WRITE_TIMEOUT 15s App write response time.
DB_USER root Postgres database user.
DB_PASS password Postgres database password.
DB_HOST db Postgres database host.
DB_PORT 5432 Postgres database port.
DB_NAME app Postgres database name.
SENTRY_DSN Sentry DSN.
SENTRY_ENV staging Sentry environment.

Installation

Manual

You can build the service with the following terminal command:

go build ./cmd/...

Before running the service set up the environment variables and run the app:

app

Docker installation

Installation could be done with docker and docker compose tools. Set up the environment variables or use .env file.

docker compose up --build

Migrations

Migrations are run automatically on service startup. But you can run them manually with golang-migrate tool.

Database pattern: postgresql://DB_USER:DB_PASS@DB_HOST:DB_PORT/DB_NAME?sslmode=disable

# create migration
migrate create -ext sql -dir ./test-migrations [migration name]

# manual migrations
migrate -database [database url] -path ./test-migrations up

# migration with URL
migrate -database postgresql://postgres:postgres@localhost:5432/postgres?sslmode=disable -path ./test-migrations up

Testing

Unit-tests are using mocks generated by mockery. Mocks generation commands are embedded in-source using go:generate comments, so before running tests, you need to launch code generation.

Integration tests run only with docker. You have to wait first time to load needed images.

Unit test also run during build.

# Run code generation
go generate ./...

# Run only unit tests
go test ./... -short

# Run all tests
go test ./... 

Documentation

Swagger UI is a graphical representation of swagger specifications. You can access REST documentation with "[base_endpoint]/docs/".

You can check availability of the application with "[base_endpoint]/status" endpoint.

Note

I suggest to move pkg package to a separate repository.