Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/titusjaka/go-sample

Go Backend Sample with the modular structure.
https://github.com/titusjaka/go-sample

backend clean-architecture golang postgres restful-api

Last synced: 3 months ago
JSON representation

Go Backend Sample with the modular structure.

Awesome Lists containing this project

README

        

# go-sample

[![GitHub release](https://img.shields.io/github/v/release/titusjaka/go-sample)](https://github.com/titusjaka/go-sample/releases/latest)
[![codecov](https://codecov.io/gh/titusjaka/go-sample/branch/main/graph/badge.svg?token=UNJY7V5SZL)](https://codecov.io/gh/titusjaka/go-sample)
[![Go Report Card](https://goreportcard.com/badge/github.com/titusjaka/go-sample)](https://goreportcard.com/report/github.com/titusjaka/go-sample)
[![GitHub license](https://img.shields.io/github/license/titusjaka/go-sample)](https://github.com/titusjaka/go-sample/blob/main/LICENSE)

Go Backend Sample. It’s suitable as a starting point for a REST-API Go application.

This example uses:
- [chi](https://github.com/go-chi/chi) for HTTP router;
- [kong](https://github.com/alecthomas/kong) for building neat commands;
- [PostgreSQL](https://www.postgresql.org/) as a database and [pgx](https://github.com/jackc/pgx) as a driver;
- [testify](https://github.com/stretchr/testify) and [mock](https://github.com/uber-go/mock) for tests;
- [ozzo-validation](https://github.com/go-ozzo/ozzo-validation) for request validation;
- [slog](https://go.dev/blog/slog) as a logger;
- [the Clean Architecture](http://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html) as the primary approach.

Features:

- Modular Project Structure.
- Built-in migration handling.
- Ready to go example with tests and mocks.
- Easy-to-go Docker deployment.

## Structure
```text
go-sample
β”œβ”€β”€ πŸ“ commands/ // Sub-commands for CLI (stands for Command Line Interface).
β”œβ”€β”€ πŸ“ internal/ // Internal packages for the application according to Go convention.
β”‚ β”œβ”€β”€ πŸ“ business/ // Business logic of the application.
β”‚ β”‚ └── πŸ“ snippets/ // A specimen business-logic package β€œsnippets” with REST-API for snippets creating, listing, and deleting.
β”‚ └── πŸ“ infrastructure/ // Infrastructure code of the application.
β”‚ β”œβ”€β”€ πŸ“ api/ // API-related utilities: middlewares, authentication, error handling for the transport layer.
β”‚ β”œβ”€β”€ πŸ“ kongflag/ // Helper package for Kong CLI.
β”‚ β”œβ”€β”€ πŸ“ nopslog/ // No-operation logger for tests.
β”‚ β”œβ”€β”€ πŸ“ postgres/ // PostgreSQL-related utilities.
β”‚ β”‚ β”œβ”€β”€ πŸ“ pgmigrator/ // PostgreSQL migration utilities.
β”‚ β”‚ └── πŸ“ pgtest/ // PostgreSQL test utilities.
β”‚ β”œβ”€β”€ πŸ“ service/ // Service-related reusable code: error handling for the service layer, etc.
β”‚ └── πŸ“ utils/
β”‚ └── πŸ“ testutils/ // Test utilities.
β”œβ”€β”€ πŸ“ migrations/ // This folder contains *.sql migrations.
└── main.go // Entry point for the application.
```

## Installation

```shell
git clone https://github.com/titusjaka/go-sample
```

## Usage

```shell
docker-compose up --build
```

## Future improvements
- [ ] Return verbose API errors with exact fields in it:
```json
{
"errors": {
"title": "Title must not be empty",
"expires_at": "Expires at must be within range 1-365 days"
}
}
```
- [ ] Add user authentication + session storage.
- [ ] Add `/status` handler with service health.