From 3bf2677d69e542001778dac3fc988d67c1cce708 Mon Sep 17 00:00:00 2001 From: Hintay Date: Sat, 19 Feb 2022 03:02:06 +0800 Subject: [PATCH] Refactor installation and database initialization. Added optional database name to installation. --- frontend/src/layouts/FooterLayout.vue | 2 +- frontend/src/views/other/About.vue | 3 ++- frontend/src/views/other/Install.vue | 19 +++++++++++---- frontend/src/views/other/Login.vue | 2 +- frontend/version.json | 2 +- main.go | 12 +++++----- server/api/install.go | 29 +++++++++++++---------- server/settings/settings.go | 34 +++++++++++++++++++++------ 8 files changed, 70 insertions(+), 33 deletions(-) diff --git a/frontend/src/layouts/FooterLayout.vue b/frontend/src/layouts/FooterLayout.vue index 398b2ce5..5fcdfd04 100644 --- a/frontend/src/layouts/FooterLayout.vue +++ b/frontend/src/layouts/FooterLayout.vue @@ -1,6 +1,6 @@ diff --git a/frontend/src/views/other/About.vue b/frontend/src/views/other/About.vue index 4bac41d7..b91cc3c0 100644 --- a/frontend/src/views/other/About.vue +++ b/frontend/src/views/other/About.vue @@ -8,6 +8,7 @@

Version: {{ version }} ({{ build_id }})

项目组

Designer:@0xJacky

+

@Hintay

技术栈

Go

Gin

@@ -15,7 +16,7 @@

Websocket

开源协议

GNU General Public License v2.0

-

Copyright © 2020 - {{ this_year }} 0xJacky

+

Copyright © 2020 - {{ this_year }} Nginx UI

diff --git a/frontend/src/views/other/Install.vue b/frontend/src/views/other/Install.vue index a88144f1..a0db6909 100644 --- a/frontend/src/views/other/Install.vue +++ b/frontend/src/views/other/Install.vue @@ -22,7 +22,7 @@ message: 'Please input your E-mail!', },] }, ]" - placeholder="Email" + placeholder="Email (*)" > @@ -33,7 +33,7 @@ 'username', { rules: [{ required: true, message: 'Please input your username!' }] }, ]" - placeholder="Username" + placeholder="Username (*)" > @@ -45,11 +45,22 @@ { rules: [{ required: true, message: 'Please input your Password!' }] }, ]" type="password" - placeholder="Password" + placeholder="Password (*)" > + + + + + 安装 @@ -57,7 +68,7 @@
- Copyright © 2020 - {{ thisYear }} 0xJacky + Copyright © 2020 - {{ thisYear }} Nginx UI
diff --git a/frontend/src/views/other/Login.vue b/frontend/src/views/other/Login.vue index 3ae145e0..4312e141 100644 --- a/frontend/src/views/other/Login.vue +++ b/frontend/src/views/other/Login.vue @@ -39,7 +39,7 @@ diff --git a/frontend/version.json b/frontend/version.json index 67c23b80..eb7b4a3a 100644 --- a/frontend/version.json +++ b/frontend/version.json @@ -1 +1 @@ -{"version":"1.1.0","build_id":4,"total_build":21} \ No newline at end of file +{"version":"1.1.0","build_id":7,"total_build":24} \ No newline at end of file diff --git a/main.go b/main.go index 0536dfff..9954b570 100644 --- a/main.go +++ b/main.go @@ -25,21 +25,21 @@ func main() { _ = mime.AddExtensionType(".js", "text/javascript; charset=utf-8") var confPath string - flag.StringVar(&confPath, "config", "./app.ini", "Specify the configuration file") + flag.StringVar(&confPath, "config", "app.ini", "Specify the configuration file") flag.Parse() settings.Init(confPath) - model.Init() + log.Printf("nginx config dir path: %s", tool2.GetNginxConfPath("")) + if "" != settings.ServerSettings.JwtSecret { + model.Init() + go tool2.AutoCert() + } srv := &http.Server{ Addr: ":" + settings.ServerSettings.HttpPort, Handler: router.InitRouter(), } - log.Printf("nginx config dir path: %s", tool2.GetNginxConfPath("")) - - go tool2.AutoCert() - // Initializing the server in a goroutine so that // it won't block the graceful shutdown handling below go func() { diff --git a/server/api/install.go b/server/api/install.go index 3883781b..06c6f76d 100644 --- a/server/api/install.go +++ b/server/api/install.go @@ -1,21 +1,17 @@ package api import ( - model2 "github.com/0xJacky/Nginx-UI/server/model" + "github.com/0xJacky/Nginx-UI/server/model" "github.com/0xJacky/Nginx-UI/server/settings" + "github.com/0xJacky/Nginx-UI/server/tool" "github.com/gin-gonic/gin" "github.com/google/uuid" "golang.org/x/crypto/bcrypt" "net/http" - "os" ) func installLockStatus() bool { - lockPath := settings.ConfPath - _, err := os.Stat(lockPath) - - return !os.IsNotExist(err) - + return "" != settings.ServerSettings.JwtSecret } func InstallLockCheck(c *gin.Context) { @@ -28,6 +24,7 @@ type InstallJson struct { Email string `json:"email" binding:"required,email"` Username string `json:"username" binding:"required,max=255"` Password string `json:"password" binding:"required,max=255"` + Database string `json:"database"` } func InstallNginxUI(c *gin.Context) { @@ -44,18 +41,26 @@ func InstallNginxUI(c *gin.Context) { return } - serverSettings := settings.Conf.Section("server") - serverSettings.Key("JwtSecret").SetValue(uuid.New().String()) - serverSettings.Key("Email").SetValue(json.Email) + settings.ServerSettings.JwtSecret = uuid.New().String() + settings.ServerSettings.Email = json.Email + if "" != json.Database { + settings.ServerSettings.Database = json.Database + } + settings.ReflectFrom() + err := settings.Save() if err != nil { ErrHandler(c, err) return } - curd := model2.NewCurd(&model2.Auth{}) + // Init model and auto cert + model.Init() + go tool.AutoCert() + + curd := model.NewCurd(&model.Auth{}) pwd, _ := bcrypt.GenerateFromPassword([]byte(json.Password), bcrypt.DefaultCost) - err = curd.Add(&model2.Auth{ + err = curd.Add(&model.Auth{ Name: json.Username, Password: string(pwd), }) diff --git a/server/settings/settings.go b/server/settings/settings.go index b59e50d2..ba0ad6e7 100644 --- a/server/settings/settings.go +++ b/server/settings/settings.go @@ -3,7 +3,6 @@ package settings import ( "gopkg.in/ini.v1" "log" - "os" ) var Conf *ini.File @@ -27,21 +26,35 @@ var ServerSettings = &Server{ var ConfPath string +var sections = map[string]interface{}{ + "server": ServerSettings, +} + func Init(confPath string) { ConfPath = confPath - if _, err := os.Stat(ConfPath); os.IsExist(err) { - Setup() - } + Setup() } func Setup() { var err error - Conf, err = ini.Load(ConfPath) + Conf, err = ini.LooseLoad(ConfPath) if err != nil { - log.Fatalf("setting.Setup, fail to parse '%s': %v", ConfPath, err) + log.Printf("setting.Setup: %v", err) + } else { + MapTo() } +} - mapTo("server", ServerSettings) +func MapTo() { + for k, v := range sections { + mapTo(k, v) + } +} + +func ReflectFrom() { + for k, v := range sections { + reflectFrom(k, v) + } } func mapTo(section string, v interface{}) { @@ -51,6 +64,13 @@ func mapTo(section string, v interface{}) { } } +func reflectFrom(section string, v interface{}) { + err := Conf.Section(section).ReflectFrom(v) + if err != nil { + log.Fatalf("Cfg.ReflectFrom %s err: %v", section, err) + } +} + func Save() (err error) { err = Conf.SaveTo(ConfPath) if err != nil {