diff --git a/compile_assets.go b/compile_assets.go index 0654c8f..8c1ba5e 100644 --- a/compile_assets.go +++ b/compile_assets.go @@ -1,7 +1,6 @@ package main import ( - "io/ioutil" "os" "path/filepath" "regexp" @@ -54,11 +53,11 @@ var AssetPaths = map[string]string{ listContent += ` }` - err = ioutil.WriteFile(outputPath+"/assets_list.go", []byte(listContent), 0644) + err = os.WriteFile(outputPath+"/assets_list.go", []byte(listContent), 0644) if err != nil { return } - err = ioutil.WriteFile(outputPath+"/assets_path.go", []byte(pathsContent), 0644) + err = os.WriteFile(outputPath+"/assets_path.go", []byte(pathsContent), 0644) if err != nil { return } @@ -67,7 +66,7 @@ var AssetPaths = map[string]string{ func getAllFiles(dirPth string) (files []string, err error) { var dirs []string - dir, err := ioutil.ReadDir(dirPth) + dir, err := os.ReadDir(dirPth) if err != nil { return nil, err } diff --git a/compile_tmpl.go b/compile_tmpl.go index 038a95e..ef40554 100644 --- a/compile_tmpl.go +++ b/compile_tmpl.go @@ -2,7 +2,7 @@ package main import ( "fmt" - "io/ioutil" + "os" "path" "strings" ) @@ -16,7 +16,7 @@ var ` + varName + ` = map[string]string{` content += `}` - _ = ioutil.WriteFile(outputPath, []byte(content), 0644) + _ = os.WriteFile(outputPath, []byte(content), 0644) } func fixPath(p string) string { @@ -27,7 +27,7 @@ func fixPath(p string) string { } func getContentFromDir(content, dirPath, rootPath string) string { - files, _ := ioutil.ReadDir(dirPath) + files, _ := os.ReadDir(dirPath) for _, f := range files { @@ -36,7 +36,7 @@ func getContentFromDir(content, dirPath, rootPath string) string { continue } - b, err := ioutil.ReadFile(dirPath + f.Name()) + b, err := os.ReadFile(dirPath + f.Name()) if err != nil { fmt.Print(err) } diff --git a/db.go b/db.go index a34ae58..eb70b67 100644 --- a/db.go +++ b/db.go @@ -111,15 +111,15 @@ func askForDBConfig(info *dbInfo) config.DatabaseList { return map[string]config.Database{ "default": { - Host: info.Host, - Port: info.Port, - User: info.User, - Pwd: info.Password, - Name: info.Database, - MaxIdleCon: 5, - MaxOpenCon: 10, - Driver: info.DriverName, - File: "", + Host: info.Host, + Port: info.Port, + User: info.User, + Pwd: info.Password, + Name: info.Database, + MaxIdleConns: 5, + MaxOpenConns: 10, + Driver: info.DriverName, + File: "", }, } } else { diff --git a/go.sum b/go.sum index c96b73b..12c05d1 100644 --- a/go.sum +++ b/go.sum @@ -8,8 +8,8 @@ github.com/AlecAivazis/survey/v2 v2.3.1 h1:lzkuHA60pER7L4eYL8qQJor4bUWlJe4V0gqAT github.com/AlecAivazis/survey/v2 v2.3.1/go.mod h1:TH2kPCDU3Kqq7pLbnCWwZXDBjnhZtmsCle5EiYDJ2fg= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/GoAdminGroup/go-admin v1.2.23 h1:myn18AVWNs2U2e2OwzzSDk1QNlOsOvv1t8G4X8UygE4= -github.com/GoAdminGroup/go-admin v1.2.23/go.mod h1:EX+fDr3yPIQ8jKoXzjh5A8MbhfCY2KOhcd0rctlxBH4= +github.com/GoAdminGroup/go-admin v1.2.24 h1:s1pyok1zAkDj+Ro0TKsB+2igyJ7sepHIXpB78LeuoQU= +github.com/GoAdminGroup/go-admin v1.2.24/go.mod h1:voQOP8uoaNi/V0EO3WtOvAm3fvTaNlxrmpe4nZdbzas= github.com/GoAdminGroup/html v0.0.1 h1:SdWNWl4OKPsvDk2GDp5ZKD6ceWoN8n4Pj6cUYxavUd0= github.com/GoAdminGroup/html v0.0.1/go.mod h1:A1laTJaOx8sQ64p2dE8IqtstDeCNBHEazrEp7hR5VvM= github.com/NebulousLabs/fastrand v0.0.0-20181203155948-6fb6489aac4e h1:n+DcnTNkQnHlwpsrHoQtkrJIO7CBx029fw6oR4vIob4= @@ -58,6 +58,8 @@ github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5a github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= @@ -133,8 +135,6 @@ github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= -github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= -github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/schollz/progressbar v1.0.0 h1:gbyFReLHDkZo8mxy/dLWMr+Mpb1MokGJ1FqCiqacjZM= github.com/schollz/progressbar v1.0.0/go.mod h1:/l9I7PC3L3erOuz54ghIRKUEFcosiWfLvJv+Eq26UMs= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= diff --git a/helper.go b/helper.go index 4e3d16e..acaa64e 100644 --- a/helper.go +++ b/helper.go @@ -6,8 +6,8 @@ import ( "errors" "fmt" "go/format" + "io" "io/fs" - "io/ioutil" "net/http" "os" "strings" @@ -47,7 +47,7 @@ func getLatestVersion() string { return "" } - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) if err != nil || body == nil { return "" @@ -95,7 +95,7 @@ func mkdirs(dirs []string) { func mkEmptyFiles(names []string) { for _, name := range names { - checkError(ioutil.WriteFile(name, []byte{}, os.ModePerm)) + checkError(os.WriteFile(name, []byte{}, os.ModePerm)) } } @@ -157,16 +157,16 @@ func (cfgs *WriteFilesConfig) AddRaw(path, file string, perm fs.FileMode) { func writeFileOfInstallation(cfg WriteFileConfig) { if cfg.IsFormat { - checkError(ioutil.WriteFile(cfg.Path, parseFileWithFormat(readFileOfInstallation(&installationTmplFS, cfg.File), cfg.Data), cfg.Perm)) + checkError(os.WriteFile(cfg.Path, parseFileWithFormat(readFileOfInstallation(&installationTmplFS, cfg.File), cfg.Data), cfg.Perm)) return } - checkError(ioutil.WriteFile(cfg.Path, parseFile(readFileOfInstallation(&installationTmplFS, cfg.File), cfg.Data), cfg.Perm)) + checkError(os.WriteFile(cfg.Path, parseFile(readFileOfInstallation(&installationTmplFS, cfg.File), cfg.Data), cfg.Perm)) } func writeFilesOfInstallation(cfgs WriteFilesConfig) { for _, cfg := range cfgs { if !cfg.Parse { - checkError(ioutil.WriteFile(cfg.Path, []byte(readFileOfInstallation(&installationTmplFS, cfg.File)), cfg.Perm)) + checkError(os.WriteFile(cfg.Path, []byte(readFileOfInstallation(&installationTmplFS, cfg.File)), cfg.Perm)) continue } writeFileOfInstallation(cfg) diff --git a/minify.go b/minify.go index 9ce94e9..fe55963 100644 --- a/minify.go +++ b/minify.go @@ -5,7 +5,6 @@ import ( "crypto/md5" "encoding/hex" "fmt" - "io/ioutil" "log" "os" "path" @@ -75,7 +74,7 @@ func jsMinifier(inputDir, outputFile string, hash bool) { } filePath := inputDir + name - fileTxt, err := ioutil.ReadFile(filePath) + fileTxt, err := os.ReadFile(filePath) if err != nil { checkError(err) } @@ -128,7 +127,7 @@ func removeOutputFile(outputFile string) error { func getInputFiles(inputDir string) ([]string, error) { filenames := make([]string, 0, 1) - files, err := ioutil.ReadDir(inputDir) + files, err := os.ReadDir(inputDir) if err != nil { log.Panicln("readInputDirError", err) return filenames, err @@ -157,7 +156,7 @@ func combineFiles(filenames []string, inputDir string) (string, error) { } filePath := inputDir + name - fileTxt, err := ioutil.ReadFile(filePath) + fileTxt, err := os.ReadFile(filePath) if err != nil { return "", err } @@ -189,7 +188,7 @@ func makeMini(notMinifiedString, fileType string) (string, error) { } func writeOutputFile(outputText, outputFile string) error { - err := ioutil.WriteFile(outputFile, []byte(outputText), 0644) + err := os.WriteFile(outputFile, []byte(outputText), 0644) if err != nil { return err } diff --git a/plugin.go b/plugin.go index 432815e..7526cc2 100644 --- a/plugin.go +++ b/plugin.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "go/format" - "io/ioutil" "os" "strings" "text/template" @@ -18,20 +17,20 @@ func getPluginTemplate(modulePath, pluginName string) { PluginName: pluginName, PluginTitle: strings.Title(pluginName), } - checkError(ioutil.WriteFile("./"+pluginName+".go", parsePluginTmpl("main", data), 0644)) - checkError(ioutil.WriteFile("./router.go", parsePluginTmpl("router", data), 0644)) + checkError(os.WriteFile("./"+pluginName+".go", parsePluginTmpl("main", data), 0644)) + checkError(os.WriteFile("./router.go", parsePluginTmpl("router", data), 0644)) checkError(os.Mkdir("controller", os.ModePerm)) - checkError(ioutil.WriteFile("./controller/common.go", parsePluginTmpl("controller", data), 0644)) - checkError(ioutil.WriteFile("./controller/example.go", parsePluginTmpl("controller_example", data), 0644)) + checkError(os.WriteFile("./controller/common.go", parsePluginTmpl("controller", data), 0644)) + checkError(os.WriteFile("./controller/example.go", parsePluginTmpl("controller_example", data), 0644)) checkError(os.Mkdir("guard", os.ModePerm)) - checkError(ioutil.WriteFile("./guard/guard.go", parsePluginTmpl("guard", data), 0644)) - checkError(ioutil.WriteFile("./guard/example.go", parsePluginTmpl("guard_example", data), 0644)) + checkError(os.WriteFile("./guard/guard.go", parsePluginTmpl("guard", data), 0644)) + checkError(os.WriteFile("./guard/example.go", parsePluginTmpl("guard_example", data), 0644)) checkError(os.Mkdir("modules", os.ModePerm)) checkError(os.Mkdir("./modules/language", os.ModePerm)) - checkError(ioutil.WriteFile("./modules/language/language.go", parsePluginTmpl("language", data), 0644)) - checkError(ioutil.WriteFile("./modules/language/cn.go", parsePluginTmpl("language_cn", data), 0644)) - checkError(ioutil.WriteFile("./modules/language/en.go", parsePluginTmpl("language_en", data), 0644)) - checkError(ioutil.WriteFile("./Makefile", []byte(pluginTemplate["makefile"]), 0644)) + checkError(os.WriteFile("./modules/language/language.go", parsePluginTmpl("language", data), 0644)) + checkError(os.WriteFile("./modules/language/cn.go", parsePluginTmpl("language_cn", data), 0644)) + checkError(os.WriteFile("./modules/language/en.go", parsePluginTmpl("language_en", data), 0644)) + checkError(os.WriteFile("./Makefile", []byte(pluginTemplate["makefile"]), 0644)) checkError(os.Mkdir("example", os.ModePerm)) checkError(os.Mkdir("tests", os.ModePerm)) fmt.Println() diff --git a/project.go b/project.go index eb3c4d0..7bdea25 100644 --- a/project.go +++ b/project.go @@ -246,6 +246,9 @@ var installationTmplFS embed.FS func installProjectTmpl(p Project, cfg *config.Config, cfgFile string, info *dbInfo) { + cfg.Logger.Encoder = defaultEncoderCfg + cfg.Logger.Rotate = defaultRotateCfg + mkdirs([]string{"pages", "tables", "logs", "uploads", "html", "build"}) mkEmptyFiles([]string{"./logs/access.log", "./logs/info.log", "./logs/error.log"}) @@ -278,3 +281,25 @@ func installProjectTmpl(p Project, cfg *config.Config, cfgFile string, info *dbI writeFilesOfInstallation(*fileConfigs) } + +var ( + defaultEncoderCfg = config.EncoderCfg{ + TimeKey: "ts", + LevelKey: "level", + NameKey: "logger", + CallerKey: "caller", + MessageKey: "msg", + StacktraceKey: "stacktrace", + Level: "capitalColor", + Time: "ISO8601", + Duration: "seconds", + Caller: "short", + Encoding: "console", + } + defaultRotateCfg = config.RotateCfg{ + MaxSize: 10, + MaxBackups: 5, + MaxAge: 30, + Compress: false, + } +) diff --git a/project_web.go b/project_web.go index 8c1e34f..b01017c 100644 --- a/project_web.go +++ b/project_web.go @@ -183,14 +183,14 @@ func buildProjectWeb(port string) { if info.DriverName != db.DriverSqlite { dbList = map[string]config.Database{ "default": { - Host: info.Host, - Port: info.Port, - User: info.User, - Pwd: info.Password, - Name: info.Database, - MaxIdleCon: 5, - MaxOpenCon: 10, - Driver: info.DriverName, + Host: info.Host, + Port: info.Port, + User: info.User, + Pwd: info.Password, + Name: info.Database, + MaxIdleConns: 5, + MaxOpenConns: 10, + Driver: info.DriverName, }, } } else { diff --git a/template.go b/template.go index 62f7ef5..c453d7b 100644 --- a/template.go +++ b/template.go @@ -4,7 +4,6 @@ import ( "archive/zip" "fmt" "io" - "io/ioutil" "net/http" "os" "path/filepath" @@ -116,19 +115,19 @@ func unzipDir(src, dest string) error { } func replaceContents(fileDir, moduleName, themeName string) { - files, err := ioutil.ReadDir(fileDir) + files, err := os.ReadDir(fileDir) checkError(err) for _, file := range files { path := fileDir + "/" + file.Name() if !file.IsDir() { - buf, err := ioutil.ReadFile(path) + buf, err := os.ReadFile(path) checkError(err) content := string(buf) newContent := utils.ReplaceAll(content, "github.com/GoAdminGroup/themes/adminlte", moduleName, "adminlte", themeName, "Adminlte", strings.Title(themeName)) - checkError(ioutil.WriteFile(path, []byte(newContent), 0)) + checkError(os.WriteFile(path, []byte(newContent), 0)) } } } diff --git a/templates/installation/config.yml/cn.tmpl b/templates/installation/config.yml/cn.tmpl index bcd97a0..31cba54 100644 --- a/templates/installation/config.yml/cn.tmpl +++ b/templates/installation/config.yml/cn.tmpl @@ -164,4 +164,43 @@ operation_log_off: {{.OperationLogOff}} # show_create: /info/:__prefix/new # update: /update/:__prefix +# Logger配置用于设置日志记录器的行为和设置。 +# 更多细节:https://pkg.go.dev/go.uber.org/zap +logger: + # Encoder配置用于设置日志编码器的行为和设置。 + encoder: + # 时间键 + time_key: {{.Logger.Encoder.TimeKey}} + # 级别键 + level_key: {{.Logger.Encoder.LevelKey}} + # 名称键 + name_key: {{.Logger.Encoder.NameKey}} + # 调用者键 + caller_key: {{.Logger.Encoder.CallerKey}} + # 消息键 + message_key: {{.Logger.Encoder.MessageKey}} + # 堆栈跟踪键 + stacktrace_key: {{.Logger.Encoder.StacktraceKey}} + # 级别格式 + level: {{.Logger.Encoder.Level}} + # 时间格式 + time: {{.Logger.Encoder.Time}} + # 间隔 + duration: {{.Logger.Encoder.Duration}} + # 调用者格式 + caller: {{.Logger.Encoder.Caller}} + # 编码格式 console/json + encoding: {{.Logger.Encoder.Encoding}} + + # Rotate配置用于设置日志轮转的行为和设置。 + rotate: + # 最大保存时间 + max_age: {{.Logger.Rotate.MaxSize}} + # 最大保存数量 + max_backups: {{.Logger.Rotate.MaxBackups}} + # 最大大小 + max_size: {{.Logger.Rotate.MaxAge}} + # 压缩 + compress: {{.Logger.Rotate.Compress}} + {{end}} \ No newline at end of file diff --git a/templates/installation/config.yml/en.tmpl b/templates/installation/config.yml/en.tmpl index 645a6c6..d36f896 100644 --- a/templates/installation/config.yml/en.tmpl +++ b/templates/installation/config.yml/en.tmpl @@ -172,4 +172,43 @@ operation_log_off: {{.OperationLogOff}} # show_create: /info/:__prefix/new # update: /update/:__prefix +# Logger configuration is used to set the behavior and settings of the logger. +# More details: https://pkg.go.dev/go.uber.org/zap +logger: +# Encoder configuration is used to set the behavior and settings of the log encoder. + encoder: + # Time key + time_key: {{.Logger.Encoder.TimeKey}} + # Level key + level_key: {{.Logger.Encoder.LevelKey}} + # Name key + name_key: {{.Logger.Encoder.NameKey}} + # Caller key + caller_key: {{.Logger.Encoder.CallerKey}} + # Message key + message_key: {{.Logger.Encoder.MessageKey}} + # Stacktrace key + stacktrace_key: {{.Logger.Encoder.StacktraceKey}} + # Level format + level: {{.Logger.Encoder.Level}} + # Time format + time: {{.Logger.Encoder.Time}} + # Duration + duration: {{.Logger.Encoder.Duration}} + # Caller format + caller: {{.Logger.Encoder.Caller}} + # Encoding format (console/json) + encoding: {{.Logger.Encoder.Encoding}} + + # Rotate configuration is used to set the behavior and settings of log rotation. + rotate: + # Maximum age to retain old log files + max_age: {{.Logger.Rotate.MaxSize}} + # Maximum number of old log files to retain + max_backups: {{.Logger.Rotate.MaxBackups}} + # Maximum size of a log file before it's rotated + max_size: {{.Logger.Rotate.MaxAge}} + # Compression setting for rotated log files + compress: {{.Logger.Rotate.Compress}} + {{end}} \ No newline at end of file