Skip to content

justyork/template-api

Repository files navigation

API Template

A lightweight and scalable REST API server built with GoLang.

Overview

This project is a template for building RESTful APIs using Go. It includes features such as JWT authentication, database migrations, and Swagger documentation.

Features

  • 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.

Project Structure

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.

Getting Started

Prerequisites

  • Go 1.23 or later
  • Docker (optional, for containerization)
  • SQLite (for local development)

Installation

  1. Clone the repository:

    git clone https://github.com/justyork/api-template.git
    cd api-template
  2. Copy the example environment file:

    cp .env.example .env
  3. Update .env with your configuration.

  4. Run the application:

    go run cmd/main.go

Running with Docker

  1. Build the Docker image:

    docker build -t api-template .
  2. Run the Docker container:

    docker-compose up

Running Tests

Execute the following command to run tests:

go test ./... -v

API Documentation

Access the Swagger UI at http://localhost:8080/swagger/ to view the API documentation.

Demo

Explore the live demo of the deployed project and its Swagger documentation.

CI/CD Pipeline

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

License

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

Contact

For support, contact yorkshp@gmail.com.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors