Home

Awesome

Gin-Admin

基于 Golang + Gin + GORM 2.0 + Casbin 2.0 + Wire DI 的轻量级、灵活、优雅且功能齐全的 RBAC 脚手架。

English | 中文

LICENSE Language Go Report Card GitHub release GitHub release date GoDoc

功能特性

demo swagger

前端项目

安装依赖工具

快速开始

创建新的项目

可通过 gin-admin-cli help new 查看命令的详细说明

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

启动服务

通过更改 configs/dev/server.toml 配置文件中的 MenuFile = "menu_cn.json" 可以切换到中文菜单

cd ~/go/src/testapp

make start
# or
go run main.go start

编译服务

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

生成 Docker 镜像

docker build -f ./Dockerfile -t testapp:v1.0.0 .

代码生成

可通过 gin-admin-cli help gen 查看命令的详细说明

准备配置文件 dictionary.yaml

- name: Dictionary
  comment: 字典管理
  disable_pagination: true
  fill_gorm_commit: true
  fill_router_prefix: true
  tpl_type: "tree"
  fields:
    - name: Code
      type: string
      comment: Code of dictionary (unique for same parent)
      gorm_tag: "size:32;"
      form:
        binding_tag: "required,max=32"
    - name: Name
      type: string
      comment: Display name of dictionary
      gorm_tag: "size:128;index"
      query:
        name: LikeName
        in_query: true
        form_tag: name
        op: LIKE
      form:
        binding_tag: "required,max=128"
    - name: Description
      type: string
      comment: Details about dictionary
      gorm_tag: "size:1024"
      form: {}
    - name: Sequence
      type: int
      comment: Sequence for sorting
      gorm_tag: "index;"
      order: DESC
      form: {}
    - name: Status
      type: string
      comment: Status of dictionary (disabled, enabled)
      gorm_tag: "size:20;index"
      query: {}
      form:
        binding_tag: "required,oneof=disabled enabled"
gin-admin-cli gen -d . -m SYS -c dictionary.yaml

删除模块

可通过 gin-admin-cli help remove 查看命令的详细说明

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

生成 Swagger 文档

通过 Swag 可以自动生成 Swagger 文档

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

生成依赖注入代码

依赖注入本身的作用是解决了各个模块间层级依赖繁琐的初始化过程,通过 Wire 可以自动生成依赖注入代码,简化依赖注入的过程。

make wire
# or
wire gen ./internal/wirex

项目结构概览

├── cmd                             (命令行定义目录)
│   ├── start.go                    (启动命令)
│   ├── stop.go                     (停止命令)
│   └── version.go                  (版本命令)
├── configs
│   ├── dev
│   │   ├── logging.toml            (日志配置文件)
│   │   ├── middleware.toml         (中间件配置文件)
│   │   └── server.toml             (服务配置文件)
│   ├── menu.json                   (初始化菜单文件)
│   └── rbac_model.conf             (Casbin RBAC 模型配置文件)
├── internal
│   ├── bootstrap                   (初始化目录)
│   │   ├── bootstrap.go            (初始化)
│   │   ├── http.go                 (HTTP 服务)
│   │   └── logger.go               (日志服务)
│   ├── config                      (配置文件目录)
│   │   ├── config.go               (配置文件初始化)
│   │   ├── consts.go               (常量定义)
│   │   ├── middleware.go           (中间件配置)
│   │   └── parse.go                (配置文件解析)
│   ├── mods
│   │   ├── rbac                    (RBAC 模块)
│   │   │   ├── api                 (API层)
│   │   │   ├── biz                 (业务逻辑层)
│   │   │   ├── dal                 (数据访问层)
│   │   │   ├── schema              (数据模型层)
│   │   │   ├── casbin.go           (Casbin 初始化)
│   │   │   ├── main.go             (RBAC 模块入口)
│   │   │   └── wire.go             (RBAC 依赖注入初始化)
│   │   └── mods.go
│   ├── utility
│   │   └── prom
│   │       └── prom.go             (Prometheus 监控,用于集成 prometheus)
│   └── wirex                       (依赖注入目录,包含了依赖组的定义和初始化)
│       ├── injector.go
│       ├── wire.go
│       └── wire_gen.go
├── pkg                             (公共包目录)
│   ├── cachex                      (缓存包)
│   ├── crypto                      (加密包)
│   │   ├── aes                     (AES加密)
│   │   ├── hash                    (哈希加密)
│   │   └── rand                    (随机数)
│   ├── encoding                    (编码包)
│   │   ├── json                    (JSON编码)
│   │   ├── toml                    (TOML编码)
│   │   └── yaml                    (YAML编码)
│   ├── errors                      (错误处理包)
│   ├── gormx                       (Gorm扩展包)
│   ├── jwtx                        (JWT包)
│   ├── logging                     (日志包)
│   ├── mail                        (邮件包)
│   ├── middleware                  (中间件包)
│   ├── oss                         (对象存储包)
│   ├── promx                       (Prometheus包)
│   └── util                        (工具包)
├── test                            (单元测试目录)
│   ├── menu_test.go
│   ├── role_test.go
│   ├── test.go
│   └── user_test.go
├── Dockerfile
├── Makefile
├── README.md
├── go.mod
├── go.sum
└── main.go                         (入口文件)

合作交流 :beers:

微信

扫码加微信群

wechat

QQ

qq

License

Copyright (c) 2024 Lyric

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.