[go: up one dir, main page]

Skip to content

A simple API server built with Golang using the Gin Framework and supports live reloading during development using Air, Validators Auto-Registration and has an integrated migration system.

License

Notifications You must be signed in to change notification settings

faizahmedfarooqui/go-gin-api-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Go-Gin API Server

This project is a simple API server built with Go using the Gin Web Framework.

The project supports live reloading during development using Air, has an integrated migration system, auto-registeration of validators and cron scheduling system.

Table of Contents


Prerequisites

Before setting up the project, ensure you have the following installed:

Installation

  1. Clone the repository:
git clone https://github.com/yourusername/go-gin-api-server.git
cd go-gin-api-server
  1. Install dependencies: Ensure all Go dependencies and tools (including Air) are installed.
go mod tidy
go install github.com/air-verse/air@latest
  1. Setup environment variables: Create a .env file with your database and other environment variables.
cp .env.example .env

Example .env file:

# Available modes: debug, release or test
GIN_MODE=debug

# The port that the application will run on
APP_PORT=3000

# Database configuration
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=your_password
DB_NAME=postgres
DB_SSLMODE=disable

Development

To start the server in development mode with live-reloading (using Air):

make develop

Project Structure

.
β”œβ”€β”€ main.go                   # Entry point of the application
β”œβ”€β”€ tools.go                  # Track tools used in the project
β”œβ”€β”€ Makefile                  # Makefile for common tasks
β”œβ”€β”€ air.toml                  # Air configuration for live-reloading
β”œβ”€β”€ go.mod                    # Go module file
β”œβ”€β”€ go.sum                    # Go module file
β”œβ”€β”€ cmd
β”‚   β”œβ”€β”€ generate_validators   # Tool to auto-generate validators
β”‚   β”‚   └── main.go
β”‚   β”œβ”€β”€ migrate               # Tool to run database migrations
β”‚   β”‚   └── main.go
β”‚   └── schedules             # Tool to register and run cron jobs
β”‚       └── main.go
β”œβ”€β”€ schedules                 # Package for cron tasks
β”‚   └── tasks.go              # Decoupled task logic for cron jobs
β”œβ”€β”€ config                    # Configuration files
β”‚   └── database.go
β”œβ”€β”€ controllers               # API route handlers
β”‚   β”œβ”€β”€ auth_controller.go
β”‚   └── item_controller.go
β”œβ”€β”€ database                  # Database-related code
β”‚   β”œβ”€β”€ migrate.go            # Migration logic
β”‚   └── migrations            # SQL migration files
β”‚       β”œβ”€β”€ 000001_create_items_table.up.sql
β”‚       β”œβ”€β”€ 000001_create_items_table.down.sql
β”‚       └── 000002_create_users_table.up.sql
β”‚       β”œβ”€β”€ 000002_create_users_table.down.sql
β”œβ”€β”€ middlewares               # Middleware logic
β”‚   └── error_handler.go
β”œβ”€β”€ models                    # Data models
β”‚   β”œβ”€β”€ item.go
β”‚   └── user.go
β”œβ”€β”€ repositories              # Data access layer
β”‚   β”œβ”€β”€ item_repository.go
β”‚   └── user_repository.go
β”œβ”€β”€ routes                    # API routes
β”‚   └── routes.go
β”œβ”€β”€ services                  # Business logic
β”‚   β”œβ”€β”€ auth_service.go
β”‚   └── item_service.go
β”œβ”€β”€ tmp                       # Temporary files (excluded from version control)
β”‚   └── main
β”œβ”€β”€ utils                     # Utility functions
β”‚   └── password.go
└── validators                # Input validation logic
    β”œβ”€β”€ auth_validator.go
    β”œβ”€β”€ item_validator.go
    β”œβ”€β”€ auto_generated.go     # Auto-generated file
    └── register.go           # Handles go:generate directive

Explanation of Directories

  • cmd/: This directory contains subdirectories for command-line tools. Currently, there are two:

    • generate_validators: Contains main.go, which is responsible for auto-generating the validator registration.
    • migrate: Contains main.go, which handles database migration commands such as migrate-up and migrate-down.
    • schedules: Contains main.go, which is responsible for registering and running cron jobs.
  • config/: Contains configuration-related files, such as database.go, which is responsible for initializing the database connection.

  • controllers/: This directory contains the handlers for your API endpoints. Each file corresponds to a different part of the API:

    • auth_controller.go: Handles authentication-related API routes (e.g., login, register).
    • item_controller.go: Handles item-related routes (e.g., CRUD operations for items).
  • database/: Contains all database-related code:

    • migrate.go: The migration logic, handling applying and rolling back migrations.
    • migrations/: Directory containing SQL migration files, including both .up.sql (for applying migrations) and .down.sql (for rolling back).
  • middlewares/: This directory contains middleware logic, such as error_handler.go, which is responsible for handling validation and binding errors.

  • models/: Defines the data models for the application:

    • item.go: Defines the structure for the Item model.
    • user.go: Defines the structure for the User model.
  • repositories/: Contains the data access layer, which abstracts database queries for different models:

    • item_repository.go: Provides the database access methods for the Item model.
    • user_repository.go: Provides the database access methods for the User model.
  • routes/: Responsible for setting up the API routes:

    • routes.go: Contains the function that configures all the routes for the application.
  • services/: This directory contains the business logic of the application:

    • auth_service.go: Contains the logic for user authentication, such as login and registration.
    • item_service.go: Contains the business logic for managing items.
  • tmp/: Temporary files created during development, such as the Go binary generated by Air for live-reloading. This directory is excluded from version control.

  • tools.go: A Go file that is used to track tools like Air. This file ensures development tools are included in go.mod and can be installed by others working on the project.

  • utils/: This directory contains utility functions, such as password.go, which includes password hashing and validation logic.

  • validators/: This directory contains all the input validation logic for your application:

    • auth_validator.go: Defines validators for authentication-related data (e.g., login, register).
    • item_validator.go: Defines validators for item-related data (e.g., item creation).
    • auto_generated.go: This file is auto-generated and contains dynamic registration of validators.
    • register.go: Handles the go:generate directive for generating the auto_generated.go file.
  • schedules/: Contains the logic for scheduling and running cron jobs:

    • tasks.go: Contains the decoupled task logic for cron jobs.

