Skip to content

Golang Boilerplate using gin, gorm, logrus

License

Notifications You must be signed in to change notification settings

phattranky/gin-boilerplate

Repository files navigation

Go Boilerplate

An API boilerplate written in Golang with Gin Framework

Motivation

Write restful API with fast development and developer friendly

Configuration Manage

  • Manage from config.yml file
  • Use Gin Web Framework
  • Server environment is Gin debug logger, use prod in production and dev in development mode

Server Configuration

server:
  host: "0.0.0.0"
  port: "8000"
  secret: "secret"
  environment: "dev" #debug logger ,use `prod` in production
  request:
    timeout: 100

Database Configuration

  • Use GORM as an ORM. you just need to configure config.yml file according to your setup.
  • Use database host as localhost for local development, if docker use postgres_db
  • Database log_mode is SQL logger, false in production and true in development mode
database:
  driver: "postgres"
  dbname: "test_pg_go"
  username: "mamun"
  password: "123"
  host: "postgres_db" # use "localhost" for local development, `postgres_db` for docker
  port: "5432"
  log_mode: true # SQL logger , false in production

Develop Application in Docker Compose with Live Reload

Follow these steps:

Local Setup Instruction

Follow these steps:

  • Configuration manage from config.yml file
  • To add all dependencies for a package in your module go get . in the current directory
  • Locally run go run main.go or go build main.go and run ./main

Routes

Logging

  • Use logrus - Structured, pluggable logging for Go.
  • INFO 2022-03-12T00:33:32+03:00 Server is starting at 127.0.0.1:8000

Middlewares

  • Use Gin CORSMiddleware
router := gin.New()
router.Use(gin.Logger())
router.Use(gin.Recovery())
router.Use(middleware.CORSMiddleware())

Boilerplate structure

├── config.yml
├── docker-compose-dev.yml
├── docker-compose-prod.yml
├── Dockerfile
├── Dockerfile-dev
├── go.mod
├── go.sum
├── LICENSE
├── main.go
├── Makefile
├── README.md
└── src
    ├── config
    │   ├── config.go
    │   ├── db.go
    │   └── server.go
    ├── controllers
    │   ├── controller.go
    │   └── example_controller.go
    ├── database
    │   ├── database.go
    │   └── migration.go
    ├── helpers
    │   ├── pagination
    │   │   └── pagination.go
    ├── logger
    │   └── logger.go
    ├── models
    │   └── example_model.go
    ├── repository
    │   └── example_repo.go
    └── routers
        ├── example.go
        ├── index.go
        ├── middleware
        │   └── cors.go
        └── router.go

Use Packages

  • Viper - Go configuration with fangs.
  • Gorm - The fantastic ORM library for Golang
  • Logger - Structured, pluggable logging for Go.
  • Air - Live reload for Go apps (Docker Development)

Useful Commands

  • make dev: make dev for development work
  • make build: make build container
  • make production: docker production build and up
  • clean: clean for all clear docker images

Container Development Build

  • Run make build

Container Production Build and Up

  • Run make production

Code Examples

  • Example contains sample code of different type of example

About

Golang Boilerplate using gin, gorm, logrus

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 89.7%
  • Dockerfile 5.4%
  • Makefile 4.9%