Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/mikhail-bigun/go-app-template

Go application template
https://github.com/mikhail-bigun/go-app-template

Last synced: 3 months ago
JSON representation

Go application template

Lists

README

        

# App [![Mentioned in Awesome Fiber](https://awesome.re/mentioned-badge-flat.svg)](https://github.com/gofiber/awesome-fiber)

Golang application template. Based on clean architecture principles
- `internal/domain` - business models and rules
- `internal/app` - "App" applicaiton setup
- `internal/usecase` - business logic implementations through usecase and repository interfaces
- `internal/controller` - controllers implementations that orchestrate various usecases
- `migrations` - application migrations
- `config` - application configuration structs and helpers
- `pkg` - additional, non-dependant infrastructure and logic that can be also used externaly, outside of current project scope
- `cmd` - entrypoints to the applications
- `embed.go` - files and directories that have to be embeded into the applications (migrations for ex.)
- `gen` - autogenerated stubs
## List of contents
- [App ](#app-)
- [List of contents](#list-of-contents)
- [Project requirements](#project-requirements)
- [Install Go helpers](#install-go-helpers)
- [gocritic](#gocritic)
- [golangci-lint](#golangci-lint)
- [gosec](#gosec)
- [swag](#swag)
- [migrate](#migrate)
- [Makefile](#makefile)
- [Postgres migrations](#postgres-migrations)
- [Application](#application)
- [Docker](#docker)
- [Application](#application-1)
- [Postgres](#postgres)
- [Redis](#redis)
- [Configuration](#configuration)
- [env](#env)
- [yaml](#yaml)
- [json](#json)
## Project requirements
- Go 1.19
- Docker
## Install Go helpers
### gocritic
Highly extensible Go source code linter providing checks currently missing from other linters.
```bash
go install github.com/go-critic/go-critic/cmd/gocritic@latest
```
### golangci-lint
Fast Go linters runner. It runs linters in parallel, uses caching, supports yaml config, has integrations with all major IDE and has dozens of linters included.
```bash
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
```
### gosec
Inspects source code for security problems by scanning the Go AST.
```bash
go install github.com/securego/gosec/v2/cmd/gosec@latest
```
### swag
Swag converts Go annotations to Swagger Documentation 2.0.
```bash
go install github.com/swaggo/swag/cmd/swag@latest
```
### migrate
Database migrations written in Go. Use as CLI or import as library.
```bash
go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
```
## Makefile
### Postgres migrations
Default path for migrations folder is `migrations/$(APP_NAME)`

To create new migration
```bash
make migrate.create MIGRATIONS_FOLDER=migrations/my_app NAME=create_my_table
```
To apply migrations
```bash
make migrate.up
```
To reverse migrations
```bash
make migrate.down
```
To force migrations up to a certain version
```bash
make migrate.force VERSION=000003
```
### Application
To test, build and run application
```bash
make run
```
### Docker
To start project within docker network
```bash
make docker.run
```
To stop
```bash
make docker.stop
```
#### Application
To build application docker image
```bash
make docker.buld.app
```
You can also override build args to access GOPRIVATE repositories inside container
```bash
make docker.app.build GOPRIVATE="" GOPRIVATE_USER="" GOPRIVATE_PAT="" GOPRIVATE_SCHEMA=""
```
To start application
```bash
make docker.run.app
```
To stop application
```bash
make docker.stop.app
```
#### Postgres
To start postgres instance
```bash
make docker.run.postgres
```
To stop
```bash
make docker.stop.postgres
```
#### Redis
To start redis instance
```bash
make docker.run.redis
```
To stop
```bash
make docker.stop.redis
```
## Configuration
### env
```bash
LOGGER_LEVEL="info" # standard logger level options (panic, fatal, warn/warning, info, debug, trace)

HTTP_HOST="0.0.0.0"
HTTP_PORT="8000"
HTTP_TIMEOUT="4s"
HTTP_PREFIX=""
HTTP_API_PATH="/api"

TLS_CERT_FILEPATH=""
TLS_KEY_FILEPATH=""

POSTGRES_HOST="127.0.0.1"
POSTGRES_PORT="5432"
POSTGRES_SSL_MODE="disable"
POSTGRES_DB="postgres"
POSTGRES_USER="postgres"
POSTGRES_PASSWORD="postgres"
POSTGRES_MAX_CONNS="10"
POSTGRES_MIN_CONNS="2"
POSTGRES_MAX_CONN_LIFETIME="10m"
POSTGRES_MAX_CONN_IDLE_TIME="1m"
POSTGRES_HEALTH_CHECK_PERIOD="10s"

REDIS_HOST="127.0.0.1"
REDIS_PORT="6379"
REDIS_USERNAME=""
REDIS_PASSWORD=""
REDIS_DB="0"

SWAGGER_HOST="127.0.0.1:8888"
SWAGGER_BASE_PATH="/api"
```
### yaml
```yaml
logger:
level: info
http:
host: 0.0.0.0
port: 8000
timeout: 4s
prefix: ""
apiPath: /api
tls:
cert:
filepath: ""
key:
filepath: ""
postgres:
host: 127.0.0.1
port: 5432
sslMode: disable
db: postgres
user: postgres
maxConns: 10
minConns: 2
maxConnLifetime: 10m
maxConnIdleTime: 1m
healthCheckPeriod: 10s
redis:
host: 127.0.0.1
port: 6379
username: ""
password: ""
db: 0
swagger:
host: 127.0.0.1:8888
basePath: /api
```
### json
```json
{
"logger": {
"level": "info",
"file": {
"path": "",
"name": "",
"max_age": "24h",
"rotation_time": "168h"
},
"format": {
"type": "text",
"caller": false,
"pretty": false
}
},
"http": {
"host": "0.0.0.0",
"port": "8000",
"timeout": "4s",
"prefix": "",
"api_path": "/api"
},
"tls": {
"cert": {
"filepath": "",
},
"key": {
"filepath": "",
}
},
"postgres": {
"host": "127.0.0.1",
"port": "5432",
"ssl_mode":"disable",
"db": "postgres",
"user": "postgres",
"max_conns": 10,
"min_conns": 2,
"max_conn_lifetime": "10m",
"max_conn_idle_time": "1m",
"health_check_period": "10s"
},
"redis": {
"host": "127.0.0.1",
"port": "6379",
"username": "",
"password": "",
"db": 0
},
"swagger": {
"host": "127.0.0.1:8888",
"base_path": "/api"
}
}
```