Skip to content

Simple RESTful API boilerplate built using Go programming language and Fiber framework with many key features

Notifications You must be signed in to change notification settings

ahmadammarm/go-rest-api-template

Repository files navigation

Go REST API Template

A RESTful API template built with Go programming language.

Go Postgres JWT GitHub Actions Docker

Prequisites

  • Go 1.24 or later
  • PostgreSQL 17 or later
  • Docker (optional)

Features

  • Unit Testing: Implement the unit testing in order to code maintenance.
  • Middleware: Implement the JWT Auth for the middleware.
  • Service Layer: Implement the service layer for separating business logic from application logic.
  • Repository Pattern: Managing interactions with databases using the repository pattern.
  • CRUD Implementation: Implement CRUD logic in the model entity.
  • Configuration: Supports environment variables configuration via .env files.
  • CORS Support: Supports Cross-Origin Resource Sharing (CORS) configuration.
  • Deployment: Supports build and deploy using Docker.

REST API Design

The project provides a RESTful API for posts. The API follows standard REST conventions:

Users API Routes

  • GET /api/v1/users - Get all users.
  • GET /api/v1/user/:id - Get an user by id.
  • POST /api/v1/user/register - Register an user.
  • POST /api/v1/user/login - Login for the registered user.

News API Routes

  • GET /api/v1/news - Get all news.
  • GET /api/v1/news/:id - Get a news by id.
  • POST /api/v1/news - Create a news.
  • PUT /api/v1/news/:id - Edit a news by id.
  • DELETE /api/v1/news/:id - Delete a news by id.

Getting Started

  1. Clone the repository:
git clone https://github.com/ahmadammarm/go-rest-api-template.git
  1. Navigate to the project directory:
cd go-rest-api-template
  1. Install the project dependencies:
go mod download
  1. Configure Environment Variable: Copy the file .env.example to .env and adjust it to your configuration:
cp .env.example .env
POSTGRES_HOST=localhost
POSTGRES_PORT=
POSTGRES_USER=
POSTGRES_PASSWORD=
POSTGRES_DB=
JWT_SECRET_KEY=your-jwt-secret-key
CORS_ALLOW_ORIGINS=your-header-http-domain
  • POSTGRES: Configuration for PostgreSQL database connection.

  • JWT_SECRET_KEY: The secret key used for JWT token signing and verification.

  • CORS_ALLOW_ORIGINS: The allowed origins for Cross-Origin Resource Sharing (CORS). This is the domain that will be able to access resources from this API. For example, if you are running the frontend on http://localhost:5173, you should set this environment variable to http://localhost:5173.

  1. Run the project:
go run cmd/main.go

Getting Started with Docker

  1. Clone the repository:
git clone https://github.com/ahmadammarm/go-rest-api-template.git
  1. Navigate to the project directory:
cd go-rest-api-template
  1. Run the Docker Compose:
docker-compose up

or run in detach mode:

docker-compose up -d
  1. To stop the Docker Compose:
docker-compose down

The project will be available at:

http://localhost:8080

This project also available for your contributions, thank you :)

About

Simple RESTful API boilerplate built using Go programming language and Fiber framework with many key features

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published