{"id":15095989,"url":"https://github.com/akmamun/gin-boilerplate","last_synced_at":"2026-02-26T04:38:24.894Z","repository":{"id":37242013,"uuid":"466505016","full_name":"akmamun/gin-boilerplate","owner":"akmamun","description":"Golang API Boilerplate Template using gin, gorm, logrus","archived":false,"fork":false,"pushed_at":"2024-02-05T06:53:58.000Z","size":378,"stargazers_count":97,"open_issues_count":0,"forks_count":19,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-14T16:14:21.540Z","etag":null,"topics":["boilerplate","dockerized","gin","gin-gonic","golang","gorm","hot-reload","live-reload","logrus","microservice","mvc-architecture","postgresql","production-ready","rest-api","scaffold"],"latest_commit_sha":null,"homepage":"https://github.com/akmamun/gin-boilerplate-examples","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/akmamun.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-03-05T16:20:15.000Z","updated_at":"2025-03-28T14:45:54.000Z","dependencies_parsed_at":"2023-07-13T00:08:48.615Z","dependency_job_id":"9ccfe44b-5bc9-4787-b97b-37b76ffd3821","html_url":"https://github.com/akmamun/gin-boilerplate","commit_stats":null,"previous_names":["akmamun/gin-gorm-boilerplate"],"tags_count":5,"template":true,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akmamun%2Fgin-boilerplate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akmamun%2Fgin-boilerplate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akmamun%2Fgin-boilerplate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akmamun%2Fgin-boilerplate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/akmamun","download_url":"https://codeload.github.com/akmamun/gin-boilerplate/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248914115,"owners_count":21182359,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["boilerplate","dockerized","gin","gin-gonic","golang","gorm","hot-reload","live-reload","logrus","microservice","mvc-architecture","postgresql","production-ready","rest-api","scaffold"],"created_at":"2024-09-25T15:44:34.999Z","updated_at":"2026-02-26T04:38:19.843Z","avatar_url":"https://github.com/akmamun.png","language":"Go","funding_links":["https://www.buymeacoffee.com/akmamun"],"categories":[],"sub_categories":[],"readme":"# Go Boilerplate\nAn API boilerplate written in Golang with Gin Framework and Gorm \n\n\u003cdiv style=\"display: flex; justify-content: center;\"\u003e\n    \u003ca href=\"https://www.buymeacoffee.com/akmamun\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png\" alt=\"Buy Me A Coffee\" style=\"height: 46px !important;width: 179px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;\"\u003e\n    \u003c/a\u003e\n\u003c/div\u003e\n\n### Motivation\nWrite restful API with fast development and developer friendly\n\n## Table of Contents\n- [Motivation](#motivation)\n- [Configuration Manage](#configuration-manage)\n  - [ENV Manage](#env-manage)\n  - [Server Configuration](#server-configuration)\n  - [Database Configuration](#database-configuration)\n  - [PgAdmin](#pg-admin)\n- [Installation](#installation)\n  - [Local Setup Instruction](#local-setup-instruction)\n  - [Develop Application in Docker with Live Reload](#develop-application-in-docker-with-live-reload)\n- [Middlewares](#middlewares)\n- [Boilerplate Structure](#boilerplate-structure)\n- [Code Examples](#examples)\n- [Let's Build an API](#lets-build-an-api)\n- [Deployment](#deployment)\n  - [Container Development Build](#container-development-build)\n  - [Container Production Build and Up](#container-production-build-and-up)\n- [Useful Commands](#useful-commands)\n- [ENV YAML Configure](#env-yaml-configure)\n- [Use Packages](#use-packages)\n\n### Configuration Manage\n#### ENV Manage\n\n- Default ENV Configuration Manage from `.env`. sample file `.env.example`\n```text\n# Server Configuration\nSECRET=h9wt*pasj6796j##w(w8=xaje8tpi6h*r\u0026hzgrz065u\u0026ed+k2)\nDEBUG=True # `False` in Production\nALLOWED_HOSTS=0.0.0.0\nSERVER_HOST=0.0.0.0\nSERVER_PORT=8000\n\n# Database Configuration\nMASTER_DB_NAME=test_pg_go\nMASTER_DB_USER=mamun\nMASTER_DB_PASSWORD=123\nMASTER_DB_HOST=postgres_db\nMASTER_DB_PORT=5432\nMASTER_DB_LOG_MODE=True # `False` in Production\nMASTER_SSL_MODE=disable\n\nREPLICA_DB_NAME=test_pg_go\nREPLICA_DB_USER=mamun\nREPLICA_DB_PASSWORD=123\nREPLICA_DB_HOST=localhost\nREPLICA_DB_PORT=5432\nREPLICA_DB_LOG_MODE=True # `False` in Production\nREPLICA_SSL_MODE=disable\n```\n- Server `DEBUG` set `False` in Production\n- Database Logger `MASTER_DB_LOG_MODE` and `REPLICA_DB_LOG_MODE`  set `False` in production\n- If ENV Manage from YAML file add a config.yml file and configuration [db.go](config/db.go) and [server.go](config/server.go). See More [ENV YAML Configure](#env-yaml-configure)\n\n#### Server Configuration\n- Use [Gin](https://github.com/gin-gonic/gin) Web Framework\n\n#### Database Configuration\n- Use [GORM](https://github.com/go-gorm/gorm) as an ORM\n- Use database `MASTER_DB_HOST` value set as `localhost` for local development, and use `postgres_db` for docker development \n#### PG Admin\n- Check  PG Admin on [http://0.0.0.0:5050/browser/](http://0.0.0.0:5050/browser/)\n- Login with Credential Email `admin@admin.com` Password `root`\n- Connect Database Host as `postgres_db`, DB Username and Password as per `.env` set\n- Note: if not configure `.env`, default Username `mamun` and password `123`\n\n### Installation\n#### Local Setup Instruction\nFollow these steps:\n- Copy [.env.example](.env.example) as `.env` and configure necessary values\n- To add all dependencies for a package in your module `go get .` in the current directory\n- Locally run `go run main.go` or `go build main.go` and run `./main`\n- Check Application health available on [0.0.0.0:8000/health](http://0.0.0.0:8000/health)\n\n#### Develop Application in Docker with Live Reload\nFollow these steps:\n- Make sure install the latest version of docker and docker-compose\n- Docker Installation for your desire OS https://docs.docker.com/engine/install/ubuntu/\n- Docker Composer Installation https://docs.docker.com/compose/install/\n- Run and Develop `make dev`\n- Check Application health available on [0.0.0.0:8000/health](http://0.0.0.0:8000/health)\n\n### Middlewares\n- Use Gin CORSMiddleware\n```go\nrouter := gin.New()\nrouter.Use(gin.Logger())\nrouter.Use(gin.Recovery())\nrouter.Use(middleware.CORSMiddleware())\n```\n\n### Boilerplate Structure\n\u003cpre\u003e├── \u003cfont color=\"#3465A4\"\u003e\u003cb\u003econfig\u003c/b\u003e\u003c/font\u003e\n│   ├── config.go\n│   ├── db.go\n│   └── server.go\n├── \u003cfont color=\"#3465A4\"\u003e\u003cb\u003econtrollers\u003c/b\u003e\u003c/font\u003e\n│   └── example_controller.go\n├── docker-compose-dev.yml\n├── docker-compose-prod.yml\n├── Dockerfile\n├── Dockerfile-dev\n├── go.mod\n├── go.sum\n├── \u003cfont color=\"#3465A4\"\u003e\u003cb\u003ehelpers\u003c/b\u003e\u003c/font\u003e\n│   ├── response.go\n│   └── search.go\n├── \u003cfont color=\"#3465A4\"\u003e\u003cb\u003einfra\u003c/b\u003e\u003c/font\u003e\n│   ├── \u003cfont color=\"#3465A4\"\u003e\u003cb\u003edatabase\u003c/b\u003e\u003c/font\u003e\n│   │   └── database.go\n│   └── \u003cfont color=\"#3465A4\"\u003e\u003cb\u003elogger\u003c/b\u003e\u003c/font\u003e\n│       └── logger.go\n├── LICENSE\n├── main.go\n├── Makefile\n├── \u003cfont color=\"#3465A4\"\u003e\u003cb\u003emigrations\u003c/b\u003e\u003c/font\u003e\n│   └── migration.go\n├── \u003cfont color=\"#3465A4\"\u003e\u003cb\u003emodels\u003c/b\u003e\u003c/font\u003e\n│   └── example_model.go\n├── README.md\n├── \u003cfont color=\"#3465A4\"\u003e\u003cb\u003erepository\u003c/b\u003e\u003c/font\u003e\n│   └── sql_repo.go\n├── \u003cfont color=\"#3465A4\"\u003e\u003cb\u003erouters\u003c/b\u003e\u003c/font\u003e\n│   ├── index.go\n│   ├── \u003cfont color=\"#3465A4\"\u003e\u003cb\u003emiddleware\u003c/b\u003e\u003c/font\u003e\n│   │   └── cors.go\n│   └── router.go\n\u003c/pre\u003e\n\n### Examples\n- More Example [gin-boilerplate-examples](https://github.com/akmamun/gin-boilerplate-examples)\n\n### Let's Build an API\n\n1. [models](models) folder add a new file name `example_model.go`\n\n```go\npackage models\n\nimport (\n\t\"time\"\n)\n\ntype Example struct {\n\tId        int        `json:\"id\"`\n\tData      string     `json:\"data\" binding:\"required\"`\n\tCreatedAt *time.Time `json:\"created_at,string,omitempty\"`\n\tUpdatedAt *time.Time `json:\"updated_at_at,string,omitempty\"`\n}\n// TableName is Database Table Name of this model\nfunc (e *Example) TableName() string {\n\treturn \"examples\"\n}\n```\n2. Add Model to [migration](pkg/database/migration.go)\n```go\npackage migrations\n\nimport (\n\t\"gin-boilerplate/infra/database\"\n\t\"gin-boilerplate/models\"\n)\n\n// Migrate Add list of model add for migrations\nfunc Migrate() {\n\tvar migrationModels = []interface{}{\u0026models.Example{}}\n\terr := database.DB.AutoMigrate(migrationModels...)\n\tif err != nil {\n\t\treturn\n\t}\n}\n\n```\n3. [controller](controllers) folder add a file `example_controller.go`\n- Create API Endpoint \n- Write Database Operation in Repository and use them from controller\n```go\npackage controllers\n\nimport (\n  \"gin-boilerplate/models\"\n  \"gin-boilerplate/repository\"\n  \"github.com/gin-gonic/gin\"\n  \"net/http\"\n)\n\nfunc GetData(ctx *gin.Context) {\n  var example []*models.Example\n  repository.Get(\u0026example)\n  ctx.JSON(http.StatusOK, \u0026example)\n\n}\nfunc Create(ctx *gin.Context) {\n  example := new(models.Example)\n  repository.Save(\u0026example)\n  ctx.JSON(http.StatusOK, \u0026example)\n}\n```\n4. [routers](routers) folder add a file `example.go`\n```go\npackage routers\n\nimport (\n  \"gin-boilerplate/controllers\"\n  \"github.com/gin-gonic/gin\"\n  \"net/http\"\n)\n\nfunc RegisterRoutes(route *gin.Engine) {\n  route.GET(\"/health\", func(ctx *gin.Context) { ctx.JSON(http.StatusOK, gin.H{\"live\": \"ok\"}) })\n  //added new\n  route.GET(\"/v1/example/\", controllers.GetData)\n  route.POST(\"/v1/example/\", controllers.Create)\n\n  //Add All route\n  //TestRoutes(route)\n}\n```\n- Congratulation, your new endpoint `0.0.0.0:8000/v1/example/`\n\n### Deployment\n#### Container Development Build\n- Run `make build`\n\n#### Container Production Build and Up\n- Run `make production`\n\n#### ENV Yaml Configure\n```yaml\ndatabase:\n  driver: \"postgres\"\n  dbname: \"test_pg_go\"\n  username: \"mamun\"\n  password: \"123\"\n  host: \"postgres_db\" # use `localhost` for local development\n  port: \"5432\"\n  ssl_mode: disable\n  log_mode: false\n\nserver:\n  host: \"0.0.0.0\"\n  port: \"8000\"\n  secret: \"secret\"\n  allow_hosts: \"localhost\"\n  debug: false #use `false` in production\n  request:\n    timeout: 100\n```\n- [Server Config](config/server.go)\n```go\nfunc ServerConfig() string {\nviper.SetDefault(\"server.host\", \"0.0.0.0\")\nviper.SetDefault(\"server.port\", \"8000\")\nappServer := fmt.Sprintf(\"%s:%s\", viper.GetString(\"server.host\"), viper.GetString(\"server.port\"))\nreturn appServer\n}\n```\n- [DB Config](config/db.go)\n```go\nfunc DbConfiguration() string {\n\t\ndbname := viper.GetString(\"database.dbname\")\nusername := viper.GetString(\"database.username\")\npassword := viper.GetString(\"database.password\")\nhost := viper.GetString(\"database.host\")\nport := viper.GetString(\"database.port\")\nsslMode := viper.GetString(\"database.ssl_mode\")\n\ndsn := fmt.Sprintf(\"host=%s user=%s password=%s dbname=%s port=%s sslmode=%s\",\nhost, username, password, dbname, port, sslMode)\nreturn dsn\n}\n```\n\n### Useful Commands\n\n- `make dev`: make dev for development work\n- `make build`: make build container\n- `make production`: docker production build and up\n- `clean`: clean for all clear docker images\n\n### Use Packages\n- [Viper](https://github.com/spf13/viper) - Go configuration with fangs.\n- [Gorm](https://github.com/go-gorm/gorm) - The fantastic ORM library for Golang\n- [Logger](https://github.com/sirupsen/logrus) - Structured, pluggable logging for Go.\n- [Air](https://github.com/cosmtrek/air) - Live reload for Go apps (Docker Development)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakmamun%2Fgin-boilerplate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fakmamun%2Fgin-boilerplate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakmamun%2Fgin-boilerplate/lists"}