/gin-admin

A lightweight, flexible, elegant and full-featured RBAC scaffolding based on GIN + GORM 2.0 + Casbin 2.0 + Wire DI.

Primary LanguageGoApache License 2.0Apache-2.0

Gin-Admin

A lightweight, flexible, elegant and full-featured RBAC scaffolding based on GIN + GORM 2.0 + Casbin 2.0 + Wire DI.

English | δΈ­ζ–‡

LICENSE Language Go Report Card GitHub release GitHub release date GoDoc

Features

  • πŸ“œ Follow the RESTful API design specification & interface-based programming specification
  • 🏠 More concise project structure, modular design, improve code readability and maintainability
  • 🧰 Based on the GIN framework, it provides rich middleware support (JWTAuth, CORS, RequestLogger, RequestRateLimiter, TraceID, Casbin, Recover, GZIP, StaticWebsite)
  • πŸ” RBAC access control model based on Casbin
  • πŸ—ƒοΈ Database access layer based on GORM 2.0
  • πŸ”Œ Dependency injection based on WIRE -- the role of dependency injection itself is to solve the cumbersome initialization process of hierarchical dependencies between modules
  • ⚑ Log output based on Zap & Context, and unified output of key fields such as TraceID/UserID through combination with Context (also supports log hooks written to GORM)
  • πŸ”‘ User authentication based on JWT
  • πŸ”¬ Automatically generate Swagger documentation based on Swaggo - Preview
  • πŸ§ͺ Implement API unit testing based on testify
  • πŸ’― Stateless service, horizontally scalable, improves service availability - dynamic permission management of Casbin is implemented through scheduled tasks and Redis
  • πŸ”¨ Complete efficiency tools, can develop complete code modules through configuration - gin-admin-cli

swagger

Frontend

Dependencies

  • Go 1.19+
  • Wire go install github.com/google/wire/cmd/wire@latest
  • Swag go install github.com/swaggo/swag/cmd/swag@latest
  • GIN-ADMIN-CLI go install github.com/gin-admin/gin-admin-cli/v10@latest

Quick Start

Create a new project

gin-admin-cli new -d ~/go/src --name testapp --desc 'A test API service based on golang.' --pkg 'github.com/xxx/testapp'

Start the service

cd ~/go/src/testapp
make start
# or
go run main.go start

Generate a new module

For more detailed usage instructions, please refer to gin-admin-cli

gin-admin-cli gen -d . -m CMS --structs Article --structs-comment 'Article management'

Remove a module

gin-admin-cli rm -d . -m CMS --structs Article

Build the service

make build
# or
go build -ldflags "-w -s -X main.VERSION=v1.0.0" -o ginadmin

Generate swagger docs

make swagger
# or
swag init --parseDependency --generalInfo ./main.go --output ./internal/swagger

Generate wire inject

make wire
# or
wire gen ./internal/wirex

Project Layout

β”œβ”€β”€ cmd
β”‚   β”œβ”€β”€ start.go
β”‚   β”œβ”€β”€ stop.go
β”‚   └── version.go
β”œβ”€β”€ configs
β”‚   β”œβ”€β”€ dev
β”‚   β”‚   β”œβ”€β”€ logging.toml           (Log configuration file)
β”‚   β”‚   β”œβ”€β”€ middleware.toml        (Middleware configuration file)
β”‚   β”‚   └── server.toml            (Service configuration file)
β”‚   β”œβ”€β”€ menu.json                  (Initialize menu file)
β”‚   └── rbac_model.conf            (Casbin RBAC model configuration file)
β”œβ”€β”€ internal
β”‚   β”œβ”€β”€ bootstrap
β”‚   β”‚   β”œβ”€β”€ bootstrap.go          (Initialization)
β”‚   β”‚   β”œβ”€β”€ http.go               (HTTP service)
β”‚   β”‚   └── logger.go             (Log service)
β”‚   β”œβ”€β”€ config                    (Configuration file)
β”‚   β”‚   β”œβ”€β”€ config.go
β”‚   β”‚   β”œβ”€β”€ consts.go
β”‚   β”‚   β”œβ”€β”€ middleware.go
β”‚   β”‚   └── parse.go
β”‚   β”œβ”€β”€ mods
β”‚   β”‚   β”œβ”€β”€ rbac                  (RBAC module)
β”‚   β”‚   β”‚   β”œβ”€β”€ api               (API layer)
β”‚   β”‚   β”‚   β”œβ”€β”€ biz               (Business logic layer)
β”‚   β”‚   β”‚   β”œβ”€β”€ dal               (Data access layer)
β”‚   β”‚   β”‚   β”œβ”€β”€ schema            (Data model layer)
β”‚   β”‚   β”‚   β”œβ”€β”€ casbin.go         (Casbin initialization)
β”‚   β”‚   β”‚   β”œβ”€β”€ main.go           (Module initialization)
β”‚   β”‚   β”‚   └── wire.go           (Dependency injection)
β”‚   β”‚   β”œβ”€β”€ sys
β”‚   β”‚   β”‚   β”œβ”€β”€ api
β”‚   β”‚   β”‚   β”œβ”€β”€ biz
β”‚   β”‚   β”‚   β”œβ”€β”€ dal
β”‚   β”‚   β”‚   β”œβ”€β”€ schema
β”‚   β”‚   β”‚   β”œβ”€β”€ main.go
β”‚   β”‚   β”‚   └── wire.go
β”‚   β”‚   └── mods.go
β”‚   β”œβ”€β”€ utility
β”‚   β”‚   └── prom
β”‚   β”‚       └── prom.go           (Prometheus)
β”‚   └── wirex                     (Dependency injection)
β”‚       β”œβ”€β”€ injector.go
β”‚       β”œβ”€β”€ wire.go
β”‚       └── wire_gen.go
β”œβ”€β”€ test                          (Unit test)
β”‚   β”œβ”€β”€ menu_test.go
β”‚   β”œβ”€β”€ role_test.go
β”‚   β”œβ”€β”€ test.go
β”‚   └── user_test.go
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ Makefile
β”œβ”€β”€ README.md
β”œβ”€β”€ go.mod
β”œβ”€β”€ go.sum
└── main.go                       (Entry)

Community