Skip to content

martimmpr/go-api-template

Repository files navigation

Go API Template

This repository is a basic template for creating APIs in Go. It includes support for:

  • Docker: To create containers and manage dependencies.
  • Nginx: Configured as a reverse proxy for the API.
  • Gin: A web framework for building APIs in Go.
  • GORM: An ORM library for interacting with the database.
  • Insomnia: Pre-configured file with requests to facilitate testing.

Main Features

  • Initial setup for APIs in Go.
  • Support for JWT authentication.
  • Pre-configured CORS policies.
  • Rate limiting for request control.
  • PostgreSQL support (or other databases, depending on the setup).

Getting Started

  1. Clone the repository:

    git clone https://github.com/martimmpr/go-api-template.git
    cd go-api-template
  2. Set up the environment variables:

    • On Linux:
      cp .env.example .env
    • On Windows (PowerShell):
      Copy-Item .env.example .env
  3. Start the containers using Docker Compose:

    docker compose up --build
  4. Test the API endpoints using the insomnia.json file:

    • Import the file into Insomnia and execute the requests directly.
  5. Access the API: The server will be available at http://localhost:8080.

Important Commands

Docker Compose

Command Action Mode
docker compose up Starts the containers and shows logs. Foreground
docker compose up -d Starts the containers without showing logs. Background
docker compose up --build Rebuilds images and starts the containers (showing logs). Foreground
docker compose up --build -d Rebuilds images and starts the containers (without showing logs). Background
docker compose down Stops and removes containers, networks, and anonymous volumes. N/A
docker compose down -v Stops and removes containers, networks, and all volumes. N/A

Golang

Command Description
go mod init [FOLDER] Initializes the project and creates go.mod and go.sum.
go mod tidy Updates go.mod with the used packages.

Insomnia File

The provided insomnia.json file contains a collection of pre-configured requests to test the API endpoints. To use it:

  1. Open Insomnia.
  2. Go to File > Import > From File.
  3. Select the insomnia.json file located in this repository.
  4. Use the requests to quickly test the API.

Technologies Used

  • Go: For building the API.
  • Gin: Web framework for building APIs.
  • GORM: ORM for interacting with databases.
  • Docker: For containerization and isolated development environments.
  • Nginx: Reverse proxy for the API.
  • Insomnia: For easy API testing.
  • JWT: For secure authentication.
  • PostgreSQL: Relational database (can be replaced).

Contributing

Contributions, issues, and feature requests are welcome! Feel free to check the issues page.

License

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

About

Basic starter template for a monolithic Go backend

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors