Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/yuraxdrumz/golang-starter-kit

A starter kit for golang + ports and adapters (hexagonal) architecture
https://github.com/yuraxdrumz/golang-starter-kit

golang hexagonal ports-and-adapters starter-kit starter-project starter-template

Last synced: 1 day ago
JSON representation

A starter kit for golang + ports and adapters (hexagonal) architecture

Awesome Lists containing this project

README

        

# Golang Starter Kit

A starter kit written in Golang + Ports and Adapters structure on top of

## Getting Started

1. `git clone [email protected]/yuraxdrumz/golang-starter-kit`
2. `go install github.com/golangci/golangci-lint/cmd/[email protected]`
3. Run `brew install pre-commit` / `pip install pre-commit`
4. Run `pre-commit install`

If you want to check pre-commit hook on current folder run `pre-commit run --all-files`

### Prerequisites

Ports and Adapters divides your code to 3 parts:

- Business-logic - These are your business rules + types, implemented without any dependency on 3rd party modules (self-contained)
- Ports - Interfaces to speak with your business rules
- Adapters - Implementations of the ports, There are two kinds of adapters:
- In(Driver) - your external API to the world. For example - `internal/pkg/adapters/in/http.go`
- Out(Driven) - what your business logic uses. For example - `internal/pkg/adapters/out/reverser/in-memory.go`

Usually, you divide `ports` and `adapters` to separate directories, but the best practice in golang is to keep structs near implementations. That is why, I decided to add `ports.go` near each adapter.

Adding a new business logic:

1. Create appropriate structs in `ports.go` file under `internal/app/your-use-case/ports.go`
2. Create your use-case with your application specific logic under `internal/app/your-use-case/logic.go`
3. Create your in/out adapter, for example - `Repository(out)` or `gRPC(in)`. `internal/pkg/adapters/*`
4. Tests!!! `your-file-name_test.go` under same directory as file `internal/app/your-use-case/logic_test.go`

### Installing Packages

```golang
go get -u
```

### Running the service

```bash
go run main.go
```

### Environment Variables

To read environement variables, this service uses `envconfig` library, which allows defining a struct of environment variables. You can easily add new variables under `internal/pkg/adapters/out/env`

The default is:

```golang

type Specification struct {
LogLevel string `envconfig:"LOG_LEVEL" default:"info"`
Port string `envConfig:"PORT" default:"8080"`
}

```

By default this service looks for .env in root folder.

### Logging

The repo uses `logrus` logger to write to stdout.

To change log level change the `LOG_LEVEL` environment variable

Possible log levels:

- info - default
- debug
- error
- warn
- fatal

### Pre Commit

You can add any pre commit hooks to `.pre-commit-config.yaml` if needed