Skip to content

Commit

Permalink
Merge pull request #644 from 0xJacky/refactor/migrate-cosy
Browse files Browse the repository at this point in the history
Refactor/migrate-cosy
  • Loading branch information
0xJacky authored Oct 23, 2024
2 parents ddc0bf0 + a66b7db commit 4747c69
Show file tree
Hide file tree
Showing 177 changed files with 4,419 additions and 3,412 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/documents.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ jobs:
with:
apiToken: ${{ secrets.CF_API_TOKEN }}
accountId: ${{ secrets.CF_ACCOUNT_ID }}
command: pages deploy "docs/.vitepress/dist/" --branch=main --commit-dirty=true --project-name=nginxui
command: pages deploy "docs/.vitepress/dist/" --commit-dirty=true --project-name=nginxui
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ tmp
node_modules
.pnpm-store
app.ini
app.*.ini
dist
*.exe
*.po~
Expand All @@ -12,3 +13,5 @@ resources/development/nginx
app/.env
app/.status_hash
casdoor.pub
.idea/deployment.xml
.idea/webServers.xml
2 changes: 1 addition & 1 deletion api/analytic/analytic.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"github.com/0xJacky/Nginx-UI/internal/analytic"
"github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/internal/logger"
"github.com/uozi-tech/cosy/logger"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/host"
"github.com/shirou/gopsutil/v3/load"
Expand Down
2 changes: 1 addition & 1 deletion api/analytic/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package analytic
import (
"github.com/0xJacky/Nginx-UI/internal/analytic"
"github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/internal/logger"
"github.com/uozi-tech/cosy/logger"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"net/http"
Expand Down
2 changes: 1 addition & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package api

import (
"errors"
"github.com/0xJacky/Nginx-UI/internal/logger"
"github.com/uozi-tech/cosy/logger"
"github.com/0xJacky/Nginx-UI/model"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
Expand Down
6 changes: 3 additions & 3 deletions api/certificate/acme_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package certificate

import (
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/internal/cosy"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/0xJacky/Nginx-UI/settings"
"github.com/gin-gonic/gin"
"github.com/spf13/cast"
"github.com/uozi-tech/cosy"
"net/http"
)

Expand All @@ -31,7 +31,7 @@ func CreateAcmeUser(c *gin.Context) {
"register_on_startup": "omitempty",
}).BeforeExecuteHook(func(ctx *cosy.Ctx[model.AcmeUser]) {
if ctx.Model.CADir == "" {
ctx.Model.CADir = settings.ServerSettings.GetCADir()
ctx.Model.CADir = settings.CertSettings.GetCADir()
}
err := ctx.Model.Register()
if err != nil {
Expand All @@ -50,7 +50,7 @@ func ModifyAcmeUser(c *gin.Context) {
"register_on_startup": "omitempty",
}).BeforeExecuteHook(func(ctx *cosy.Ctx[model.AcmeUser]) {
if ctx.Model.CADir == "" {
ctx.Model.CADir = settings.ServerSettings.GetCADir()
ctx.Model.CADir = settings.CertSettings.GetCADir()
}

if ctx.OriginModel.Email != ctx.Model.Email ||
Expand Down
2 changes: 1 addition & 1 deletion api/certificate/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package certificate
import (
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/internal/cert"
"github.com/0xJacky/Nginx-UI/internal/cosy"
"github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/internal/notification"
Expand All @@ -12,6 +11,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/go-acme/lego/v4/certcrypto"
"github.com/spf13/cast"
"github.com/uozi-tech/cosy"
"net/http"
"os"
)
Expand Down
2 changes: 1 addition & 1 deletion api/certificate/dns_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package certificate
import (
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/internal/cert/dns"
"github.com/0xJacky/Nginx-UI/internal/cosy"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/gin-gonic/gin"
"github.com/spf13/cast"
"github.com/uozi-tech/cosy"
"net/http"
)

Expand Down
2 changes: 1 addition & 1 deletion api/certificate/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package certificate

import (
"github.com/0xJacky/Nginx-UI/internal/cert"
"github.com/0xJacky/Nginx-UI/internal/logger"
"github.com/uozi-tech/cosy/logger"
"github.com/0xJacky/Nginx-UI/model"
"github.com/gin-gonic/gin"
"github.com/go-acme/lego/v4/certcrypto"
Expand Down
2 changes: 1 addition & 1 deletion api/cluster/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/internal/analytic"
"github.com/0xJacky/Nginx-UI/internal/cluster"
"github.com/0xJacky/Nginx-UI/internal/cosy"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/0xJacky/Nginx-UI/settings"
"github.com/gin-gonic/gin"
"github.com/spf13/cast"
"github.com/uozi-tech/cosy"
"gorm.io/gorm"
"net/http"
)
Expand Down
2 changes: 1 addition & 1 deletion api/cluster/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func GetCurrentNode(c *gin.Context) {
if _, ok := c.Get("NodeSecret"); !ok {
if _, ok := c.Get("Secret"); !ok {
c.JSON(http.StatusNotAcceptable, gin.H{
"message": "node secret not exist",
})
Expand Down
2 changes: 1 addition & 1 deletion api/config/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package config
import (
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/internal/config"
"github.com/0xJacky/Nginx-UI/internal/logger"
"github.com/uozi-tech/cosy/logger"
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/gin-gonic/gin"
"net/http"
Expand Down
2 changes: 1 addition & 1 deletion api/config/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/internal/config"
"github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/internal/logger"
"github.com/uozi-tech/cosy/logger"
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/query"
"github.com/gin-gonic/gin"
Expand Down
2 changes: 1 addition & 1 deletion api/nginx/nginx_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/internal/cache"
"github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/internal/logger"
"github.com/uozi-tech/cosy/logger"
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/settings"
"github.com/gin-gonic/gin"
Expand Down
2 changes: 1 addition & 1 deletion api/notification/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package notification

import (
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/internal/cosy"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/gin-gonic/gin"
"github.com/spf13/cast"
"github.com/uozi-tech/cosy"
"net/http"
)

Expand Down
57 changes: 45 additions & 12 deletions api/settings/settings.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,71 @@
package settings

import (
"fmt"
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/internal/cron"
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/settings"
"github.com/gin-gonic/gin"
cSettings "github.com/uozi-tech/cosy/settings"
"net/http"
)

func GetServerName(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"name": settings.ServerSettings.Name,
"name": settings.NodeSettings.Name,
})
}

func GetSettings(c *gin.Context) {
settings.NginxSettings.AccessLogPath = nginx.GetAccessLogPath()
settings.NginxSettings.ErrorLogPath = nginx.GetErrorLogPath()
settings.NginxSettings.ConfigDir = nginx.GetConfPath()
settings.NginxSettings.PIDPath = nginx.GetPIDPath()

if settings.NginxSettings.ReloadCmd == "" {
settings.NginxSettings.ReloadCmd = "nginx -s reload"
}

if settings.NginxSettings.RestartCmd == "" {
pidPath := nginx.GetPIDPath()
daemon := nginx.GetSbinPath()
if daemon == "" {
settings.NginxSettings.RestartCmd =
fmt.Sprintf("start-stop-daemon --stop --quiet --oknodo --retry=TERM/30/KILL/5"+
" --pidfile %s && nginx", pidPath)
return
}

settings.NginxSettings.RestartCmd =
fmt.Sprintf("start-stop-daemon --start --quiet --pidfile %s --exec %s", pidPath, daemon)
}

c.JSON(http.StatusOK, gin.H{
"server": settings.ServerSettings,
"app": cSettings.AppSettings,
"server": cSettings.ServerSettings,
"database": settings.DatabaseSettings,
"auth": settings.AuthSettings,
"casdoor": settings.CasdoorSettings,
"cert": settings.CertSettings,
"http": settings.HTTPSettings,
"logrotate": settings.LogrotateSettings,
"nginx": settings.NginxSettings,
"node": settings.NodeSettings,
"openai": settings.OpenAISettings,
"logrotate": settings.LogrotateSettings,
"auth": settings.AuthSettings,
"terminal": settings.TerminalSettings,
"webauthn": settings.WebAuthnSettings,
})
}

func SaveSettings(c *gin.Context) {
var json struct {
Server settings.Server `json:"server"`
Nginx settings.Nginx `json:"nginx"`
Auth settings.Auth `json:"auth"`
Cert settings.Cert `json:"cert"`
Http settings.HTTP `json:"http"`
Node settings.Node `json:"node"`
Openai settings.OpenAI `json:"openai"`
Logrotate settings.Logrotate `json:"logrotate"`
Auth settings.Auth `json:"auth"`
}

if !api.BindAndValid(c, &json) {
Expand All @@ -45,11 +77,12 @@ func SaveSettings(c *gin.Context) {
go cron.RestartLogrotate()
}

settings.ProtectedFill(&settings.ServerSettings, &json.Server)
settings.ProtectedFill(&settings.NginxSettings, &json.Nginx)
settings.ProtectedFill(&settings.OpenAISettings, &json.Openai)
settings.ProtectedFill(&settings.LogrotateSettings, &json.Logrotate)
settings.ProtectedFill(&settings.AuthSettings, &json.Auth)
cSettings.ProtectedFill(settings.AuthSettings, &json.Auth)
cSettings.ProtectedFill(settings.CertSettings, &json.Cert)
cSettings.ProtectedFill(settings.HTTPSettings, &json.Http)
cSettings.ProtectedFill(settings.NodeSettings, &json.Node)
cSettings.ProtectedFill(settings.OpenAISettings, &json.Openai)
cSettings.ProtectedFill(settings.LogrotateSettings, &json.Logrotate)

err := settings.Save()
if err != nil {
Expand Down
41 changes: 41 additions & 0 deletions api/sites/category.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package sites

import (
"github.com/0xJacky/Nginx-UI/model"
"github.com/gin-gonic/gin"
"github.com/uozi-tech/cosy"
)

func GetCategory(c *gin.Context) {

}

func GetCategoryList(c *gin.Context) {
cosy.Core[model.SiteCategory](c).PagingList()
}

func AddCategory(c *gin.Context) {
cosy.Core[model.SiteCategory](c).
SetValidRules(gin.H{
"name": "required",
"sync_node_ids": "omitempty",
}).
Create()
}

func ModifyCategory(c *gin.Context) {
cosy.Core[model.SiteCategory](c).
SetValidRules(gin.H{
"name": "required",
"sync_node_ids": "omitempty",
}).
Modify()
}

func DeleteCategory(c *gin.Context) {
cosy.Core[model.SiteCategory](c).Destroy()
}

func RecoverCategory(c *gin.Context) {
cosy.Core[model.SiteCategory](c).Recover()
}
63 changes: 1 addition & 62 deletions api/sites/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,78 +3,17 @@ package sites
import (
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/internal/cert"
"github.com/0xJacky/Nginx-UI/internal/config"
"github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/internal/logger"
"github.com/uozi-tech/cosy/logger"
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/gin-gonic/gin"
"github.com/sashabaranov/go-openai"
"net/http"
"os"
"strings"
)

func GetSiteList(c *gin.Context) {
name := c.Query("name")
enabled := c.Query("enabled")
orderBy := c.Query("order_by")
sort := c.DefaultQuery("sort", "desc")

configFiles, err := os.ReadDir(nginx.GetConfPath("sites-available"))
if err != nil {
api.ErrHandler(c, err)
return
}

enabledConfig, err := os.ReadDir(nginx.GetConfPath("sites-enabled"))
if err != nil {
api.ErrHandler(c, err)
return
}

enabledConfigMap := make(map[string]bool)
for i := range enabledConfig {
enabledConfigMap[enabledConfig[i].Name()] = true
}

var configs []config.Config

for i := range configFiles {
file := configFiles[i]
fileInfo, _ := file.Info()
if !file.IsDir() {
// name filter
if name != "" && !strings.Contains(file.Name(), name) {
continue
}
// status filter
if enabled != "" {
if enabled == "true" && !enabledConfigMap[file.Name()] {
continue
}
if enabled == "false" && enabledConfigMap[file.Name()] {
continue
}
}
configs = append(configs, config.Config{
Name: file.Name(),
ModifiedAt: fileInfo.ModTime(),
Size: fileInfo.Size(),
IsDir: fileInfo.IsDir(),
Enabled: enabledConfigMap[file.Name()],
})
}
}

configs = config.Sort(orderBy, sort, configs)

c.JSON(http.StatusOK, gin.H{
"data": configs,
})
}

func GetSite(c *gin.Context) {
rewriteName, ok := c.Get("rewriteConfigFileName")
name := c.Param("name")
Expand Down
Loading

0 comments on commit 4747c69

Please sign in to comment.