Skip to content

Commit

Permalink
refactor: migrate to new cosy
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJacky committed Oct 22, 2024
1 parent 6082aef commit 33a996e
Show file tree
Hide file tree
Showing 111 changed files with 1,164 additions and 1,773 deletions.
1 change: 1 addition & 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.testing.ini
dist
*.exe
*.po~
Expand Down
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
17 changes: 9 additions & 8 deletions api/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ import (
"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()
c.JSON(http.StatusOK, gin.H{
"server": settings.ServerSettings,
"server": cSettings.ServerSettings,
"nginx": settings.NginxSettings,
"openai": settings.OpenAISettings,
"logrotate": settings.LogrotateSettings,
Expand All @@ -29,7 +30,7 @@ func GetSettings(c *gin.Context) {

func SaveSettings(c *gin.Context) {
var json struct {
Server settings.Server `json:"server"`
Server cSettings.Server `json:"server"`
Nginx settings.Nginx `json:"nginx"`
Openai settings.OpenAI `json:"openai"`
Logrotate settings.Logrotate `json:"logrotate"`
Expand All @@ -45,11 +46,11 @@ 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(cSettings.ServerSettings, &json.Server)
cSettings.ProtectedFill(settings.NginxSettings, &json.Nginx)
cSettings.ProtectedFill(settings.OpenAISettings, &json.Openai)
cSettings.ProtectedFill(settings.LogrotateSettings, &json.Logrotate)
cSettings.ProtectedFill(settings.AuthSettings, &json.Auth)

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 33a996e

Please sign in to comment.