Răsfoiți Sursa

Refactor installation and database initialization.
Added optional database name to installation.

Hintay 3 ani în urmă
părinte
comite
3bf2677d69

+ 1 - 1
frontend/src/layouts/FooterLayout.vue

@@ -1,6 +1,6 @@
 <template>
     <div class="footer center">
-        Copyright © 2020 - {{ thisYear }} 0xJacky
+        Copyright © 2020 - {{ thisYear }} Nginx UI
     </div>
 </template>
 

+ 2 - 1
frontend/src/views/other/About.vue

@@ -8,6 +8,7 @@
         <p>Version: {{ version }} ({{ build_id }})</p>
         <h3>项目组</h3>
         <p>Designer:<a href="https://jackyu.cn/">@0xJacky</a></p>
+        <p><a href="https://blog.kugeek.com/">@Hintay</a></p>
         <h3>技术栈</h3>
         <p>Go</p>
         <p>Gin</p>
@@ -15,7 +16,7 @@
         <p>Websocket</p>
         <h3>开源协议</h3>
         <p>GNU General Public License v2.0</p>
-        <p>Copyright © 2020 - {{ this_year }} 0xJacky </p>
+        <p>Copyright © 2020 - {{ this_year }} Nginx UI </p>
     </a-card>
 </template>
 

+ 15 - 4
frontend/src/views/other/Install.vue

@@ -22,7 +22,7 @@
                 message: 'Please input your E-mail!',
               },] },
         ]"
-                    placeholder="Email"
+                    placeholder="Email (*)"
                 >
                     <a-icon slot="prefix" type="mail" style="color: rgba(0,0,0,.25)"/>
                 </a-input>
@@ -33,7 +33,7 @@
           'username',
           { rules: [{ required: true, message: 'Please input your username!' }] },
         ]"
-                    placeholder="Username"
+                    placeholder="Username (*)"
                 >
                     <a-icon slot="prefix" type="user" style="color: rgba(0,0,0,.25)"/>
                 </a-input>
@@ -45,11 +45,22 @@
           { rules: [{ required: true, message: 'Please input your Password!' }] },
         ]"
                     type="password"
-                    placeholder="Password"
+                    placeholder="Password (*)"
                 >
                     <a-icon slot="prefix" type="lock" style="color: rgba(0,0,0,.25)"/>
                 </a-input>
             </a-form-item>
+            <a-form-item>
+                <a-input
+                    v-decorator="[
+          'database',
+          { rules: [{ pattern: /^[^\\/:*?\x22<>|]{1,120}$/, message: 'Please input a legal file name!'}] },
+        ]"
+                    placeholder="Database (Optional, default: database)"
+                >
+                    <a-icon slot="prefix" type="database" style="color: rgba(0,0,0,.25)"/>
+                </a-input>
+            </a-form-item>
             <a-form-item>
                 <a-button type="primary" :block="true" html-type="submit" :loading="loading">
                     安装
@@ -57,7 +68,7 @@
             </a-form-item>
         </a-form>
         <footer>
-            Copyright © 2020 - {{ thisYear }} 0xJacky
+            Copyright © 2020 - {{ thisYear }} Nginx UI
         </footer>
     </div>
 

+ 1 - 1
frontend/src/views/other/Login.vue

@@ -39,7 +39,7 @@
                 </a-form-item>
             </a-form>
             <div class="footer">
-                Copyright © 2020 - {{ thisYear }} 0xJacky
+                Copyright © 2020 - {{ thisYear }} Nginx UI
             </div>
         </div>
     </div>

+ 1 - 1
frontend/version.json

@@ -1 +1 @@
-{"version":"1.1.0","build_id":4,"total_build":21}
+{"version":"1.1.0","build_id":7,"total_build":24}

+ 6 - 6
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() {

+ 17 - 12
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),
 	})

+ 27 - 7
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 {