api_template
A Golang API with MySQL database support, featuring dynamic API endpoints and Swagger documentation. The project is containerized using Docker and managed via Docker Compose, ensuring a streamlined development and deployment process.
Features π
β
Modular Code Structure β Organized into controllers, models, middlewares, and utilities.
β
JWT Authentication β Secure API with authentication and role-based access control.
β
Dynamic API β Auto-generated CRUD endpoints for structured data models.
β
Swagger Documentation β Auto-generated API docs for easy usage.
β
Dockerized Deployment β Seamless setup with Docker Compose.
β
Persistent MySQL Database β Ensures data remains intact across restarts.
Using as a Template ποΈ
This project is designed to be a boilerplate template for Golang-based REST APIs. You can use it as a foundation for building your own API without starting from scratch.
How to Use it as a Template
1οΈβ£ Click "Use this template" on GitHub.
2οΈβ£ Clone your new repository:
git clone https://github.com/yourusername/yourproject.git
cd yourproject
3οΈβ£ Update Module Name in go.mod:
module github.com/yourusername/yourproject
Then, run:
go mod tidy
4οΈβ£ Modify Models & Controllers
- Add your own data models inside
models/.
- Create custom endpoints in
controllers/.
- Adjust database migrations in
database/.
5οΈβ£ Run Your API π
docker-compose up --build
π Your Golang API is now running! Modify and expand it as needed.
Getting Started π
Prerequisites π οΈ
Installation & Setup βοΈ
1. Clone the repository
git clone https://github.com/r4ulcl/api_template.git
cd api_template
2. Start the application using Docker
docker-compose up --build
This command will:
- Start a MySQL database container (
db).
- Build and launch the Go API application (
app).
- Expose the API on
http://localhost:8080.
Project Structure π
api_template/
βββ api/ # Contains all API-related logic
β βββ controllers/ # Request handlers for API endpoints (business logic)
β βββ middlewares/ # Authentication, authorization, and other middleware
β βββ routes/ # Routing definitions that map endpoints to controllers
βββ database/ # Database connection and query logic
βββ docs/ # Swagger/OpenAPI files and other documentation
βββ utils/ # Utility functions (e.g., hashing, JWT creation)
β βββ models/ # Data models and structs (e.g., User, Roles)
βββ main.go # Application entry point: runs the server
βββ Dockerfile # Instructions to containerize the application
βββ docker-compose.yml # Docker Compose config for multi-service setups
βββ go.mod # Go module dependencies and module path
βββ go.sum # Dependency checksums for reproducible builds
Environment Variables βοΈ
The application requires some environment variables to be set. These are defined in docker-compose.yml.
| Variable |
Description |
Default Value |
DB_HOST |
MySQL Database Host |
db |
DB_PORT |
MySQL Port |
3306 |
DB_USER |
MySQL Username |
demo_user |
DB_PASSWORD |
MySQL Password |
demo_pass |
DB_NAME |
MySQL Database Name |
demo_db |
JWT_SECRET |
JWT Secret Key for Tokens |
your_jwt_secret_key |
ADMIN_PASSWORD |
Default Admin Password |
SuperSecurePassword |
β οΈ Important: Modify these values in docker-compose.yml or set them manually before running the app.
API Documentation π
Swagger UI is available at:
π http://localhost:8080/swagger/index.html
This provides a detailed overview of all endpoints, parameters, and responses.
Usage π
1. Register a New User
curl -X POST "http://localhost:8080/register" \
-H "Content-Type: application/json" \
-d '{"username": "testuser", "password": "password123", "role": "user"}'
2. Login to Get JWT Token
curl -X POST "http://localhost:8080/login" \
-H "Content-Type: application/json" \
-d '{"username": "testuser", "password": "password123"}'
Response:
{
"token": "your.jwt.token"
}
3. Access Protected Routes
Include the JWT token in the Authorization header:
curl -X GET "http://localhost:8080/xxxxxxx" \
-H "Authorization: Bearer your.jwt.token"
License π
π MIT License β Feel free to use, modify, and distribute this project.
Contributors π€
π Maintained by: r4ulcl
π‘ Have suggestions or found an issue? Open a pull request or file an issue in the repository!