feat(env_group): migrate site_category to env_group

This commit is contained in:
Jacky 2025-04-05 02:32:40 +00:00
parent de1860718e
commit a379211e3c
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
66 changed files with 4837 additions and 4251 deletions

48
api/cluster/group.go Normal file
View file

@ -0,0 +1,48 @@
package cluster
import (
"github.com/0xJacky/Nginx-UI/model"
"github.com/gin-gonic/gin"
"github.com/uozi-tech/cosy"
"gorm.io/gorm"
)
func GetGroup(c *gin.Context) {
cosy.Core[model.EnvGroup](c).Get()
}
func GetGroupList(c *gin.Context) {
cosy.Core[model.EnvGroup](c).GormScope(func(tx *gorm.DB) *gorm.DB {
return tx.Order("order_id ASC")
}).PagingList()
}
func AddGroup(c *gin.Context) {
cosy.Core[model.EnvGroup](c).
SetValidRules(gin.H{
"name": "required",
"sync_node_ids": "omitempty",
}).
Create()
}
func ModifyGroup(c *gin.Context) {
cosy.Core[model.EnvGroup](c).
SetValidRules(gin.H{
"name": "required",
"sync_node_ids": "omitempty",
}).
Modify()
}
func DeleteGroup(c *gin.Context) {
cosy.Core[model.EnvGroup](c).Destroy()
}
func RecoverGroup(c *gin.Context) {
cosy.Core[model.EnvGroup](c).Recover()
}
func UpdateGroupsOrder(c *gin.Context) {
cosy.Core[model.EnvGroup](c).UpdateOrder()
}

View file

@ -16,4 +16,12 @@ func InitRouter(r *gin.RouterGroup) {
}
// Node
r.GET("node", GetCurrentNode)
r.GET("env_groups", GetGroupList)
r.GET("env_groups/:id", GetGroup)
r.POST("env_groups", AddGroup)
r.POST("env_groups/:id", ModifyGroup)
r.DELETE("env_groups/:id", DeleteGroup)
r.POST("env_groups/:id/recover", RecoverGroup)
r.POST("env_groups/order", UpdateGroupsOrder)
}

View file

@ -1,48 +0,0 @@
package sites
import (
"github.com/0xJacky/Nginx-UI/model"
"github.com/gin-gonic/gin"
"github.com/uozi-tech/cosy"
"gorm.io/gorm"
)
func GetCategory(c *gin.Context) {
cosy.Core[model.SiteCategory](c).Get()
}
func GetCategoryList(c *gin.Context) {
cosy.Core[model.SiteCategory](c).GormScope(func(tx *gorm.DB) *gorm.DB {
return tx.Order("order_id ASC")
}).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()
}
func UpdateCategoriesOrder(c *gin.Context) {
cosy.Core[model.SiteCategory](c).UpdateOrder()
}

View file

@ -21,7 +21,7 @@ func GetSiteList(c *gin.Context) {
enabled := c.Query("enabled")
orderBy := c.Query("sort_by")
sort := c.DefaultQuery("order", "desc")
querySiteCategoryId := cast.ToUint64(c.Query("site_category_id"))
queryEnvGroupId := cast.ToUint64(c.Query("env_group_id"))
configFiles, err := os.ReadDir(nginx.GetConfPath("sites-available"))
if err != nil {
@ -36,9 +36,9 @@ func GetSiteList(c *gin.Context) {
}
s := query.Site
sTx := s.Preload(s.SiteCategory)
if querySiteCategoryId != 0 {
sTx.Where(s.SiteCategoryID.Eq(querySiteCategoryId))
sTx := s.Preload(s.EnvGroup)
if queryEnvGroupId != 0 {
sTx.Where(s.EnvGroupID.Eq(queryEnvGroupId))
}
sites, err := sTx.Find()
if err != nil {
@ -76,28 +76,28 @@ func GetSiteList(c *gin.Context) {
}
}
var (
siteCategoryId uint64
siteCategory *model.SiteCategory
envGroupId uint64
envGroup *model.EnvGroup
)
if site, ok := sitesMap[file.Name()]; ok {
siteCategoryId = site.SiteCategoryID
siteCategory = site.SiteCategory
envGroupId = site.EnvGroupID
envGroup = site.EnvGroup
}
// site category filter
if querySiteCategoryId != 0 && siteCategoryId != querySiteCategoryId {
// env group filter
if queryEnvGroupId != 0 && envGroupId != queryEnvGroupId {
continue
}
configs = append(configs, config.Config{
Name: file.Name(),
ModifiedAt: fileInfo.ModTime(),
Size: fileInfo.Size(),
IsDir: fileInfo.IsDir(),
Enabled: enabledConfigMap[file.Name()],
SiteCategoryID: siteCategoryId,
SiteCategory: siteCategory,
Name: file.Name(),
ModifiedAt: fileInfo.ModTime(),
Size: fileInfo.Size(),
IsDir: fileInfo.IsDir(),
Enabled: enabledConfigMap[file.Name()],
EnvGroupID: envGroupId,
EnvGroup: envGroup,
})
}

View file

@ -23,13 +23,3 @@ func InitRouter(r *gin.RouterGroup) {
// duplicate site
r.POST("sites/:name/duplicate", DuplicateSite)
}
func InitCategoryRouter(r *gin.RouterGroup) {
r.GET("site_categories", GetCategoryList)
r.GET("site_categories/:id", GetCategory)
r.POST("site_categories", AddCategory)
r.POST("site_categories/:id", ModifyCategory)
r.DELETE("site_categories/:id", DeleteCategory)
r.POST("site_categories/:id/recover", RecoverCategory)
r.POST("site_categories/order", UpdateCategoriesOrder)
}

View file

@ -114,17 +114,17 @@ func SaveSite(c *gin.Context) {
name := c.Param("name")
var json struct {
Content string `json:"content" binding:"required"`
SiteCategoryID uint64 `json:"site_category_id"`
SyncNodeIDs []uint64 `json:"sync_node_ids"`
Overwrite bool `json:"overwrite"`
Content string `json:"content" binding:"required"`
EnvGroupID uint64 `json:"env_group_id"`
SyncNodeIDs []uint64 `json:"sync_node_ids"`
Overwrite bool `json:"overwrite"`
}
if !cosy.BindAndValid(c, &json) {
return
}
err := site.Save(name, json.Content, json.Overwrite, json.SiteCategoryID, json.SyncNodeIDs)
err := site.Save(name, json.Content, json.Overwrite, json.EnvGroupID, json.SyncNodeIDs)
if err != nil {
cosy.ErrHandler(c, err)
return
@ -191,7 +191,7 @@ func DeleteSite(c *gin.Context) {
func BatchUpdateSites(c *gin.Context) {
cosy.Core[model.Site](c).SetValidRules(gin.H{
"site_category_id": "required",
"env_group_id": "required",
}).SetItemKey("path").
BeforeExecuteHook(func(ctx *cosy.Ctx[model.Site]) {
effectedPath := make([]string, len(ctx.BatchEffectedIDs))

View file

@ -5,6 +5,7 @@ import "github.com/gin-gonic/gin"
func InitRouter(r *gin.RouterGroup) {
r.GET("streams", GetStreams)
r.GET("streams/:name", GetStream)
r.PUT("streams", BatchUpdateStreams)
r.POST("streams/:name", SaveStream)
r.POST("streams/:name/rename", RenameStream)
r.POST("streams/:name/enable", EnableStream)

View file

@ -3,16 +3,21 @@ package streams
import (
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/0xJacky/Nginx-UI/internal/config"
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/internal/stream"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/gin-gonic/gin"
"github.com/samber/lo"
"github.com/sashabaranov/go-openai"
"github.com/spf13/cast"
"github.com/uozi-tech/cosy"
"gorm.io/gorm/clause"
)
type Stream struct {
@ -24,13 +29,17 @@ type Stream struct {
ChatGPTMessages []openai.ChatCompletionMessage `json:"chatgpt_messages,omitempty"`
Tokenized *nginx.NgxConfig `json:"tokenized,omitempty"`
Filepath string `json:"filepath"`
EnvGroupID uint64 `json:"env_group_id"`
EnvGroup *model.EnvGroup `json:"env_group,omitempty"`
SyncNodeIDs []uint64 `json:"sync_node_ids" gorm:"serializer:json"`
}
func GetStreams(c *gin.Context) {
name := c.Query("name")
enabled := c.Query("enabled")
orderBy := c.Query("order_by")
sort := c.DefaultQuery("sort", "desc")
queryEnvGroupId := cast.ToUint64(c.Query("env_group_id"))
configFiles, err := os.ReadDir(nginx.GetConfPath("streams-available"))
if err != nil {
@ -51,23 +60,91 @@ func GetStreams(c *gin.Context) {
var configs []config.Config
// Get all streams map for environment group lookup
s := query.Stream
var streams []*model.Stream
if queryEnvGroupId != 0 {
streams, err = s.Where(s.EnvGroupID.Eq(queryEnvGroupId)).Find()
} else {
streams, err = s.Find()
}
if err != nil {
cosy.ErrHandler(c, err)
return
}
// Retrieve environment groups data
eg := query.EnvGroup
envGroups, err := eg.Find()
if err != nil {
cosy.ErrHandler(c, err)
return
}
// Create a map of environment groups for quick lookup by ID
envGroupMap := lo.SliceToMap(envGroups, func(item *model.EnvGroup) (uint64, *model.EnvGroup) {
return item.ID, item
})
// Convert streams slice to map for efficient lookups
streamsMap := lo.SliceToMap(streams, func(item *model.Stream) (string, *model.Stream) {
// Associate each stream with its corresponding environment group
if item.EnvGroupID > 0 {
item.EnvGroup = envGroupMap[item.EnvGroupID]
}
return filepath.Base(item.Path), item
})
for i := range configFiles {
file := configFiles[i]
fileInfo, _ := file.Info()
if !file.IsDir() {
if name != "" && !strings.Contains(file.Name(), name) {
if file.IsDir() {
continue
}
// Apply name filter if specified
if name != "" && !strings.Contains(file.Name(), name) {
continue
}
// Apply enabled status filter if specified
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()],
})
}
var (
envGroupId uint64
envGroup *model.EnvGroup
)
// Lookup stream in the streams map to get environment group info
if stream, ok := streamsMap[file.Name()]; ok {
envGroupId = stream.EnvGroupID
envGroup = stream.EnvGroup
}
// Apply environment group filter if specified
if queryEnvGroupId != 0 && envGroupId != queryEnvGroupId {
continue
}
// Add the config to the result list after passing all filters
configs = append(configs, config.Config{
Name: file.Name(),
ModifiedAt: fileInfo.ModTime(),
Size: fileInfo.Size(),
IsDir: fileInfo.IsDir(),
Enabled: enabledConfigMap[file.Name()],
EnvGroupID: envGroupId,
EnvGroup: envGroup,
})
}
// Sort the configs based on the provided sort parameters
configs = config.Sort(orderBy, sort, configs)
c.JSON(http.StatusOK, gin.H{
@ -78,6 +155,7 @@ func GetStreams(c *gin.Context) {
func GetStream(c *gin.Context) {
name := c.Param("name")
// Get the absolute path to the stream configuration file
path := nginx.GetConfPath("streams-available", name)
file, err := os.Stat(path)
if os.IsNotExist(err) {
@ -87,24 +165,26 @@ func GetStream(c *gin.Context) {
return
}
// Check if the stream is enabled
enabled := true
if _, err := os.Stat(nginx.GetConfPath("streams-enabled", name)); os.IsNotExist(err) {
enabled = false
}
// Retrieve or create ChatGPT log for this stream
g := query.ChatGPTLog
chatgpt, err := g.Where(g.Name.Eq(path)).FirstOrCreate()
if err != nil {
cosy.ErrHandler(c, err)
return
}
// Initialize empty content if nil
if chatgpt.Content == nil {
chatgpt.Content = make([]openai.ChatCompletionMessage, 0)
}
// Retrieve or create stream model from database
s := query.Stream
streamModel, err := s.Where(s.Path.Eq(path)).FirstOrCreate()
if err != nil {
@ -112,6 +192,7 @@ func GetStream(c *gin.Context) {
return
}
// For advanced mode, return the raw content
if streamModel.Advanced {
origContent, err := os.ReadFile(path)
if err != nil {
@ -127,13 +208,15 @@ func GetStream(c *gin.Context) {
Config: string(origContent),
ChatGPTMessages: chatgpt.Content,
Filepath: path,
EnvGroupID: streamModel.EnvGroupID,
EnvGroup: streamModel.EnvGroup,
SyncNodeIDs: streamModel.SyncNodeIDs,
})
return
}
// For normal mode, parse and tokenize the configuration
nginxConfig, err := nginx.ParseNgxConfig(path)
if err != nil {
cosy.ErrHandler(c, err)
return
@ -148,6 +231,8 @@ func GetStream(c *gin.Context) {
Tokenized: nginxConfig,
ChatGPTMessages: chatgpt.Content,
Filepath: path,
EnvGroupID: streamModel.EnvGroupID,
EnvGroup: streamModel.EnvGroup,
SyncNodeIDs: streamModel.SyncNodeIDs,
})
}
@ -157,24 +242,55 @@ func SaveStream(c *gin.Context) {
var json struct {
Content string `json:"content" binding:"required"`
EnvGroupID uint64 `json:"env_group_id"`
SyncNodeIDs []uint64 `json:"sync_node_ids"`
Overwrite bool `json:"overwrite"`
}
// Validate input JSON
if !cosy.BindAndValid(c, &json) {
return
}
err := stream.Save(name, json.Content, json.Overwrite, json.SyncNodeIDs)
// Get stream from database or create if not exists
path := nginx.GetConfPath("streams-available", name)
s := query.Stream
streamModel, err := s.Where(s.Path.Eq(path)).FirstOrCreate()
if err != nil {
cosy.ErrHandler(c, err)
return
}
// Update environment group ID if provided
if json.EnvGroupID > 0 {
streamModel.EnvGroupID = json.EnvGroupID
}
// Update synchronization node IDs if provided
if json.SyncNodeIDs != nil {
streamModel.SyncNodeIDs = json.SyncNodeIDs
}
// Save the updated stream model to database
_, err = s.Where(s.ID.Eq(streamModel.ID)).Updates(streamModel)
if err != nil {
cosy.ErrHandler(c, err)
return
}
// Save the stream configuration file
err = stream.Save(name, json.Content, json.Overwrite, json.SyncNodeIDs)
if err != nil {
cosy.ErrHandler(c, err)
return
}
// Return the updated stream
GetStream(c)
}
func EnableStream(c *gin.Context) {
// Enable the stream by creating a symlink in streams-enabled directory
err := stream.Enable(c.Param("name"))
if err != nil {
cosy.ErrHandler(c, err)
@ -187,6 +303,7 @@ func EnableStream(c *gin.Context) {
}
func DisableStream(c *gin.Context) {
// Disable the stream by removing the symlink from streams-enabled directory
err := stream.Disable(c.Param("name"))
if err != nil {
cosy.ErrHandler(c, err)
@ -199,6 +316,7 @@ func DisableStream(c *gin.Context) {
}
func DeleteStream(c *gin.Context) {
// Delete the stream configuration file and its symbolic link if exists
err := stream.Delete(c.Param("name"))
if err != nil {
cosy.ErrHandler(c, err)
@ -215,10 +333,12 @@ func RenameStream(c *gin.Context) {
var json struct {
NewName string `json:"new_name"`
}
// Validate input JSON
if !cosy.BindAndValid(c, &json) {
return
}
// Rename the stream configuration file
err := stream.Rename(oldName, json.NewName)
if err != nil {
cosy.ErrHandler(c, err)
@ -229,3 +349,29 @@ func RenameStream(c *gin.Context) {
"message": "ok",
})
}
func BatchUpdateStreams(c *gin.Context) {
cosy.Core[model.Stream](c).SetValidRules(gin.H{
"env_group_id": "required",
}).SetItemKey("path").
BeforeExecuteHook(func(ctx *cosy.Ctx[model.Stream]) {
effectedPath := make([]string, len(ctx.BatchEffectedIDs))
var streams []*model.Stream
for i, name := range ctx.BatchEffectedIDs {
path := nginx.GetConfPath("streams-available", name)
effectedPath[i] = path
streams = append(streams, &model.Stream{
Path: path,
})
}
s := query.Stream
err := s.Clauses(clause.OnConflict{
DoNothing: true,
}).Create(streams...)
if err != nil {
ctx.AbortWithError(err)
return
}
ctx.BatchEffectedIDs = effectedPath
}).BatchModify()
}