Skip to content

This is a Go backend service built using clean architecture principles. It provides a robust foundation for building scalable and maintainable web applications.

Notifications You must be signed in to change notification settings

Rafli-Dewanto/go-backend-skeleton

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Backend Service

This is a Go backend service built using clean architecture principles. It provides a robust foundation for building scalable and maintainable web applications.

Features

  • Clean Architecture implementation
  • HTTP server using standard net/http package
  • PostgreSQL database integration with sqlx
  • Middleware support (CORS, XSS, Logging, etc.)
  • Structured logging with Zap
  • Configuration management
  • Graceful shutdown
  • Health check endpoints
  • Request ID tracking
  • Response compression
  • Security headers
  • Input validation
  • Error handling

Project Structure

├── cmd
│   └── main.go                 # Application entry point
├── internal
│   ├── entity                  # Business objects
│   ├── model                   # Database models
│   ├── repository             
│   │   ├── interfaces         # Repository interfaces
│   │   └── impl              # Repository implementations
│   ├── usecase
│   │   ├── interfaces        # UseCase interfaces
│   │   └── impl             # UseCase implementations
│   ├── delivery
│   │   ├── http             # HTTP handlers
│   │   └── grpc             # gRPC handlers (optional)
│   └── middleware           # Custom middleware
├── pkg
│   ├── logger               # Logging package
│   └── utils               # Utility functions
└── config                  # Configuration files

Prerequisites

  • Go 1.21 or later
  • PostgreSQL
  • Make (optional)

Getting Started

  1. Clone the repository
git clone https://github.com/yourusername/go-skeleton.git
cd go-skeleton
  1. Install dependencies
go mod download
  1. Configure the application
  • Copy config/config.json.example to config/config.json
  • Update the configuration values as needed
  1. Create the database
CREATE DATABASE myapp;
  1. Run the application
go run cmd/main.go

API Endpoints

Users API

  • POST /users - Create a new user
  • GET /users/{id} - Get user by ID
  • PUT /users/{id} - Update user
  • DELETE /users/{id} - Delete user
  • GET /users - List users (with pagination)

Health Check

  • GET /health - Check application health

Configuration

The application can be configured using the config/config.json file. The following configuration options are available:

{
    "server": {
        "port": "8080",
        "readTimeout": "15s",
        "writeTimeout": "15s",
        "idleTimeout": "60s"
    },
    "database": {
        "driver": "postgres",
        "dsn": "postgres://postgres:postgres@localhost:5432/myapp?sslmode=disable",
        "maxOpenConns": 25,
        "maxIdleConns": 25,
        "connMaxLifetime": "5m",
        "queryTimeout": "10s"
    },
    "logger": {
        "level": "debug",
        "file": "logs/app.log"
    },
    "security": {
        "allowedOrigins": ["*"],
        "allowedMethods": ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
        "allowedHeaders": ["Content-Type", "Authorization"],
        "maxRequestSize": 1048576,
        "rateLimit": {
            "requests": 100,
            "window": "1m"
        }
    }
}

Development

Adding a New Feature

  1. Create entity in internal/entity
  2. Create repository interface in internal/repository/interfaces
  3. Implement repository in internal/repository/impl
  4. Create use case interface in internal/usecase/interfaces
  5. Implement use case in internal/usecase/impl
  6. Create HTTP handler in internal/delivery/http
  7. Register routes in cmd/main.go

Running Tests

go test ./...

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

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

About

This is a Go backend service built using clean architecture principles. It provides a robust foundation for building scalable and maintainable web applications.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages