api_template

command module
v0.0.0-...-f49f964 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 6, 2025 License: MIT Imports: 9 Imported by: 0

README ΒΆ

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!

Documentation ΒΆ

The Go Gopher

There is no documentation for this package.

Directories ΒΆ

Path Synopsis
api
Package docs Code generated by swaggo/swag.
Package docs Code generated by swaggo/swag.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL