From 1f416cc8fd691f8702c5a03c3e86ec6649665aac Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Fri, 10 Jul 2020 00:21:26 +0800 Subject: [PATCH] fear: Support sqlite and pgsql --- cmd/api/server.go | 8 +- config/db.sql | 2 + go.mod | 1 - handler/nofound.go | 2 +- middleware/permission.go | 2 +- router/sys_router.go | 240 ++++++++++++++++++--------------------- sqlite.db | Bin 163840 -> 163840 bytes tools/user.go | 2 +- 8 files changed, 121 insertions(+), 136 deletions(-) diff --git a/cmd/api/server.go b/cmd/api/server.go index 472385e..fb65127 100644 --- a/cmd/api/server.go +++ b/cmd/api/server.go @@ -92,14 +92,14 @@ func run() error { }() content, _ := ioutil.ReadFile("./static/go-admin.txt") fmt.Println(string(content)) - fmt.Printf("%s Server Run http://%s:%s/ \r\n", tools.GetCurrntTimeStr(), config.ApplicationConfig.Host, config.ApplicationConfig.Port) - fmt.Printf("%s Swagger URL http://%s:%s/swagger/index.html \r\n", tools.GetCurrntTimeStr(), config.ApplicationConfig.Host, config.ApplicationConfig.Port) - fmt.Printf("%s Enter Control + C Shutdown Server \r\n", tools.GetCurrntTimeStr()) + fmt.Printf("%s Server Run http://%s:%s/ \r\n", tools.GetCurrentTimeStr(), config.ApplicationConfig.Host, config.ApplicationConfig.Port) + fmt.Printf("%s Swagger URL http://%s:%s/swagger/index.html \r\n", tools.GetCurrentTimeStr(), config.ApplicationConfig.Host, config.ApplicationConfig.Port) + fmt.Printf("%s Enter Control + C Shutdown Server \r\n", tools.GetCurrentTimeStr()) // 等待中断信号以优雅地关闭服务器(设置 5 秒的超时时间) quit := make(chan os.Signal) signal.Notify(quit, os.Interrupt) <-quit - fmt.Printf("%s Shutdown Server ... \r\n", tools.GetCurrntTimeStr()) + fmt.Printf("%s Shutdown Server ... \r\n", tools.GetCurrentTimeStr()) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() diff --git a/config/db.sql b/config/db.sql index 4d97455..03b635b 100644 --- a/config/db.sql +++ b/config/db.sql @@ -1,4 +1,5 @@ -- 开始初始化数据 ; +BEGIN; INSERT INTO casbin_rule VALUES ('p', 'Tester', '/api/v1/calendar', 'GET', NULL, NULL, NULL); INSERT INTO casbin_rule VALUES ('p', 'Tester', '/api/v1/calendar', 'POST', NULL, NULL, NULL); INSERT INTO casbin_rule VALUES ('p', 'Tester', '/api/v1/calendar/:id', 'DELETE', NULL, NULL, NULL); @@ -637,5 +638,6 @@ INSERT INTO sys_user VALUES (1, 'zhangwj', '13818888888', 1, NULL, '', '0', '1@q INSERT INTO sys_user VALUES (2, 'zhangwj', '13211111111', 3, NULL, NULL, '0', 'q@q.com', 8, 2, '1', '1', NULL, 0, '2019-11-12 18:28:27', '2020-03-14 20:08:43', NULL, 'zhangwj', '$2a$10$CqMwHahA3cNrNv16CoSxmeD4XMPU.BiKHPEAeaG5oXMavOKrjInXi'); INSERT INTO sys_user VALUES (3, '李四', '13838385438', 2, '', '', '0', 'qq@qq.com', 7, 2, '1', '1', '', 0, '2020-04-07 22:17:38', '2020-05-03 20:31:00', NULL, 'lisi', '$2a$10$DejldFea5.hGZGC7/oVN9OLDrHAWgu9l29RDz9FomLnWnro4umYl2'); INSERT INTO sys_user VALUES (4, '王五', '13535353535', 3, '', '', '2', 'qq@qq.com', 8, 2, '1', '1', '', 0, '2020-04-12 14:06:49', '2020-04-12 14:07:09', NULL, 'wangwu', '$2a$10$3.RT6rpXANXvvlibX6PzU.FGA2CvfDxd1UmJ2H5zTzF4sYocbvsTO'); +COMMIT; -- 数据完成 ; \ No newline at end of file diff --git a/go.mod b/go.mod index f505cbd..0eac1a6 100644 --- a/go.mod +++ b/go.mod @@ -34,7 +34,6 @@ require ( github.com/ugorji/go v1.1.7 // indirect github.com/unrolled/secure v1.0.8 golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59 - golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e // indirect golang.org/x/tools v0.0.0-20200402223321-bcf690261a44 // indirect gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect gopkg.in/yaml.v2 v2.2.8 // indirect diff --git a/handler/nofound.go b/handler/nofound.go index a2c7570..46ee59b 100644 --- a/handler/nofound.go +++ b/handler/nofound.go @@ -2,8 +2,8 @@ package handler import ( "github.com/gin-gonic/gin" - jwt "go-admin/pkg/jwtauth" log "github.com/sirupsen/logrus" + jwt "go-admin/pkg/jwtauth" "net/http" ) diff --git a/middleware/permission.go b/middleware/permission.go index 157360a..99a4826 100644 --- a/middleware/permission.go +++ b/middleware/permission.go @@ -2,11 +2,11 @@ package middleware import ( "github.com/gin-gonic/gin" + log "github.com/sirupsen/logrus" mycasbin "go-admin/pkg/casbin" "go-admin/pkg/jwtauth" _ "go-admin/pkg/jwtauth" "go-admin/tools" - log "github.com/sirupsen/logrus" "net/http" ) diff --git a/router/sys_router.go b/router/sys_router.go index eede6d2..b7d87a7 100644 --- a/router/sys_router.go +++ b/router/sys_router.go @@ -13,10 +13,9 @@ import ( "go-admin/handler" "go-admin/middleware" "go-admin/pkg/jwtauth" - jwt "go-admin/pkg/jwtauth" ) -func InitSysRouter(r *gin.Engine, authMiddleware *jwt.GinJWTMiddleware) *gin.RouterGroup { +func InitSysRouter(r *gin.Engine, authMiddleware *jwtauth.GinJWTMiddleware) *gin.RouterGroup { g := r.Group("") sysBaseRouter(g) // 静态文件 @@ -90,156 +89,141 @@ func sysCheckRoleRouterInit(r *gin.RouterGroup, authMiddleware *jwtauth.GinJWTMi // Refresh time can be longer than token timeout r.GET("/refresh_token", authMiddleware.RefreshHandler) - v1 := r.Group("/api/v1") - - registerPageRouter(v1, authMiddleware) - registerBaseRouter(v1, authMiddleware) - registerDeptRouter(v1, authMiddleware) - registerDictRouter(v1, authMiddleware) - registerSysUserRouter(v1, authMiddleware) - registerRoleRouter(v1, authMiddleware) - registerConfigRouter(v1, authMiddleware) - registerUserCenterRouter(v1, authMiddleware) - registerPostRouter(v1, authMiddleware) - registerMenuRouter(v1, authMiddleware) - registerLoginLogRouter(v1, authMiddleware) - registerOperLogRouter(v1, authMiddleware) -} - -func registerBaseRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) { - v1auth := v1.Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole()) + v1 := r.Group("/api/v1").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole()) { - v1auth.GET("/getinfo", system.GetInfo) - v1auth.GET("/menurole", system.GetMenuRole) - v1auth.PUT("/roledatascope", system.UpdateRoleDataScope) - v1auth.GET("/roleMenuTreeselect/:roleId", system.GetMenuTreeRoleselect) - v1auth.GET("/roleDeptTreeselect/:roleId", system.GetDeptTreeRoleselect) - - v1auth.POST("/logout", handler.LogOut) - v1auth.GET("/menuids", system.GetMenuIDS) - - v1auth.GET("/operloglist", log2.GetOperLogList) - v1auth.GET("/configKey/:configKey", system.GetConfigByConfigKey) + registerPageRouter(v1) + registerBaseRouter(v1) + registerDeptRouter(v1) + registerDictRouter(v1) + registerSysUserRouter(v1) + registerRoleRouter(v1) + registerConfigRouter(v1) + registerUserCenterRouter(v1) + registerPostRouter(v1) + registerMenuRouter(v1) + registerLoginLogRouter(v1) + registerOperLogRouter(v1) } } -func registerPageRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) { - v1auth := v1.Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole()) - { - v1auth.GET("/deptList", system.GetDeptList) - v1auth.GET("/deptTree", system.GetDeptTree) - v1auth.GET("/sysUserList", system.GetSysUserList) - v1auth.GET("/rolelist", system.GetRoleList) - v1auth.GET("/configList", system.GetConfigList) - v1auth.GET("/postlist", system.GetPostList) - v1auth.GET("/menulist", system.GetMenuList) - v1auth.GET("/loginloglist", log2.GetLoginLogList) - } +func registerBaseRouter(v1 gin.IRoutes) { + + v1.GET("/getinfo", system.GetInfo) + v1.GET("/menurole", system.GetMenuRole) + v1.PUT("/roledatascope", system.UpdateRoleDataScope) + v1.GET("/roleMenuTreeselect/:roleId", system.GetMenuTreeRoleselect) + v1.GET("/roleDeptTreeselect/:roleId", system.GetDeptTreeRoleselect) + + v1.POST("/logout", handler.LogOut) + v1.GET("/menuids", system.GetMenuIDS) + + v1.GET("/operloglist", log2.GetOperLogList) + v1.GET("/configKey/:configKey", system.GetConfigByConfigKey) + } -func registerUserCenterRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) { - user := v1.Group("/user").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole()) - { - user.GET("/profile", system.GetSysUserProfile) - user.POST("/avatar", system.InsetSysUserAvatar) - user.PUT("/pwd", system.SysUserUpdatePwd) - } +func registerPageRouter(v1 gin.IRoutes) { + v1.GET("/deptList", system.GetDeptList) + v1.GET("/deptTree", system.GetDeptTree) + v1.GET("/sysUserList", system.GetSysUserList) + v1.GET("/rolelist", system.GetRoleList) + v1.GET("/configList", system.GetConfigList) + v1.GET("/postlist", system.GetPostList) + v1.GET("/menulist", system.GetMenuList) + v1.GET("/loginloglist", log2.GetLoginLogList) + } -func registerOperLogRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) { - operlog := v1.Group("/operlog").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole()) - { - operlog.GET("/:operId", log2.GetOperLog) - operlog.DELETE("/:operId", log2.DeleteOperLog) - } +func registerUserCenterRouter(v1 gin.IRoutes) { + v1.GET("/user/profile", system.GetSysUserProfile) + v1.POST("/user/avatar", system.InsetSysUserAvatar) + v1.PUT("/user/pwd", system.SysUserUpdatePwd) } -func registerLoginLogRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) { - loginlog := v1.Group("/loginlog").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole()) - { - loginlog.GET("/:infoId", log2.GetLoginLog) - loginlog.POST("", log2.InsertLoginLog) - loginlog.PUT("", log2.UpdateLoginLog) - loginlog.DELETE("/:infoId", log2.DeleteLoginLog) - } +func registerOperLogRouter(v1 gin.IRoutes) { + + v1.GET("/operlog/:operId", log2.GetOperLog) + v1.DELETE("/operlog/:operId", log2.DeleteOperLog) + } -func registerPostRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) { - post := v1.Group("/post").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole()) - { - post.GET("/:postId", system.GetPost) - post.POST("", system.InsertPost) - post.PUT("", system.UpdatePost) - post.DELETE("/:postId", system.DeletePost) - } +func registerLoginLogRouter(v1 gin.IRoutes) { + + v1.GET("/loginlog/:infoId", log2.GetLoginLog) + v1.POST("/loginlog", log2.InsertLoginLog) + v1.PUT("/loginlog", log2.UpdateLoginLog) + v1.DELETE("/loginlog/:infoId", log2.DeleteLoginLog) + } -func registerMenuRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) { - menu := v1.Group("/menu").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole()) - { - menu.GET("/:id", system.GetMenu) - menu.POST("", system.InsertMenu) - menu.PUT("", system.UpdateMenu) - menu.DELETE("/:id", system.DeleteMenu) - } +func registerPostRouter(v1 gin.IRoutes) { + + v1.GET("/post/:postId", system.GetPost) + v1.POST("/post", system.InsertPost) + v1.PUT("/post", system.UpdatePost) + v1.DELETE("/post/:postId", system.DeletePost) + } -func registerConfigRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) { - config := v1.Group("/config").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole()) - { - config.GET("/:configId", system.GetConfig) - config.POST("", system.InsertConfig) - config.PUT("", system.UpdateConfig) - config.DELETE("/:configId", system.DeleteConfig) - } +func registerMenuRouter(v1 gin.IRoutes) { + + v1.GET("/menu/:id", system.GetMenu) + v1.POST("/menu", system.InsertMenu) + v1.PUT("/menu", system.UpdateMenu) + v1.DELETE("/menu/:id", system.DeleteMenu) + } -func registerRoleRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) { - role := v1.Group("/role").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole()) - { - role.GET("/:roleId", system.GetRole) - role.POST("", system.InsertRole) - role.PUT("", system.UpdateRole) - role.DELETE("/:roleId", system.DeleteRole) - } +func registerConfigRouter(v1 gin.IRoutes) { + v1.GET("/config/:configId", system.GetConfig) + v1.POST("/config", system.InsertConfig) + v1.PUT("/config", system.UpdateConfig) + v1.DELETE("/config/:configId", system.DeleteConfig) + } -func registerSysUserRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) { - sysuser := v1.Group("/sysUser").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole()) - { - sysuser.GET("/:userId", system.GetSysUser) - sysuser.GET("/", system.GetSysUserInit) - sysuser.POST("", system.InsertSysUser) - sysuser.PUT("", system.UpdateSysUser) - sysuser.DELETE("/:userId", system.DeleteSysUser) - } +func registerRoleRouter(v1 gin.IRoutes) { + + v1.GET("/role/:roleId", system.GetRole) + v1.POST("/role", system.InsertRole) + v1.PUT("/role", system.UpdateRole) + v1.DELETE("/role/:roleId", system.DeleteRole) + } -func registerDictRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) { - dicts := v1.Group("/dict").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole()) - { - dicts.GET("/datalist", dict.GetDictDataList) - dicts.GET("/typelist", dict.GetDictTypeList) - dicts.GET("/typeoptionselect", dict.GetDictTypeOptionSelect) +func registerSysUserRouter(v1 gin.IRoutes) { - dicts.GET("/data/:dictCode", dict.GetDictData) - dicts.POST("/data", dict.InsertDictData) - dicts.PUT("/data/", dict.UpdateDictData) - dicts.DELETE("/data/:dictCode", dict.DeleteDictData) + v1.GET("/sysUser/:userId", system.GetSysUser) + v1.GET("/sysUser/", system.GetSysUserInit) + v1.POST("/sysUser", system.InsertSysUser) + v1.PUT("/sysUser", system.UpdateSysUser) + v1.DELETE("/sysUser/:userId", system.DeleteSysUser) - dicts.GET("/type/:dictId", dict.GetDictType) - dicts.POST("/type", dict.InsertDictType) - dicts.PUT("/type", dict.UpdateDictType) - dicts.DELETE("/type/:dictId", dict.DeleteDictType) - } } -func registerDeptRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) { - dept := v1.Group("/dept").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole()) - { - dept.GET("/:deptId", system.GetDept) - dept.POST("", system.InsertDept) - dept.PUT("", system.UpdateDept) - dept.DELETE("/:id", system.DeleteDept) - } +func registerDictRouter(v1 gin.IRoutes) { + + v1.GET("/dict/datalist", dict.GetDictDataList) + v1.GET("/dict/typelist", dict.GetDictTypeList) + v1.GET("/dict/typeoptionselect", dict.GetDictTypeOptionSelect) + + v1.GET("/dict/data/:dictCode", dict.GetDictData) + v1.POST("/dict/data", dict.InsertDictData) + v1.PUT("/dict/data/", dict.UpdateDictData) + v1.DELETE("/dict/data/:dictCode", dict.DeleteDictData) + + v1.GET("/dict/type/:dictId", dict.GetDictType) + v1.POST("/dict/type", dict.InsertDictType) + v1.PUT("/dict/type", dict.UpdateDictType) + v1.DELETE("/dict/type/:dictId", dict.DeleteDictType) + +} + +func registerDeptRouter(v1 gin.IRoutes) { + + v1.GET("/dept/:deptId", system.GetDept) + v1.POST("/dept", system.InsertDept) + v1.PUT("/dept", system.UpdateDept) + v1.DELETE("/dept/:id", system.DeleteDept) + } diff --git a/sqlite.db b/sqlite.db index 994fe62149b7fdff6bb5676932c8ee9fda5bdea4..f250f704b9d3eb65ba021bd725d393605114f53d 100644 GIT binary patch delta 316 zcmZo@;A&{#njkHx&%nT-0mY0!T5h6_F{?gPFaknd19Kok=t7ona$-X=)X>V?jJ eMmcsCV0he`EU0j8`~UBZj1@qo@A)S#5Cj0U=~CeU delta 196 zcmZo@;A&{#njkHx!@$6x0mLvMJ5k4&Rfj=OBWq)doINA!=4SiG0zOvGr3`#~c}sbW zxL$EC-7KgunUlH6iFLBv_LR*$yMC~4W@&iA-~8tM_BZDlB@~#s*j2Z)e_)hj_iPN8 zWfwOzWNeBoNleN~EiTN-EJ-y^PApE!%!@B7#U`TR9OUX4;;Inh=;Y(7paj)2Isg5{ U?f<_sGFD7;U}D+KqVR_w08Pd|tpET3 diff --git a/tools/user.go b/tools/user.go index d6bccc5..0ab50ec 100644 --- a/tools/user.go +++ b/tools/user.go @@ -3,8 +3,8 @@ package tools import ( "fmt" "github.com/gin-gonic/gin" - jwt "go-admin/pkg/jwtauth" log "github.com/sirupsen/logrus" + jwt "go-admin/pkg/jwtauth" ) func ExtractClaims(c *gin.Context) jwt.MapClaims {