Skip to content

s0j0hn/go-rest-boilerplate-echo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Go REST API Boilerplate with Echo

A modern, feature-rich RESTful API boilerplate built with Go and Echo framework. This project provides a solid foundation for building scalable and maintainable web services with best practices baked in.

Go Version Echo Framework License

πŸš€ Features

  • Echo Framework: High performance, extensible, minimalist Go web framework
  • GORM: The fantastic ORM library for Golang
  • Casbin: Powerful and efficient open-source access control library
  • RabbitMQ: Message broker for asynchronous processing
  • WebSockets: Real-time communication between server and clients
  • Swagger: Automated API documentation
  • Viper: Complete configuration solution
  • Zerolog: Zero allocation JSON logger
  • Validator: Request validation
  • Rate Limiting: Built-in protection against DoS attacks
  • Graceful Shutdown: Proper handling of server shutdown

πŸ“ Prerequisites

πŸ›΄οΈ Quick Start

Clone the repository

git clone <repository-url>
cd go-rest-boilerplate-echo

Install dependencies

make dep
# or manually:'
go mod download

Configure the application

# Copy the example config file
cp config.yaml.example config.yaml

# Edit the config file with your database and RabbitMQ credentials
# using your favorite text editor
vim config.yaml

Start services with Docker (optional)

The project includes a Docker Compose configuration for local development:

make start-services

This will start PostgreSQL and RabbitMQ containers.

Run database migrations

go run ./database/migrate/migrate.go

Launch the server

make serve
# or manually:
go run main.go

The API will be available at http://localhost:8080

API Documentation

Once the server is running, access the Swagger documentation at:

http://localhost:8080/swagger/index.html

πŸ‘ Project Structure

┋── config/               # Configuration files and functionality
┋── database/             # Database connection and models
│── ┋── migrate/           # Database migration scripts
│── ┋── models/            # Database models (GORM)
┋── docs/                 # API documentation (Swagger)
┋── handlers/             # HTTP request handlers
┋── policy/               # Authorization policies (Casbin)
┋── rabbitmq/             # Message queue clients and task management
┋── websocket/            # WebSocket server implementation
┋── main.go               # Application entry point
┋── config.yaml.example   # Example configuration file

πŸ”Œ API Endpoints

The boilerplate includes a fully-functional tenant management API:

Method Endpoint Description
GET /tenants Get a list of all tenants
GET /tenants/:id Get a specific tenant by ID
POST /tenants Create a new tenant
PUT /tenants Update an existing tenant
DELETE /tenants/:id Delete a tenant
GET /swagger/* Swagger API documentation

πŸ”  Authentication and Authorization

The boilerplate uses Casbin for access control. Policies are defined in the policy/policy.go file and can be customized according to your requirements.

πŸ”¨ Asynchronous Processing

Tasks are processed asynchronously using RabbitMQ. The system includes:

  • Push and listen queues
  • Task status tracking
  • Real-time updates via WebSockets

πŸ§ͺ Testing

Run tests using:

make test
# or manually:
go test ./...

For test coverage:

make coverage

πŸ“§ Docker Support

Build the Docker image:

docker build -t go-rest-boilerplate-echo .

Run the container:

docker run -p 8080:8080 go-rest-boilerplate-echo

πŸ‘Œ Development Guidelines

Code Style

This project follows the standard Go code style conventions:

# Format code
go fmt ./...

# Check for code issues
go vet ./...

Adding a New Model

  1. Create a new model file in the database/models/ directory
  2. Add the model to the migration process in database/migrate/migrate.go
  3. Create a handler in the handlers/ directory
  4. Register routes in main.go
  5. Add authorization policies in policy/policy.go

πŸ₯ Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ‘— Acknowledgements