1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- package config
- 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/nginx"
- "github.com/gin-gonic/gin"
- "github.com/sashabaranov/go-openai"
- "net/http"
- "os"
- "time"
- )
- func AddConfig(c *gin.Context) {
- var json struct {
- Name string `json:"name" binding:"required"`
- NewFilepath string `json:"new_filepath" binding:"required"`
- Content string `json:"content"`
- Overwrite bool `json:"overwrite"`
- }
- if !api.BindAndValid(c, &json) {
- return
- }
- name := json.Name
- content := json.Content
- path := json.NewFilepath
- if !helper.IsUnderDirectory(path, nginx.GetConfPath()) {
- c.JSON(http.StatusForbidden, gin.H{
- "message": "new filepath is not under the nginx conf path",
- })
- return
- }
- if !json.Overwrite && helper.FileExists(path) {
- c.JSON(http.StatusNotAcceptable, gin.H{
- "message": "File exists",
- })
- return
- }
- err := os.WriteFile(path, []byte(content), 0644)
- if err != nil {
- api.ErrHandler(c, err)
- return
- }
- output := nginx.Reload()
- if nginx.GetLogLevel(output) >= nginx.Warn {
- c.JSON(http.StatusInternalServerError, gin.H{
- "message": output,
- })
- return
- }
- c.JSON(http.StatusOK, config.Config{
- Name: name,
- Content: content,
- ChatGPTMessages: make([]openai.ChatCompletionMessage, 0),
- FilePath: path,
- ModifiedAt: time.Now(),
- })
- }
|