Refactor installation and database initialization.

Added optional database name to installation.
This commit is contained in:
Hintay 2022-02-19 03:02:06 +08:00
parent 759a235e7e
commit 3bf2677d69
No known key found for this signature in database
GPG key ID: 120FC7FF121F2F2D
8 changed files with 70 additions and 33 deletions

View file

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

View file

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

View file

@ -22,7 +22,7 @@
message: 'Please input your E-mail!', message: 'Please input your E-mail!',
},] }, },] },
]" ]"
placeholder="Email" placeholder="Email (*)"
> >
<a-icon slot="prefix" type="mail" style="color: rgba(0,0,0,.25)"/> <a-icon slot="prefix" type="mail" style="color: rgba(0,0,0,.25)"/>
</a-input> </a-input>
@ -33,7 +33,7 @@
'username', 'username',
{ rules: [{ required: true, message: 'Please input your 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-icon slot="prefix" type="user" style="color: rgba(0,0,0,.25)"/>
</a-input> </a-input>
@ -45,11 +45,22 @@
{ rules: [{ required: true, message: 'Please input your Password!' }] }, { rules: [{ required: true, message: 'Please input your Password!' }] },
]" ]"
type="password" type="password"
placeholder="Password" placeholder="Password (*)"
> >
<a-icon slot="prefix" type="lock" style="color: rgba(0,0,0,.25)"/> <a-icon slot="prefix" type="lock" style="color: rgba(0,0,0,.25)"/>
</a-input> </a-input>
</a-form-item> </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-form-item>
<a-button type="primary" :block="true" html-type="submit" :loading="loading"> <a-button type="primary" :block="true" html-type="submit" :loading="loading">
安装 安装
@ -57,7 +68,7 @@
</a-form-item> </a-form-item>
</a-form> </a-form>
<footer> <footer>
Copyright © 2020 - {{ thisYear }} 0xJacky Copyright © 2020 - {{ thisYear }} Nginx UI
</footer> </footer>
</div> </div>

View file

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

View file

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

12
main.go
View file

@ -25,21 +25,21 @@ func main() {
_ = mime.AddExtensionType(".js", "text/javascript; charset=utf-8") _ = mime.AddExtensionType(".js", "text/javascript; charset=utf-8")
var confPath string 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() flag.Parse()
settings.Init(confPath) 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{ srv := &http.Server{
Addr: ":" + settings.ServerSettings.HttpPort, Addr: ":" + settings.ServerSettings.HttpPort,
Handler: router.InitRouter(), Handler: router.InitRouter(),
} }
log.Printf("nginx config dir path: %s", tool2.GetNginxConfPath(""))
go tool2.AutoCert()
// Initializing the server in a goroutine so that // Initializing the server in a goroutine so that
// it won't block the graceful shutdown handling below // it won't block the graceful shutdown handling below
go func() { go func() {

View file

@ -1,21 +1,17 @@
package api package api
import ( 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/settings"
"github.com/0xJacky/Nginx-UI/server/tool"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/google/uuid" "github.com/google/uuid"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
"net/http" "net/http"
"os"
) )
func installLockStatus() bool { func installLockStatus() bool {
lockPath := settings.ConfPath return "" != settings.ServerSettings.JwtSecret
_, err := os.Stat(lockPath)
return !os.IsNotExist(err)
} }
func InstallLockCheck(c *gin.Context) { func InstallLockCheck(c *gin.Context) {
@ -28,6 +24,7 @@ type InstallJson struct {
Email string `json:"email" binding:"required,email"` Email string `json:"email" binding:"required,email"`
Username string `json:"username" binding:"required,max=255"` Username string `json:"username" binding:"required,max=255"`
Password string `json:"password" binding:"required,max=255"` Password string `json:"password" binding:"required,max=255"`
Database string `json:"database"`
} }
func InstallNginxUI(c *gin.Context) { func InstallNginxUI(c *gin.Context) {
@ -44,18 +41,26 @@ func InstallNginxUI(c *gin.Context) {
return return
} }
serverSettings := settings.Conf.Section("server") settings.ServerSettings.JwtSecret = uuid.New().String()
serverSettings.Key("JwtSecret").SetValue(uuid.New().String()) settings.ServerSettings.Email = json.Email
serverSettings.Key("Email").SetValue(json.Email) if "" != json.Database {
settings.ServerSettings.Database = json.Database
}
settings.ReflectFrom()
err := settings.Save() err := settings.Save()
if err != nil { if err != nil {
ErrHandler(c, err) ErrHandler(c, err)
return 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) pwd, _ := bcrypt.GenerateFromPassword([]byte(json.Password), bcrypt.DefaultCost)
err = curd.Add(&model2.Auth{ err = curd.Add(&model.Auth{
Name: json.Username, Name: json.Username,
Password: string(pwd), Password: string(pwd),
}) })

View file

@ -3,7 +3,6 @@ package settings
import ( import (
"gopkg.in/ini.v1" "gopkg.in/ini.v1"
"log" "log"
"os"
) )
var Conf *ini.File var Conf *ini.File
@ -27,21 +26,35 @@ var ServerSettings = &Server{
var ConfPath string var ConfPath string
var sections = map[string]interface{}{
"server": ServerSettings,
}
func Init(confPath string) { func Init(confPath string) {
ConfPath = confPath ConfPath = confPath
if _, err := os.Stat(ConfPath); os.IsExist(err) { Setup()
Setup()
}
} }
func Setup() { func Setup() {
var err error var err error
Conf, err = ini.Load(ConfPath) Conf, err = ini.LooseLoad(ConfPath)
if err != nil { 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{}) { 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) { func Save() (err error) {
err = Conf.SaveTo(ConfPath) err = Conf.SaveTo(ConfPath)
if err != nil { if err != nil {