Skip to content

podanypepa/wbrestapi

Repository files navigation

wbrestapi

A simple RESTful microservice built in Go using Fiber v2, GORM, and PostgreSQL.

CI


Architecture

This application follows the principles of Hexagonal Architecture (Ports and Adapters). The goal is to isolate the business logic from infrastructure, enabling better testability, maintainability, and flexibility.

Key Components:

  • Domain Layer: Core business models and logic (internal/domain).
  • Application Layer: Use cases and interfaces/ports (internal/application).
  • Adapters Layer: Infrastructure implementations - HTTP handlers, database repositories (internal/adapter).
  • Configuration Layer: Centralized configuration management (internal/config).
  • Entrypoint: Application initialization and wiring (cmd/server).

This separation keeps the core application logic independent from frameworks, databases, and external systems.


πŸ“„ Project Assignment

This project implements the following task: View assignment

Functional Requirements

  • POST /save: Store user data in the database.
  • GET /:id: Retrieve user data by external_id.

πŸ“š API Documentation

API documentation is available in OpenAPI 3.0 format: OpenAPI Specification

API Endpoints

Health Check

GET /healthz

Response:

{"status": "ok"}

Create User

POST /save
Content-Type: application/json

{
  "external_id": "550e8400-e29b-41d4-a716-446655440000",
  "name": "John Doe",
  "email": "john.doe@example.com",
  "date_of_birth": "1990-01-15T00:00:00Z"
}

Success Response (201):

{
  "id": 1,
  "external_id": "550e8400-e29b-41d4-a716-446655440000",
  "name": "John Doe",
  "email": "john.doe@example.com",
  "date_of_birth": "1990-01-15T00:00:00Z"
}

Error Responses:

  • 400 Bad Request: Invalid input or validation error
  • 409 Conflict: User with this external_id already exists
  • 429 Too Many Requests: Rate limit exceeded
  • 500 Internal Server Error: Server error

Get User

GET /{external_id}

Success Response (200): Same as Create User response

Error Responses:

  • 404 Not Found: User not found
  • 429 Too Many Requests: Rate limit exceeded
  • 500 Internal Server Error: Server error

βš™οΈ Requirements


πŸš€ Getting Started

1. Clone the repo

git clone https://github.com/podanypepa/wbrestapi.git
cd wbrestapi

2. Create .env file

Copy .env.localhost and modify if needed:

cp .env.localhost .env

Environment config examples:

3. Run the project with Docker Compose

make docker-up

This will start the app and PostgreSQL database, initialize schema, and make the app available at http://localhost:3000.


πŸ§ͺ Running Tests

Unit Tests

make test
# or
go test -v ./...

Integration Tests

Integration tests start a real server and test HTTP endpoints:

cd cmd/server
go test -v

Note: Integration tests require PostgreSQL running on localhost:5432


πŸ“¦ Build and Run Locally (without Docker)

make build
make run

# or run in dev/watch mode
make dev

🧹 Stop and Clean Up

make docker-down

To reset DB data:

rm -rf ./data

πŸ› οΈ CLI Tools for testing


βœ… Features

  • ✨ JSON REST API with Go + Fiber
  • 🐘 PostgreSQL + GORM
  • πŸ” Input validation with structured errors
  • 🚦 Rate limiting (100 req/min by default)
  • πŸ“ Structured logging (JSON format)
  • πŸ›‘οΈ Panic recovery middleware
  • πŸ₯ Health check endpoint
  • ⚑ Graceful shutdown (SIGINT/SIGTERM)
  • 🐳 Dockerized and portable
  • βœ… Comprehensive test coverage (unit + integration)
  • πŸ“Š Database connection pooling
  • πŸ”„ CI/CD with GitHub Actions
  • πŸ“– OpenAPI 3.0 documentation
  • 🎯 Hexagonal Architecture

πŸ”§ Configuration

Configure via environment variables:

Server Configuration

  • PORT - Server port (default: 3000)
  • SHUTDOWN_TIMEOUT - Graceful shutdown timeout (default: 5s)
  • RATE_LIMIT_MAX - Max requests per window (default: 100)
  • RATE_LIMIT_WINDOW - Rate limit time window (default: 1m)
  • LOG_LEVEL - Logging level: info, debug (default: info)

Database Configuration

  • DB_HOST - Database host
  • DB_USER - Database user
  • DB_PASSWORD - Database password
  • DB_NAME - Database name
  • DB_PORT - Database port
  • DB_SSL - SSL mode (disable, require, etc.)
  • DB_MAX_OPEN_CONNS - Max open connections (default: 25)
  • DB_MAX_IDLE_CONNS - Max idle connections (default: 5)
  • DB_CONN_MAX_LIFETIME - Connection max lifetime (default: 5m)

πŸ—οΈ Project Structure

wbrestapi/
β”œβ”€β”€ api/                      # API documentation
β”‚   └── openapi.yaml         # OpenAPI 3.0 specification
β”œβ”€β”€ cmd/
β”‚   └── server/              # Application entrypoint
β”‚       β”œβ”€β”€ main.go
β”‚       └── integration_test.go
β”œβ”€β”€ internal/
β”‚   β”œβ”€β”€ adapter/             # Infrastructure adapters
β”‚   β”‚   β”œβ”€β”€ handler/         # HTTP handlers
β”‚   β”‚   └── repository/      # Database repositories
β”‚   β”œβ”€β”€ application/         # Application layer
β”‚   β”‚   β”œβ”€β”€ port/           # Interfaces (ports)
β”‚   β”‚   └── usecase/        # Use cases
β”‚   β”œβ”€β”€ config/             # Configuration
β”‚   └── domain/             # Domain models and logic
β”œβ”€β”€ .github/
β”‚   └── workflows/          # CI/CD pipelines
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ compose.yaml
β”œβ”€β”€ Makefile
└── README.md

🀝 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.

About

No description, website, or topics provided.

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors