mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 18:35:51 +02:00
feat(env_group): migrate site_category to env_group
This commit is contained in:
parent
de1860718e
commit
a379211e3c
66 changed files with 4837 additions and 4251 deletions
|
@ -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)
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue