Giter Site home page Giter Site logo

hequan2017 / go-admin Goto Github PK

View Code? Open in Web Editor NEW
402.0 16.0 92.0 234 KB

go web api,包含gin+gorm+jwt+rbac等。

Home Page: https://github.com/hequan2017/go-admin/

License: MIT License

Go 98.28% Dockerfile 1.72%
go-admin go gin casbin rbac jwt token cmdb devops linux

go-admin's Introduction

Go Web Admin

版本 语言 base base

一个Go Web Api 后端 简单例子,包含 用户、权限、菜单、JWT 、 RBAC(Casbin)等!

本项目已停止维护,请仅供参考!

交流QQ群: 620176501

  • user
    • username password
  • role
    • name
  • menu
    • name path method

API 注释

http://127.0.0.1:8000/swagger/index.html

demo

目录结构

  • conf:用于存储配置文件
  • docs: 文档
    • sql执行命令
    • API注释
  • logs: 日志
  • middleware:应用中间件
    • inject 初始化对象
    • jwt
    • permission 权限验证
  • models:应用数据库模型
  • pkg:第三方包
  • routers: 路由逻辑处理
  • service: 逻辑处理
  • test: 单元测试

权限验证说明

项目启动时,会自动user  role  menu 进行自动关联!  如有更改,会删除对应的权限,重新加载!

用户  关联  角色  
角色  关联  菜单  
权限关系为:
角色(role.name,  menu.path,  menu.method)  
用户(user.username,   role.name)

例如:
运维部      /api/v1/users       GET
hequan     运维部

当hequan  GET  /api/v1/users 地址的时候,会去检查权限,因为他属于运维部 ,同时 运维部 有对应权限,所以本次请求会通过。

用户 admin 有所有的权限,不进行权限匹配

登录接口 /auth    /api/v1/userInfo 不进行验证

请求

请求和接收 都是 传递 json 格式 数据

例如:
访问 /auth    获取token
{
	"username": "admin",
	"password": "123456"
}

访问      /api/v1/menus?page=2    页面是page
请求头设置  Authorization: Token xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


访问   /api/v1/userInfo  获取用户信息
前端所需的权限 放在password字段里面,已经去重了。

"data": {
        "lists": {
            "id": 2,
            "created_on": 1550642309,
            "modified_on": 1550642309,
            "deleted_on": 0,
            "username": "hequan",
            "password": ",system,menu,create_menu,update_menu,delete_menu,user,create_user,update_user,delete_user,role,create_role,update_role,delete_role",
            "role": [
                {
                    "id": 2,
                    "created_on": 0,
                    "modified_on": 0,
                    "deleted_on": 0,
                    "name": "运维部",
                    "menu": null
                }
            ]
        }
    },

部署

支持

  • 部署 Mysql

创建一个库 go,然后导入sql,docs/sql/go.sql 创建表!

配置文件

You should modify conf/app.ini

[database]
Type = mysql
User = root
Password =
Host = 127.0.0.1:3306
Name = go
TablePrefix = go_

安装部署


yum install go -y 


export GOPROXY=https://goproxy.io
go get go-admin
cd $GOPATH/src/go-admin
go build main.go
go run  main.go 

热编译(开发时使用)

go get github.com/silenceper/gowatch

gowatch   

windows 开发 需要gcc

执行会报错 "gcc" executable file not found in %PATH% 可以参考这个 安装gcc https://blog.csdn.net/xia_2017/article/details/105545789

运行

更新角色权限关系 [[hequan 运维部]]
角色权限关系 [[hequan 运维部]]
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:	export GIN_MODE=release
 - using code:	gin.SetMode(gin.ReleaseMode)

[GIN-debug] POST   /auth                     --> go-admin/routers/api.Auth (4 handlers)
[GIN-debug] GET    /swagger/*any             --> github.com/swaggo/gin-swagger.WrapHandler.func1 (4 handlers)
[GIN-debug] GET    /api/v1/menus             --> go-admin/routers/api/v1.GetMenus (6 handlers)
[GIN-debug] GET    /api/v1/menus/:id         --> go-admin/routers/api/v1.GetMenu (6 handlers)
[GIN-debug] POST   /api/v1/menus             --> go-admin/routers/api/v1.AddMenu (6 handlers)
[GIN-debug] PUT    /api/v1/menus/:id         --> go-admin/routers/api/v1.EditMenu (6 handlers)
[GIN-debug] DELETE /api/v1/menus/:id         --> go-admin/routers/api/v1.DeleteMenu (6 handlers)
[GIN-debug] GET    /api/v1/roles             --> go-admin/routers/api/v1.GetRoles (6 handlers)
[GIN-debug] GET    /api/v1/roles/:id         --> go-admin/routers/api/v1.GetRole (6 handlers)
[GIN-debug] POST   /api/v1/roles             --> go-admin/routers/api/v1.AddRole (6 handlers)
[GIN-debug] PUT    /api/v1/roles/:id         --> go-admin/routers/api/v1.EditRole (6 handlers)
[GIN-debug] DELETE /api/v1/roles/:id         --> go-admin/routers/api/v1.DeleteRole (6 handlers)
[GIN-debug] GET    /api/v1/users             --> go-admin/routers/api.GetUsers (6 handlers)
[GIN-debug] GET    /api/v1/users/:id         --> go-admin/routers/api.GetUser (6 handlers)
[GIN-debug] POST   /api/v1/users             --> go-admin/routers/api.AddUser (6 handlers)
[GIN-debug] PUT    /api/v1/users/:id         --> go-admin/routers/api.EditUser (6 handlers)
[GIN-debug] DELETE /api/v1/users/:id         --> go-admin/routers/api.DeleteUser (6 handlers)
2019/05/31 14:48:43 [info] start http server listening :8000

Features

- RESTful API
- Gorm
- logging
- Jwt-go
- Swagger
- Gin
- Graceful restart or stop (fvbock/endless)
- App configurable

开发

  • 何全

特别感谢

本项目主要参考了:
https://github.com/EDDYCJY/go-gin-example  包含更多的例子,上传文件图片等。本项目进行了增改。
https://github.com/LyricTian/gin-admin     主要为 gin+ casbin例子。

其他

## 更新API文档
swag init

## 
cd /opt/go-admin
nohup  go run  main.go   >>  /tmp/go-http.log   2>&1  & 

go-admin's People

Contributors

hequan2017 avatar ketanpatil3 avatar linzworld avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-admin's Issues

对象权限问题,如何解决?

readme中的例子,可以控制test访问user表,但表里的所有数据都可以访问,如何控制用户只能访问自己的用户数据,比如test用户只能访问自己创建的用户数据?

重启出现问题

enforcer, err := casbin.NewEnforcer(path, false)
assignment mismatch: 2 variables but casbin.NewEnforcer returns 1 values
请问如何解决

作者你好,我是新手,我跑你的项目直接报错,希望能帮我看看。

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x117c4e2]

goroutine 1 [running]:
github.com/casbin/casbin/model.Model.RemoveFilteredPolicy(0xc0003d2120, 0x178722c, 0x1, 0x178722c, 0x1, 0x1, 0xc0004b33c0, 0x1, 0x1, 0xc0003b4180)
/Users/Yang/Desktop/Go/src/github.com/casbin/casbin/model/policy.go:115 +0xc2
github.com/casbin/casbin.(*Enforcer).removeFilteredPolicy(0xc00010e310, 0x178722c, 0x1, 0x178722c, 0x1, 0x1, 0xc0004b33c0, 0x1, 0x1, 0x2f35d80)
/Users/Yang/Desktop/Go/src/github.com/casbin/casbin/internal_api.go:67 +0x9e
github.com/casbin/casbin.(*Enforcer).RemoveFilteredNamedGroupingPolicy(0xc00010e310, 0x178722c, 0x1, 0x1, 0xc0004b33c0, 0x1, 0x1, 0xc0004b33c0)
/Users/Yang/Desktop/Go/src/github.com/casbin/casbin/management_api.go:254 +0x8c
github.com/casbin/casbin.(*Enforcer).RemoveFilteredGroupingPolicy(0xc00010e310, 0x1, 0xc0004b33c0, 0x1, 0x1, 0xc00012e730)
/Users/Yang/Desktop/Go/src/github.com/casbin/casbin/management_api.go:229 +0x68
github.com/casbin/casbin.(*Enforcer).DeleteRole(0xc00010e310, 0xc0004b0570, 0x4)
/Users/Yang/Desktop/Go/src/github.com/casbin/casbin/rbac_api.go:70 +0x82
github.com/hequan2017/go-admin/service/role_service.(*Role).LoadPolicy(0xc0001582a0, 0x1, 0x1, 0x0)
/Users/Yang/Desktop/Go/src/github.com/hequan2017/go-admin/service/role_service/role.go:143 +0x87
github.com/hequan2017/go-admin/service/role_service.(*Role).LoadAllPolicy(0xc0001582a0, 0x8, 0xc0003d21b0)
/Users/Yang/Desktop/Go/src/github.com/hequan2017/go-admin/service/role_service/role.go:128 +0x83
github.com/hequan2017/go-admin/middleware/inject.LoadCasbinPolicyData(0x0, 0x1858c60)
/Users/Yang/Desktop/Go/src/github.com/hequan2017/go-admin/middleware/inject/inject.go:61 +0x3d
main.main()
/Users/Yang/Desktop/Go/src/github.com/hequan2017/go-admin/main.go:33 +0x48
exit status 2

swag init出错

mba➜  go-admin git:(master) ✗  >swag init
2019/10/21 16:42:50 Generate swagger docs....
2019/10/21 16:42:50 Generate general API Info, search dir:./
2019/10/21 16:42:50 ParseComment error in file routers/api/v1/role.go :can not find schema type: "v1.json"

Potential case-insensitive import collision

Due to GitHub handle change (to lowercase) for long term purpose, go get may fail fetching github.com/Unknwon/com.
Please consider take some time to update it to github.com/unknwon/com in the go.mod file.
I truly apology for the inconvenience and unintended troubles caused.

一个建议,只是一个建议

启动文件 main.go

setting.Setup()
models.Setup()
logging.Setup()
inject.Init()

是否修改成 类似在 setting.go文件

func init() {
}
使用 init 启动比较优雅?

"msg": "Token错误"

curl -X POST "http://127.0.0.1:8000/auth" -H "accept: application/json" -H "Content-Type: application/json" -d "{ "password": "admin", "username": "123456"}"

{
"code": 401,
"data": null,
"msg": "Token错误"
}

sql文件缺失

你好,最近再学习咱们这个项目,但是文档里的域名和sql的链接失效了,可以给补上sql文件吗?谢谢大神

用户角色关系 应该是多对多

不知道是不是我理解错了。
AddUser方法的role_id 是int类型。不是应该是[]int{}类型吗?
添加用户的时候,用户可属于多角色

jwt不应该存放md5密码

token中不能放敏感信息,因为有效载荷是经过Base64编码生成的,并不是加密。所以不能存放敏感信息。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.