Database Migrations

The project includes a migration system for managing database schema changes:

Apply Migrations

To apply all pending migrations, run:

make migrate-up

Rollback Migrations

To rollback the last migration, run:

make migrate-down

Migration files are located in the database/migrations/ directory.

  • Up Migration: Files ending in .up.sql are used for applying changes.
  • Down Migration: Files ending in .down.sql are used for rolling back changes.

Scheduled Tasks (Cron Jobs)

The project uses cron jobs to perform scheduled tasks such as database cleanups or other recurring operations.

Running Cron Jobs

Cron jobs are defined separately from the server and run independently. To start the cron job scheduler, run:

make run-cron

This will start the cron scheduler and execute tasks based on their defined schedule.

Adding a New Cron Job

  1. Define the task in the schedules/tasks.go. For example:
func NewTask(db *sql.DB) {
  log.Println("Running a new task...")
  // Perform the task here
}
  1. Register the task in the cmd/schedules/main.go with the cron expression:
_, err := c.AddFunc("0 0 * * *", func() {
  tasks.NewTask(db)
})

Example Cron Expression:

  • * * * * *: Every minute
  • 0 0 0 * * *: Runs at midnight every day

Validators

Validators ensure that incoming data (such as user input) meets the necessary requirements before it is processed by the server. The project uses go-playground/validator to handle validation.

Validators are defined in the validators/ directory.

RegisterUserValidator

This validator is used when creating a new user (e.g., during registration). It ensures that the username, email, and password fields meet certain criteria.

Location: validators/auth_validator.go

type RegisterUserValidator struct {
	Username string `json:"username" binding:"required,min=3,max=50" message:"Username is required with minimum of 3 characters and maximum of 50 characters"`
	Email    string `json:"email" binding:"required,email" message:"Email is required and must be a valid email address"`
	Password string `json:"password" binding:"required,min=6" message:"Password is required with minimum of 6 characters"`
}
  • Username: Required, with a minimum length of 3 and a maximum of 50 characters.
  • Email: Required, must be a valid email format.
  • Password: Required, with a minimum length of 6 characters.

LoginUserValidator

This validator is used when logging in a user. It ensures that the email and password fields are provided.

Location: validators/auth_validator.go

type LoginUserValidator struct {
  Email    string `json:"email" binding:"required,email" message:"Email is required and must be a valid email address"`
  Password string `json:"password" binding:"required" message:"Password is required"`
}
  • Email: Required, must be a valid email format.
  • Password: Required.

Custom Error Messages

Custom error messages for validators are set in the validators structs using the message tag. If validation fails, custom messages will be returned in the response.

For example:

Username string `json:"username" binding:"required,min=3,max=50" message:"Username is required with minimum of 3 characters and maximum of 50 characters"`

In case a custom message is not provided, default validation messages are returned by the validation middleware.

Auto-Generated Code

Certain files, such as validators, can be dynamically registered using the go generate command. Auto-generated code is stored in files like validators/auto_generated.go.

Using go generate

To generate the auto-generated code, run:

make generate

This command ensures that all validator structs in the validators packages are automatically registered.

Configuration

air.toml (Development Mode)

The air.toml file is used for configuring the Air live-reloading tool. It watches specific directories and file types, such as .go and .html, to automatically rebuild and restart the server during development.

You can modify air.toml to suit your development workflow, for example:

[build]
  cmd = "go build -o ./tmp/main ."
  bin = "./tmp/main"
  delay = 1000
  tmp_dir = "tmp"

[watch]
  includes = ["./controllers", "./routes", "./services", "./validators", "./config"]
  include_ext = ["go", "html", "tmpl", "tpl"]
  exclude_dir = ["vendor", "tmp", "database/migrations"]

[log]
  level = "info"

Development Workflow

The project includes a development workflow that simplifies the process of building and running the server in development mode.

Live Reloading with Air

The project uses Air for live-reloading during development. This means that the server automatically restarts whenever changes are made to the codebase.

To start the server in development mode with live-reloading:

make develop

Contributing

If you would like to contribute to the project:

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

All contributions are welcome!

License

This project is open-source and available under the MIT License.

About

A simple API server built with Golang using the Gin Framework and supports live reloading during development using Air, Validators Auto-Registration and has an integrated migration system.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published