A lightweight, flexible, elegant and full-featured RBAC scaffolding based on GIN + GORM 2.0 + Casbin 2.0 + Wire DI.
English | δΈζ
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
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 -s Article --structs-comment 'Article management'
Remove a module
gin-admin-cli rm -d . -m CMS -s 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)