mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 02:15:48 +02:00
chore: clean up code
This commit is contained in:
parent
3cdc675e4f
commit
0f259e4331
9 changed files with 92 additions and 121 deletions
|
@ -193,7 +193,12 @@ function add_server() {
|
|||
}
|
||||
|
||||
function remove_server(index: number) {
|
||||
props.ngx_config?.servers?.splice(index, 1)
|
||||
Modal.confirm({
|
||||
title: $gettext('Do you want to remove this server?'),
|
||||
mask: false,
|
||||
centered: true,
|
||||
onOk: () => props.ngx_config?.servers?.splice(index, 1)
|
||||
})
|
||||
}
|
||||
</script>
|
||||
|
||||
|
|
|
@ -1,75 +1,76 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"github.com/0xJacky/Nginx-UI/server/model"
|
||||
"github.com/0xJacky/Nginx-UI/server/query"
|
||||
"github.com/0xJacky/Nginx-UI/server/settings"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"net/http"
|
||||
"github.com/0xJacky/Nginx-UI/server/model"
|
||||
"github.com/0xJacky/Nginx-UI/server/query"
|
||||
"github.com/0xJacky/Nginx-UI/server/settings"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func installLockStatus() bool {
|
||||
return "" != settings.ServerSettings.JwtSecret
|
||||
return "" != settings.ServerSettings.JwtSecret
|
||||
}
|
||||
|
||||
func InstallLockCheck(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"lock": installLockStatus(),
|
||||
})
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"lock": installLockStatus(),
|
||||
})
|
||||
}
|
||||
|
||||
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"`
|
||||
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) {
|
||||
// Visit this api after installed is forbidden
|
||||
if installLockStatus() {
|
||||
c.JSON(http.StatusForbidden, gin.H{
|
||||
"error": "installed",
|
||||
})
|
||||
return
|
||||
}
|
||||
var json InstallJson
|
||||
ok := BindAndValid(c, &json)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
// Visit this api after installed is forbidden
|
||||
if installLockStatus() {
|
||||
c.JSON(http.StatusForbidden, gin.H{
|
||||
"error": "installed",
|
||||
})
|
||||
return
|
||||
}
|
||||
var json InstallJson
|
||||
ok := BindAndValid(c, &json)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
settings.ServerSettings.JwtSecret = uuid.New().String()
|
||||
settings.ServerSettings.Email = json.Email
|
||||
if "" != json.Database {
|
||||
settings.ServerSettings.Database = json.Database
|
||||
}
|
||||
settings.ReflectFrom()
|
||||
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
|
||||
}
|
||||
err := settings.Save()
|
||||
if err != nil {
|
||||
ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
// Init model
|
||||
db := model.Init()
|
||||
query.Init(db)
|
||||
// Init model
|
||||
db := model.Init()
|
||||
query.Init(db)
|
||||
|
||||
curd := model.NewCurd(&model.Auth{})
|
||||
pwd, _ := bcrypt.GenerateFromPassword([]byte(json.Password), bcrypt.DefaultCost)
|
||||
err = curd.Add(&model.Auth{
|
||||
Name: json.Username,
|
||||
Password: string(pwd),
|
||||
})
|
||||
pwd, _ := bcrypt.GenerateFromPassword([]byte(json.Password), bcrypt.DefaultCost)
|
||||
|
||||
if err != nil {
|
||||
ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "ok",
|
||||
})
|
||||
u := query.Auth
|
||||
err = u.Create(&model.Auth{
|
||||
Name: json.Username,
|
||||
Password: string(pwd),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "ok",
|
||||
})
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package api
|
|||
|
||||
import (
|
||||
"github.com/0xJacky/Nginx-UI/server/model"
|
||||
"github.com/0xJacky/Nginx-UI/server/query"
|
||||
"github.com/0xJacky/Nginx-UI/server/settings"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/spf13/cast"
|
||||
|
@ -16,16 +17,17 @@ func GetUsers(c *gin.Context) {
|
|||
}
|
||||
|
||||
func GetUser(c *gin.Context) {
|
||||
curd := model.NewCurd(&model.Auth{})
|
||||
id := c.Param("id")
|
||||
id := cast.ToInt(c.Param("id"))
|
||||
|
||||
var user model.Auth
|
||||
err := curd.First(&user, id)
|
||||
u := query.Auth
|
||||
|
||||
user, err := u.FirstByID(id)
|
||||
|
||||
if err != nil {
|
||||
ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, user)
|
||||
}
|
||||
|
||||
|
@ -40,7 +42,8 @@ func AddUser(c *gin.Context) {
|
|||
if !ok {
|
||||
return
|
||||
}
|
||||
curd := model.NewCurd(&model.Auth{})
|
||||
|
||||
u := query.Auth
|
||||
|
||||
pwd, err := bcrypt.GenerateFromPassword([]byte(json.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
|
@ -54,7 +57,7 @@ func AddUser(c *gin.Context) {
|
|||
Password: json.Password,
|
||||
}
|
||||
|
||||
err = curd.Add(&user)
|
||||
err = u.Create(&user)
|
||||
|
||||
if err != nil {
|
||||
ErrHandler(c, err)
|
||||
|
@ -80,17 +83,17 @@ func EditUser(c *gin.Context) {
|
|||
if !ok {
|
||||
return
|
||||
}
|
||||
curd := model.NewCurd(&model.Auth{})
|
||||
|
||||
var user, edit model.Auth
|
||||
|
||||
err := curd.First(&user, userId)
|
||||
u := query.Auth
|
||||
user, err := u.FirstByID(userId)
|
||||
|
||||
if err != nil {
|
||||
ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
edit.Name = json.Name
|
||||
edit := &model.Auth{
|
||||
Name: json.Name,
|
||||
}
|
||||
|
||||
// encrypt password
|
||||
if json.Password != "" {
|
||||
|
@ -103,7 +106,7 @@ func EditUser(c *gin.Context) {
|
|||
edit.Password = string(pwd)
|
||||
}
|
||||
|
||||
err = curd.Edit(&user, &edit)
|
||||
_, err = u.Where(u.ID.Eq(userId)).Updates(&edit)
|
||||
|
||||
if err != nil {
|
||||
ErrHandler(c, err)
|
||||
|
@ -114,7 +117,7 @@ func EditUser(c *gin.Context) {
|
|||
}
|
||||
|
||||
func DeleteUser(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
id := cast.ToInt(c.Param("id"))
|
||||
|
||||
if cast.ToInt(id) == 1 {
|
||||
c.JSON(http.StatusNotAcceptable, gin.H{
|
||||
|
@ -123,8 +126,8 @@ func DeleteUser(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
curd := model.NewCurd(&model.Auth{})
|
||||
err := curd.Delete(&model.Auth{}, "id", id)
|
||||
u := query.Auth
|
||||
err := u.DeleteByID(id)
|
||||
if err != nil {
|
||||
ErrHandler(c, err)
|
||||
return
|
||||
|
|
|
@ -1,38 +0,0 @@
|
|||
package model
|
||||
|
||||
type Curd struct {
|
||||
Model interface{}
|
||||
}
|
||||
|
||||
func NewCurd(Model interface{}) *Curd {
|
||||
return &Curd{Model: Model}
|
||||
}
|
||||
|
||||
func (c *Curd) GetList(dest interface{}) (err error) {
|
||||
err = db.Model(c.Model).Scan(dest).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Curd) First(dest interface{}, conds ...interface{}) (err error) {
|
||||
err = db.Model(c.Model).First(dest, conds).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Curd) Add(value interface{}) (err error) {
|
||||
err = db.Model(c.Model).Create(value).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = db.Find(value).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Curd) Edit(orig interface{}, new interface{}) (err error) {
|
||||
err = db.Model(orig).Updates(new).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Curd) Delete(value interface{}, conds ...interface{}) (err error) {
|
||||
err = db.Model(c.Model).Delete(value, conds).Error
|
||||
return
|
||||
}
|
|
@ -17,7 +17,7 @@ import (
|
|||
var db *gorm.DB
|
||||
|
||||
type Model struct {
|
||||
ID uint `gorm:"primary_key" json:"id"`
|
||||
ID int `gorm:"primary_key" json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt *time.Time `gorm:"index" json:"deleted_at"`
|
||||
|
|
|
@ -28,7 +28,7 @@ func newAuth(db *gorm.DB, opts ...gen.DOOption) auth {
|
|||
|
||||
tableName := _auth.authDo.TableName()
|
||||
_auth.ALL = field.NewAsterisk(tableName)
|
||||
_auth.ID = field.NewUint(tableName, "id")
|
||||
_auth.ID = field.NewInt(tableName, "id")
|
||||
_auth.CreatedAt = field.NewTime(tableName, "created_at")
|
||||
_auth.UpdatedAt = field.NewTime(tableName, "updated_at")
|
||||
_auth.DeletedAt = field.NewTime(tableName, "deleted_at")
|
||||
|
@ -44,7 +44,7 @@ type auth struct {
|
|||
authDo
|
||||
|
||||
ALL field.Asterisk
|
||||
ID field.Uint
|
||||
ID field.Int
|
||||
CreatedAt field.Time
|
||||
UpdatedAt field.Time
|
||||
DeletedAt field.Time
|
||||
|
@ -66,7 +66,7 @@ func (a auth) As(alias string) *auth {
|
|||
|
||||
func (a *auth) updateTableName(table string) *auth {
|
||||
a.ALL = field.NewAsterisk(table)
|
||||
a.ID = field.NewUint(table, "id")
|
||||
a.ID = field.NewInt(table, "id")
|
||||
a.CreatedAt = field.NewTime(table, "created_at")
|
||||
a.UpdatedAt = field.NewTime(table, "updated_at")
|
||||
a.DeletedAt = field.NewTime(table, "deleted_at")
|
||||
|
|
|
@ -28,7 +28,7 @@ func newCert(db *gorm.DB, opts ...gen.DOOption) cert {
|
|||
|
||||
tableName := _cert.certDo.TableName()
|
||||
_cert.ALL = field.NewAsterisk(tableName)
|
||||
_cert.ID = field.NewUint(tableName, "id")
|
||||
_cert.ID = field.NewInt(tableName, "id")
|
||||
_cert.CreatedAt = field.NewTime(tableName, "created_at")
|
||||
_cert.UpdatedAt = field.NewTime(tableName, "updated_at")
|
||||
_cert.DeletedAt = field.NewTime(tableName, "deleted_at")
|
||||
|
@ -49,7 +49,7 @@ type cert struct {
|
|||
certDo
|
||||
|
||||
ALL field.Asterisk
|
||||
ID field.Uint
|
||||
ID field.Int
|
||||
CreatedAt field.Time
|
||||
UpdatedAt field.Time
|
||||
DeletedAt field.Time
|
||||
|
@ -76,7 +76,7 @@ func (c cert) As(alias string) *cert {
|
|||
|
||||
func (c *cert) updateTableName(table string) *cert {
|
||||
c.ALL = field.NewAsterisk(table)
|
||||
c.ID = field.NewUint(table, "id")
|
||||
c.ID = field.NewInt(table, "id")
|
||||
c.CreatedAt = field.NewTime(table, "created_at")
|
||||
c.UpdatedAt = field.NewTime(table, "updated_at")
|
||||
c.DeletedAt = field.NewTime(table, "deleted_at")
|
||||
|
|
|
@ -28,7 +28,7 @@ func newConfigBackup(db *gorm.DB, opts ...gen.DOOption) configBackup {
|
|||
|
||||
tableName := _configBackup.configBackupDo.TableName()
|
||||
_configBackup.ALL = field.NewAsterisk(tableName)
|
||||
_configBackup.ID = field.NewUint(tableName, "id")
|
||||
_configBackup.ID = field.NewInt(tableName, "id")
|
||||
_configBackup.CreatedAt = field.NewTime(tableName, "created_at")
|
||||
_configBackup.UpdatedAt = field.NewTime(tableName, "updated_at")
|
||||
_configBackup.DeletedAt = field.NewTime(tableName, "deleted_at")
|
||||
|
@ -45,7 +45,7 @@ type configBackup struct {
|
|||
configBackupDo
|
||||
|
||||
ALL field.Asterisk
|
||||
ID field.Uint
|
||||
ID field.Int
|
||||
CreatedAt field.Time
|
||||
UpdatedAt field.Time
|
||||
DeletedAt field.Time
|
||||
|
@ -68,7 +68,7 @@ func (c configBackup) As(alias string) *configBackup {
|
|||
|
||||
func (c *configBackup) updateTableName(table string) *configBackup {
|
||||
c.ALL = field.NewAsterisk(table)
|
||||
c.ID = field.NewUint(table, "id")
|
||||
c.ID = field.NewInt(table, "id")
|
||||
c.CreatedAt = field.NewTime(table, "created_at")
|
||||
c.UpdatedAt = field.NewTime(table, "updated_at")
|
||||
c.DeletedAt = field.NewTime(table, "deleted_at")
|
||||
|
|
|
@ -28,7 +28,7 @@ func newSite(db *gorm.DB, opts ...gen.DOOption) site {
|
|||
|
||||
tableName := _site.siteDo.TableName()
|
||||
_site.ALL = field.NewAsterisk(tableName)
|
||||
_site.ID = field.NewUint(tableName, "id")
|
||||
_site.ID = field.NewInt(tableName, "id")
|
||||
_site.CreatedAt = field.NewTime(tableName, "created_at")
|
||||
_site.UpdatedAt = field.NewTime(tableName, "updated_at")
|
||||
_site.DeletedAt = field.NewTime(tableName, "deleted_at")
|
||||
|
@ -44,7 +44,7 @@ type site struct {
|
|||
siteDo
|
||||
|
||||
ALL field.Asterisk
|
||||
ID field.Uint
|
||||
ID field.Int
|
||||
CreatedAt field.Time
|
||||
UpdatedAt field.Time
|
||||
DeletedAt field.Time
|
||||
|
@ -66,7 +66,7 @@ func (s site) As(alias string) *site {
|
|||
|
||||
func (s *site) updateTableName(table string) *site {
|
||||
s.ALL = field.NewAsterisk(table)
|
||||
s.ID = field.NewUint(table, "id")
|
||||
s.ID = field.NewInt(table, "id")
|
||||
s.CreatedAt = field.NewTime(table, "created_at")
|
||||
s.UpdatedAt = field.NewTime(table, "updated_at")
|
||||
s.DeletedAt = field.NewTime(table, "deleted_at")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue