A lightweight and scalable REST API server built with GoLang.
This project is a template for building RESTful APIs using Go. It includes features such as JWT authentication, database migrations, and Swagger documentation.
- JWT Authentication: Secure your endpoints with JSON Web Tokens.
- Database Migrations: Manage your database schema changes with ease.
- Swagger Documentation: Automatically generated API documentation.
- Docker Support: Easily containerize your application.
- CI/CD Pipeline: Automated build and deployment using GitHub Actions.
The project is organized as follows:
api-template/
├── .github/
│ └── workflows/
│ └── ci.yml # GitHub Actions CI/CD pipeline configuration
├── cmd/
│ └── main.go # Main application entry point
├── docs/
│ ├── docs.go # Swagger documentation generated by swaggo
│ ├── swagger.json # Swagger JSON file
│ └── swagger.yaml # Swagger YAML file
├── internal/
│ ├── handlers/
│ │ ├── auth.go # Authentication handlers
│ │ ├── auth_test.go # Tests for authentication handlers
│ │ ├── healthcheck.go # Health check handler
│ │ └── handlers_test.go # Tests for handlers
│ ├── middleware/
│ │ ├── auth.go # JWT authentication middleware
│ │ ├── auth_test.go # Tests for authentication middleware
│ │ └── cors.go # CORS middleware
│ ├── migrations/
│ │ ├── 001_create_users_table.up.sql # SQL migration to create users table
│ │ ├── 001_create_users_table.down.sql # SQL migration to drop users table
│ │ └── migrations.go # Migration files embedded using Go embed
│ ├── models/
│ │ └── user.go # User model definition
│ ├── routes/
│ │ └── routes.go # Route registration
│ ├── services/
│ │ └── user_service.go # User service for database operations
│ └── utils/
│ └── errors.go # Utility functions for error handling
├── .env # Environment variables file
├── .env.example # Example environment variables file
├── .gitignore # Git ignore file
├── docker-compose.yml # Docker Compose configuration
└── Dockerfile # Dockerfile for building the application
This structure separates concerns into different packages, making the codebase modular and easier to maintain. The internal directory contains core application logic, while configuration and deployment files are located at the root level.
- Go 1.23 or later
- Docker (optional, for containerization)
- SQLite (for local development)
-
Clone the repository:
git clone https://github.com/justyork/api-template.git cd api-template -
Copy the example environment file:
cp .env.example .env
-
Update
.envwith your configuration. -
Run the application:
go run cmd/main.go
-
Build the Docker image:
docker build -t api-template . -
Run the Docker container:
docker-compose up
Execute the following command to run tests:
go test ./... -vAccess the Swagger UI at http://localhost:8080/swagger/ to view the API documentation.
Explore the live demo of the deployed project and its Swagger documentation.
The project includes a GitHub Actions workflow for continuous integration and deployment. The pipeline is triggered on
pushes to the main branch and performs the following steps:
- Checkout code
- Setup Go environment
- Build the application
- Upload the binary to the server
- Run the application on the server
This project is licensed under the MIT License. See the LICENSE file for details.
For support, contact yorkshp@gmail.com.