An API boilerplate written in Golang with Gin Framework
Write restful API with fast development and developer friendly
- Manage from config.yml file
- Use Gin Web Framework
- Server
environment
is Gin debug logger, useprod
in production anddev
in development mode
server:
host: "0.0.0.0"
port: "8000"
secret: "secret"
environment: "dev" #debug logger ,use `prod` in production
request:
timeout: 100
- Use GORM as an ORM. you just need to configure config.yml file according to your setup.
- Use database
host
aslocalhost
for local development, if docker usepostgres_db
- Database
log_mode
is SQL logger,false
in production andtrue
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
Follow these steps:
- Make sure install the latest version of docker and docker-compose
- Installation instruction for your desire OS https://docs.docker.com/engine/install/ubuntu/
- Install docker composer https://docs.docker.com/compose/install/
- Run
make dev
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
orgo build main.go
and run./main
- The application available and check health on 0.0.0.0:8000/health
- Use logrus - Structured, pluggable logging for Go.
INFO 2022-03-12T00:33:32+03:00 Server is starting at 127.0.0.1:8000
- Use Gin CORSMiddleware
router := gin.New()
router.Use(gin.Logger())
router.Use(gin.Recovery())
router.Use(middleware.CORSMiddleware())
├── 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
- 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)
make dev
: make dev for development workmake build
: make build containermake production
: docker production build and upclean
: clean for all clear docker images
- Run
make build
- Run
make production
- Example contains sample code of different type of example