From 59e966ede635b1fbdcddb27206a45e676bf1642f Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Wed, 29 Jul 2020 15:08:44 +0800 Subject: [PATCH 01/13] feat : Adjust the length of the title field from the original 64 to 128 --- models/menu.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/menu.go b/models/menu.go index 4f436935..042014aa 100644 --- a/models/menu.go +++ b/models/menu.go @@ -9,7 +9,7 @@ import ( type Menu struct { MenuId int `json:"menuId" gorm:"primary_key;AUTO_INCREMENT"` MenuName string `json:"menuName" gorm:"size:128;"` - Title string `json:"title" gorm:"size:64;"` + Title string `json:"title" gorm:"size:128;"` Icon string `json:"icon" gorm:"size:128;"` Path string `json:"path" gorm:"size:128;"` Paths string `json:"paths" gorm:"size:128;"` From 061cb61aa73bd37de1f68cffd34cdf74e976b890 Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Wed, 29 Jul 2020 23:31:51 +0800 Subject: [PATCH 02/13] remove : Prot default value removal --- tools/config/application.go | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/tools/config/application.go b/tools/config/application.go index c382de58..91f4ab44 100644 --- a/tools/config/application.go +++ b/tools/config/application.go @@ -19,7 +19,7 @@ func InitApplication(cfg *viper.Viper) *Application { ReadTimeout: cfg.GetInt("readTimeout"), WriterTimeout: cfg.GetInt("writerTimeout"), Host: cfg.GetString("host"), - Port: portDefault(cfg), + Port: cfg.GetString("port"), Name: cfg.GetString("name"), JwtSecret: cfg.GetString("jwtSecret"), Mode: cfg.GetString("mode"), @@ -28,13 +28,4 @@ func InitApplication(cfg *viper.Viper) *Application { } } -var ApplicationConfig = new(Application) - -func portDefault(cfg *viper.Viper) string { - if cfg.GetString("port") == "" { - return "8000" - } else { - return cfg.GetString("port") - } -} - +var ApplicationConfig = new(Application) \ No newline at end of file From f0509242345ddedfcc402abbc9d533b20918772a Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Wed, 29 Jul 2020 23:32:34 +0800 Subject: [PATCH 03/13] sql : remove no used sql --- config/db.sql | 1 - 1 file changed, 1 deletion(-) diff --git a/config/db.sql b/config/db.sql index 95822107..711e19b0 100644 --- a/config/db.sql +++ b/config/db.sql @@ -267,7 +267,6 @@ INSERT INTO sys_dict_type VALUES (11, '1', '1', 1, '1', '1', '1', '2020-04-11 15 INSERT INTO `sys_menu` VALUES (2, 'Upms', '系统管理', 'example', '/upms', '/0/2', 'M', '无', '', 0, 1, '', 'Layout', 1, '0', '1', '1', '0', '2020-04-11 15:52:48', NULL, NULL); INSERT INTO `sys_menu` VALUES (3, 'Sysuser', '用户管理', 'user', 'sysuser', '/0/2/3', 'C', '无', 'system:sysuser:list', 2, NULL, NULL, '/sysuser/index', 1, '0', '1', '1', '0', '2020-04-11 15:52:48', '2020-04-12 11:10:42', NULL); -INSERT INTO `sys_menu` VALUES (4, 'GenCode', '新功能', 'example', '/gencode', '/0/4', 'M', '无', '', 0, 1, '', 'Layout', 1, '0', '1', '1', '0', '2020-04-11 15:52:48', NULL, NULL); INSERT INTO `sys_menu` VALUES (43, NULL, '新增用户', NULL, '/api/v1/sysuser', '/0/2/3/43', 'F', 'POST', 'system:sysuser:add', 3, NULL, NULL, NULL, 0, '0', '1', '1', '0', '2020-04-11 15:52:48', NULL, NULL); INSERT INTO `sys_menu` VALUES (44, NULL, '查询用户', NULL, '/api/v1/sysuser', '/0/2/3/44', 'F', 'GET', 'system:sysuser:query', 3, NULL, NULL, NULL, 0, '0', '1', '1', '0', '2020-04-11 15:52:48', NULL, NULL); INSERT INTO `sys_menu` VALUES (45, NULL, '修改用户', NULL, '/api/v1/sysuser/', '/0/2/3/45', 'F', 'PUT', 'system:sysuser:edit', 3, NULL, NULL, NULL, 0, '0', '1', '1', '0', '2020-04-11 15:52:48', NULL, NULL); From 8d7a6ef3bb76cdfd2a80dc385614870742bf5b6f Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Sat, 1 Aug 2020 10:49:36 +0800 Subject: [PATCH 04/13] feat : update log format --- tools/user.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tools/user.go b/tools/user.go index 526ae38a..d3e0e841 100644 --- a/tools/user.go +++ b/tools/user.go @@ -3,7 +3,6 @@ package tools import ( "fmt" "github.com/gin-gonic/gin" - "log" jwt "go-admin/pkg/jwtauth" ) @@ -21,7 +20,7 @@ func GetUserId(c *gin.Context) int { if data["identity"] != nil { return int((data["identity"]).(float64)) } - log.Println("****************************** 路径:" + c.Request.URL.Path + " 请求方法:" + c.Request.Method + " 说明:缺少identity") + fmt.Println(GetCurrentTimeStr()+" [WARING] " + c.Request.Method + " " + c.Request.URL.Path + " GetUserId 缺少identity") return 0 } @@ -30,7 +29,7 @@ func GetUserIdStr(c *gin.Context) string { if data["identity"] != nil { return Int64ToString(int64((data["identity"]).(float64))) } - log.Println("****************************** 路径:" + c.Request.URL.Path + " 请求方法:" + c.Request.Method + " 缺少identity") + fmt.Println(GetCurrentTimeStr()+" [WARING] " + c.Request.Method + " " + c.Request.URL.Path + " GetUserIdStr 缺少identity") return "" } @@ -39,7 +38,7 @@ func GetUserName(c *gin.Context) string { if data["nice"] != nil { return (data["nice"]).(string) } - fmt.Println("****************************** 路径:" + c.Request.URL.Path + " 请求方法:" + c.Request.Method + " 缺少nice") + fmt.Println(GetCurrentTimeStr()+" [WARING] "+ c.Request.Method +" " + c.Request.URL.Path + " GetUserName 缺少nice") return "" } @@ -48,7 +47,7 @@ func GetRoleName(c *gin.Context) string { if data["rolekey"] != nil { return (data["rolekey"]).(string) } - fmt.Println("****************************** 路径:" + c.Request.URL.Path + " 请求方法:" + c.Request.Method + " 缺少rolekey") + fmt.Println(GetCurrentTimeStr()+" [WARING] "+ c.Request.Method +" " + c.Request.URL.Path + " GetRoleName 缺少rolekey") return "" } @@ -58,6 +57,6 @@ func GetRoleId(c *gin.Context) int { i := int((data["roleid"]).(float64)) return i } - fmt.Println("****************************** 路径:" + c.Request.URL.Path + " 请求方法:" + c.Request.Method + " 缺少roleid") + fmt.Println(GetCurrentTimeStr()+" [WARING] " + c.Request.Method + " " + c.Request.URL.Path + " GetRoleId 缺少roleid") return 0 } From e5d22c78c802b6317d283b941b6bdc51bd2f9b83 Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Sat, 1 Aug 2020 10:50:19 +0800 Subject: [PATCH 05/13] doc : update readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 22172265..ba984d30 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,8 @@ [使用生成工具轻松实现业务](https://www.bilibili.com/video/BV1Dg4y1i79D) +[v1.1.0版本代码生成工具-释放双手](https://www.bilibili.com/video/BV1N54y1i71P) [进阶] + [多命令启动方式讲解以及IDE配置](https://www.bilibili.com/video/BV1Fg4y1q7ph) [go-admin菜单的配置说明](https://www.bilibili.com/video/BV1Wp4y1D715) [必看] From 93ac874312526984f1aefcbe9e4b5b1d6e874875 Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Sat, 1 Aug 2020 10:52:17 +0800 Subject: [PATCH 06/13] feat : logger type error to fatal --- database/mysql-drive.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/database/mysql-drive.go b/database/mysql-drive.go index d007839f..5ef88ef6 100644 --- a/database/mysql-drive.go +++ b/database/mysql-drive.go @@ -20,13 +20,13 @@ func (e *Mysql) Setup() { global.Logger.Info(tools.Green(global.Source)) global.Eloquent, err = db.Open(db.GetDriver(), db.GetConnect()) if err != nil { - global.Logger.Error(tools.Red(db.GetDriver()+" connect error :"), err) + global.Logger.Fatal(tools.Red(db.GetDriver()+" connect error :"), err) } else { global.Logger.Info(tools.Green(db.GetDriver() + " connect success !")) } if global.Eloquent.Error != nil { - global.Logger.Error(tools.Red(" database error :"), global.Eloquent.Error) + global.Logger.Fatal(tools.Red(" database error :"), global.Eloquent.Error) } global.Eloquent.LogMode(config.LoggerConfig.EnabledDB) From 1a580366fc6b09b8039fac4dc4f3904323cb1f43 Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Sat, 1 Aug 2020 10:52:49 +0800 Subject: [PATCH 07/13] feat : enabledbus status to true --- config/settings.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/config/settings.yml b/config/settings.yml index 3cf120da..cce97534 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -20,7 +20,7 @@ settings: # 日志等级 level: all # 业务日志开关 - enabledbus: false + enabledbus: true # 请求日志开关 enabledreq: false # 数据库日志开关 dev模式,将自动开启 @@ -35,8 +35,6 @@ settings: driver: mysql # 数据库连接字符串 mysql 缺省信息 charset=utf8&parseTime=True&loc=Local&timeout=1000ms source: user:password@tcp(127.0.0.1:3306)/dbname?charset=utf8&parseTime=True&loc=Local&timeout=1000ms - # source: sqlite3.db - # source: host=myhost port=myport user=gorm dbname=gorm password=mypassword gen: # 代码生成读取的数据库名称 dbname: dbname From 309be41b05fc6f94f0ed09b8febea20958eeca18 Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Sat, 1 Aug 2020 11:00:06 +0800 Subject: [PATCH 08/13] feat : added version & config --- cmd/api/server.go | 12 ++++++--- cmd/cobra.go | 7 +++-- cmd/config/server.go | 61 +++++++++++++++++++++++++++++++++++++++++++ cmd/migrate/server.go | 3 ++- cmd/version/server.go | 30 +++++++++++++++++++++ 5 files changed, 106 insertions(+), 7 deletions(-) create mode 100644 cmd/config/server.go create mode 100644 cmd/version/server.go diff --git a/cmd/api/server.go b/cmd/api/server.go index bc37f865..fba0c8fa 100644 --- a/cmd/api/server.go +++ b/cmd/api/server.go @@ -25,9 +25,10 @@ var ( port string mode string StartCmd = &cobra.Command{ - Use: "server", - Short: "Start API server", - Example: "go-admin server config/settings.yml", + Use: "server", + Short: "Start API server", + Example: "go-admin server -c config/settings.yml", + SilenceUsage: true, PreRun: func(cmd *cobra.Command, args []string) { setup() }, @@ -35,8 +36,11 @@ var ( return run() }, } + + ) +var echoTimes int func init() { StartCmd.PersistentFlags().StringVarP(&configYml, "config", "c", "config/settings.yml", "Start server with provided configuration file") StartCmd.PersistentFlags().StringVarP(&port, "port", "p", "8000", "Tcp port server listening on") @@ -113,6 +117,6 @@ func run() error { } func tip() { - usageStr := `欢迎使用 `+ tools.Green( `go-admin ` +global.Version) + ` 可以使用 ` + tools.Red(`-h`) + ` 查看命令` + usageStr := `欢迎使用 ` + tools.Green(`go-admin `+global.Version) + ` 可以使用 ` + tools.Red(`-h`) + ` 查看命令` fmt.Printf("%s \n\n", usageStr) } diff --git a/cmd/cobra.go b/cmd/cobra.go index 11a20835..6bf9808d 100644 --- a/cmd/cobra.go +++ b/cmd/cobra.go @@ -5,7 +5,9 @@ import ( "fmt" "github.com/spf13/cobra" "go-admin/cmd/api" + "go-admin/cmd/config" "go-admin/cmd/migrate" + "go-admin/cmd/version" "go-admin/global" "go-admin/tools" "os" @@ -13,9 +15,8 @@ import ( var rootCmd = &cobra.Command{ Use: "go-admin", - Short: "-v", + Short: "go-admin", SilenceUsage: true, - DisableAutoGenTag: true, Long: `go-admin`, Args: func(cmd *cobra.Command, args []string) error { if len(args) < 1 { @@ -40,6 +41,8 @@ func tip() { func init() { rootCmd.AddCommand(api.StartCmd) rootCmd.AddCommand(migrate.StartCmd) + rootCmd.AddCommand(version.StartCmd) + rootCmd.AddCommand(config.StartCmd) } //Execute : apply commands diff --git a/cmd/config/server.go b/cmd/config/server.go new file mode 100644 index 00000000..c99a3d2c --- /dev/null +++ b/cmd/config/server.go @@ -0,0 +1,61 @@ +package config + +import ( + "encoding/json" + "fmt" + "go-admin/tools/config" + + "github.com/spf13/cobra" +) + +var ( + configYml string + mode string + StartCmd = &cobra.Command{ + Use: "config", + Short: "Get Application config info", + Example: "go-admin config -c config/settings.yml", + Run: func(cmd *cobra.Command, args []string) { + run() + }, + } +) + +func init() { + StartCmd.PersistentFlags().StringVarP(&configYml, "config", "c", "config/settings.yml", "Start server with provided configuration file") +} + +func run() { + config.Setup(configYml) + + application, errs := json.MarshalIndent(config.ApplicationConfig, "", " ") //转换成JSON返回的是byte[] + if errs != nil { + fmt.Println(errs.Error()) + } + fmt.Println("application:", string(application)) + + jwt, errs := json.MarshalIndent(config.JwtConfig, "", " ") //转换成JSON返回的是byte[] + if errs != nil { + fmt.Println(errs.Error()) + } + fmt.Println("jwt:", string(jwt)) + + database, errs := json.MarshalIndent(config.DatabaseConfig, "", " ") //转换成JSON返回的是byte[] + if errs != nil { + fmt.Println(errs.Error()) + } + fmt.Println("database:", string(database)) + + gen, errs := json.MarshalIndent(config.GenConfig, "", " ") //转换成JSON返回的是byte[] + if errs != nil { + fmt.Println(errs.Error()) + } + fmt.Println("gen:", string(gen)) + + loggerConfig, errs := json.MarshalIndent(config.LoggerConfig, "", " ") //转换成JSON返回的是byte[] + if errs != nil { + fmt.Println(errs.Error()) + } + fmt.Println("logger:", string(loggerConfig)) + +} diff --git a/cmd/migrate/server.go b/cmd/migrate/server.go index a7094ca7..dba76758 100644 --- a/cmd/migrate/server.go +++ b/cmd/migrate/server.go @@ -17,7 +17,8 @@ var ( mode string StartCmd = &cobra.Command{ Use: "init", - Short: "initialize the database", + Short: "Initialize the database", + Example: "go-admin init -c config/settings.yml", Run: func(cmd *cobra.Command, args []string) { run() }, diff --git a/cmd/version/server.go b/cmd/version/server.go new file mode 100644 index 00000000..2e959f66 --- /dev/null +++ b/cmd/version/server.go @@ -0,0 +1,30 @@ +package version + +import ( + "fmt" + "github.com/spf13/cobra" + "go-admin/global" +) + +var ( + configYml string + port string + mode string + StartCmd = &cobra.Command{ + Use: "version", + Short: "Get version info", + Example: "go-admin version", + PreRun: func(cmd *cobra.Command, args []string) { + + }, + RunE: func(cmd *cobra.Command, args []string) error { + return run() + }, + } +) + +func run() error { + fmt.Println(global.Version) + return nil +} + From 55b055b9a039c0764121e2c8d79ebb39d864364a Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Sat, 1 Aug 2020 11:00:36 +0800 Subject: [PATCH 09/13] version : update version --- global/adm.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/global/adm.go b/global/adm.go index ce398930..b7192716 100644 --- a/global/adm.go +++ b/global/adm.go @@ -21,7 +21,7 @@ var ( var Version string func init() { - Version = "1.1.0" + Version = "1.1.1" } var ( From 259b77dc08ccb2152c0e4b50dc133bb5fe6539a9 Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Sat, 1 Aug 2020 11:08:40 +0800 Subject: [PATCH 10/13] fix : fix no auth code gen router --- apis/tools/gen.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/apis/tools/gen.go b/apis/tools/gen.go index 24b962f4..87546c52 100644 --- a/apis/tools/gen.go +++ b/apis/tools/gen.go @@ -60,10 +60,10 @@ func GenCode(c *gin.Context) { tab, _ := table.Get() ischeckrole := true - rouyerfile := "template/routercheckrole.go.template" + routerfile := "template/routercheckrole.go.template" - if c.Param("ischeckrole") != "" { - ischeckrole, err = tools2.StringToBool(c.Query("ischeckrole")) + if c.Request.FormValue("ischeckrole") != "" { + ischeckrole, err = tools2.StringToBool(c.Request.FormValue("ischeckrole")) if err != nil { ischeckrole = true } @@ -75,14 +75,14 @@ func GenCode(c *gin.Context) { if !ischeckrole { oldTest = "// {{无需认证路由自动补充在此处请勿删除}}" newText = "// {{无需认证路由自动补充在此处请勿删除}} \r\n register" + tab.ClassName + "Router(v1)" - rouyerfile = "template/routernocheckrole.go.template" + routerfile = "template/routernocheckrole.go.template" } t1, err := template.ParseFiles("template/model.go.template") tools2.HasError(err, "", -1) t2, err := template.ParseFiles("template/api.go.template") tools2.HasError(err, "", -1) - t3, err := template.ParseFiles(rouyerfile) + t3, err := template.ParseFiles(routerfile) tools2.HasError(err, "", -1) t4, err := template.ParseFiles("template/js.go.template") tools2.HasError(err, "", -1) From 89fe63c915188fd356106a177ff8af1eee3995e1 Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Sat, 1 Aug 2020 12:56:19 +0800 Subject: [PATCH 11/13] gen : upgrade gen --- apis/tools/systables.go | 9 ++++++++- middleware/customerror.go | 2 +- template/vue.go.template | 26 ++++++++++++++++++++++---- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/apis/tools/systables.go b/apis/tools/systables.go index f28fe97a..b2e57ce6 100644 --- a/apis/tools/systables.go +++ b/apis/tools/systables.go @@ -168,8 +168,15 @@ func genTableInit(tablesList []string, i int, c *gin.Context) (tools.SysTables, } if strings.Contains(dbcolumn[i].ColumnType, "int") { - column.GoType = "int" + if strings.Contains(dbcolumn[i].ColumnKey, "PR") { + column.GoType = "int" + } else { + column.GoType = "string" + } column.HtmlType = "input" + } else if strings.Contains(dbcolumn[i].ColumnType, "timestamp") { + column.GoType = "time.Time" + column.HtmlType = "datetime" } else { column.GoType = "string" column.HtmlType = "input" diff --git a/middleware/customerror.go b/middleware/customerror.go index 5fbd99d8..2dac7bc6 100644 --- a/middleware/customerror.go +++ b/middleware/customerror.go @@ -26,7 +26,7 @@ func CustomError(c *gin.Context) { } c.Status(statusCode) fmt.Println( - time.Now().Format("\n 2006-01-02 15:04:05.9999"), + time.Now().Format("2006-01-02 15:04:05.9999"), "[ERROR]", c.Request.Method, c.Request.URL, diff --git a/template/vue.go.template b/template/vue.go.template index 305d1ad7..aa179d41 100644 --- a/template/vue.go.template +++ b/template/vue.go.template @@ -132,10 +132,14 @@ {{- else if eq "select" .HtmlType -}} - - - + + {{- else if eq "radio" .HtmlType -}} @@ -145,6 +149,20 @@ :label="dict.dictValue" >{{"{{"}} dict.dictLabel {{"}}"}} + {{- else if eq "datetime" .HtmlType -}} + + + {{- else if eq "textarea" .HtmlType -}} + + {{- end }} {{- end }} From 88a458a00392c86072aee5b301d36cf62503d196 Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Sat, 1 Aug 2020 14:07:06 +0800 Subject: [PATCH 12/13] gen : remove value-format --- template/vue.go.template | 1 - 1 file changed, 1 deletion(-) diff --git a/template/vue.go.template b/template/vue.go.template index aa179d41..e19ee053 100644 --- a/template/vue.go.template +++ b/template/vue.go.template @@ -153,7 +153,6 @@ {{- else if eq "textarea" .HtmlType -}} From c98f38cab4d33a108f3eb98ff5674e207af967c1 Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Sat, 1 Aug 2020 14:43:38 +0800 Subject: [PATCH 13/13] =?UTF-8?q?gen=20=EF=BC=9Aupdate=20import?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- template/model.go.template | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/template/model.go.template b/template/model.go.template index 92bffea2..682685cd 100644 --- a/template/model.go.template +++ b/template/model.go.template @@ -3,7 +3,10 @@ package models import ( orm "go-admin/global" "go-admin/tools" -_ "time" +{{$ii := 1}} +{{ range .Columns }} + {{- if eq .GoType "time.Time" -}}{{- if eq $ii 1 -}} "time" {{$ii = 2}}{{- end}}{{- end}} +{{- end }} ) type {{.ClassName}} struct {