Giter Site home page Giter Site logo

go-api-template's Introduction

Go REST API Boilerplate

Golang API boilerplate using GoFiber and PostgreSQL

Folder Structure

  • /api/v1

    • /routes - All API routes are defined here
    • /controllers - For validating requests and calling services
    • /services - For business logic, database calls and other services
    • /middlewares - For authentication, logging, rate limiting etc.
  • /build - Contains built binary, gitignore'd

  • /cmd - Initializes the fiber app and basic middlewares configuration

  • /config - For handling configuration/env variables

  • /db - For handling database connections

  • /handlers - For handling responses and db transactions

  • /models - Auto generated models from database tables using sqlboiler

  • /secure - Contains SSL certificates, gitignore'd

  • /types - For defining custom types that can be used across the app

  • /utils - For utility functions

  • main.go - Entrypoint of the app

Pro-tip

  • Get beautiful folder by using Material Icon Theme in VSCode

How to use

  1. The repo contains product API implementation for reference.

  2. Clone the repo and rename the folder to your project name.

  3. Search for atharvbhadange/go-api-template in the project and replace it with <your-github-id/project-name>.

  4. Run go install github.com/volatiletech/sqlboiler/v4/drivers/sqlboiler-psql@latest to install sqlboiler for generating models.

  5. Change the models/sqlboiler.toml file to match your database configuration.

  6. Under models folder, run sqlboiler psql to generate models from database tables, this will create a models folder.

  7. Copy the contents of models/models folder to models folder in the root of the project.

  8. Run go mod tidy to install all the dependencies.

  9. Copy .env.example to .env and change the values as per your configuration.

  10. Run go build -o ./build/main && ./build/main to build and run the app.

Optional

  1. Run go get github.com/cosmtrek/air to install air for hot reloading.

  2. Run air to start the app with hot reloading.

Notes

  • Success Handler for successful requests

  • BuildError Handler for build errors

  • Start new PGX trx from controllers only

  • /api/v1 is the base path for all routes except / for health check

  • /models can live as a separate repo and can be imported as a git submodule

  • To run the sample product API implementation, create a table called products using the following query:

CREATE TABLE IF NOT EXISTS products (
  id SERIAL PRIMARY KEY,
  name varchar(255) NOT NULL,
  description text NOT NULL,
  price int NOT NULL,
  created timestamp NOT NULL,
  modified timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
);

go-api-template's People

Contributors

atharv-bhadange avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.