Giter Site home page Giter Site logo

gin-rest-starter's Introduction

Gin REST Starter

Minimum Gin starter template for REST API.

This template uses

  • GORM for the ORM.
  • swag to auto generate Swagger Documentation

By default it uses Postgres connection. If you are using different Database, you can easily change the dsn, check GORM: Connecting to a Database

Getting started

This template uses air with hot reload feature in development environment. Read the installation guide here.

  1. Copy .env.example to .env and modify the env value as needed
  2. go mod download to install all required dependencies
  3. air .air.toml: start a dev server with hot reload (auto updates swagger)
  4. go test -v ./...: run all tests on all packages

Note: set GO_ENV to production to run Gin app in release mode

To access swagger documentation, open http://localhost:3000/swagger/index.html on your browser. Read more about swag's comment annotation here

Route example:

  • GET http://localhost:3000/api/compA: "Hello World"
  • GET http://localhost:3000/api/compA/3/5:
{
  "result": 8
}

Directory Structure

.
├── compA
│   ├── models.go
│   ├── router.go
│   ├── service.go
│   └── service_test.go
├── compB
│   └── ...
├── main.go
├── go.mod
└── ...

This template separate your code by your app's components, for example for a social media app, you will have something like this:

.
├── posts
│   └── ...
├── users
│   └── ...
├── main.go
├── go.mod
└── ...

Each component will usually contains 4 files:

  • models.go: contains the component's GORM Models (notice that it's models.go, not model.go it's because one component can have more than one model, for example for posts component, you can have Post model and Comment model, though you can also separate comments into it's own component folder)
  • router.go: contains api routes and it's controller
  • service.go: contains the business logic of the component (avoid putting business logic directly in router to make testing easier)
  • service_test.go: contains tests for your service.go

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.