diff --git a/.editorconfig b/.editorconfig index dffbee67..2b7ac736 100755 --- a/.editorconfig +++ b/.editorconfig @@ -9,8 +9,8 @@ insert_final_newline = true trim_trailing_whitespace = true [*.go] +indent_style = tab indent_size = 4 [README.md] -indent_style = space indent_size = 4 diff --git a/api/certificate/dns_provider.go b/api/certificate/dns_provider.go index 864f6235..db906b53 100644 --- a/api/certificate/dns_provider.go +++ b/api/certificate/dns_provider.go @@ -1,27 +1,26 @@ package certificate import ( - "github.com/0xJacky/Nginx-UI/internal/cert/dns" - "github.com/gin-gonic/gin" - "net/http" + "github.com/0xJacky/Nginx-UI/internal/cert/dns" + "github.com/gin-gonic/gin" + "net/http" ) func GetDNSProvidersList(c *gin.Context) { - c.JSON(http.StatusOK, dns.GetProvidersList()) + c.JSON(http.StatusOK, dns.GetProvidersList()) } func GetDNSProvider(c *gin.Context) { - code := c.Param("code") + code := c.Param("code") - provider, ok := dns.GetProvider(code) + provider, ok := dns.GetProvider(code) - if !ok { - c.JSON(http.StatusNotFound, gin.H{ - "message": "provider not found", - }) - return - } + if !ok { + c.JSON(http.StatusNotFound, gin.H{ + "message": "provider not found", + }) + return + } - c.JSON(http.StatusOK, provider) + c.JSON(http.StatusOK, provider) } - diff --git a/api/config/add.go b/api/config/add.go index ace2cf16..167a8e80 100644 --- a/api/config/add.go +++ b/api/config/add.go @@ -1,97 +1,97 @@ 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/0xJacky/Nginx-UI/model" - "github.com/0xJacky/Nginx-UI/query" - "github.com/gin-gonic/gin" - "github.com/sashabaranov/go-openai" - "net/http" - "os" - "path/filepath" - "time" + "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/0xJacky/Nginx-UI/model" + "github.com/0xJacky/Nginx-UI/query" + "github.com/gin-gonic/gin" + "github.com/sashabaranov/go-openai" + "net/http" + "os" + "path/filepath" + "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"` - SyncNodeIds []int `json:"sync_node_ids"` - } + var json struct { + Name string `json:"name" binding:"required"` + NewFilepath string `json:"new_filepath" binding:"required"` + Content string `json:"content"` + Overwrite bool `json:"overwrite"` + SyncNodeIds []int `json:"sync_node_ids"` + } - if !api.BindAndValid(c, &json) { - return - } + 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 - } + 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 - } + if !json.Overwrite && helper.FileExists(path) { + c.JSON(http.StatusNotAcceptable, gin.H{ + "message": "File exists", + }) + return + } - // check if the dir exists, if not, use mkdirAll to create the dir - dir := filepath.Dir(path) - if !helper.FileExists(dir) { - err := os.MkdirAll(dir, 0755) - if err != nil { - api.ErrHandler(c, err) - return - } - } + // check if the dir exists, if not, use mkdirAll to create the dir + dir := filepath.Dir(path) + if !helper.FileExists(dir) { + err := os.MkdirAll(dir, 0755) + if err != nil { + api.ErrHandler(c, err) + return + } + } - err := os.WriteFile(path, []byte(content), 0644) - if err != nil { - api.ErrHandler(c, err) - 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 - } + output := nginx.Reload() + if nginx.GetLogLevel(output) >= nginx.Warn { + c.JSON(http.StatusInternalServerError, gin.H{ + "message": output, + }) + return + } - q := query.Config - _, err = q.Where(q.Filepath.Eq(path)).Delete() - if err != nil { - api.ErrHandler(c, err) - return - } + q := query.Config + _, err = q.Where(q.Filepath.Eq(path)).Delete() + if err != nil { + api.ErrHandler(c, err) + return + } - err = q.Create(&model.Config{ - Name: name, - Filepath: path, - SyncNodeIds: json.SyncNodeIds, - SyncOverwrite: json.Overwrite, - }) - if err != nil { - api.ErrHandler(c, err) - return - } + err = q.Create(&model.Config{ + Name: name, + Filepath: path, + SyncNodeIds: json.SyncNodeIds, + SyncOverwrite: json.Overwrite, + }) + if err != nil { + api.ErrHandler(c, err) + return + } - c.JSON(http.StatusOK, config.Config{ - Name: name, - Content: content, - ChatGPTMessages: make([]openai.ChatCompletionMessage, 0), - FilePath: path, - ModifiedAt: time.Now(), - }) + c.JSON(http.StatusOK, config.Config{ + Name: name, + Content: content, + ChatGPTMessages: make([]openai.ChatCompletionMessage, 0), + FilePath: path, + ModifiedAt: time.Now(), + }) } diff --git a/api/nginx/nginx.go b/api/nginx/nginx.go index 732a6dae..51bf095c 100644 --- a/api/nginx/nginx.go +++ b/api/nginx/nginx.go @@ -1,59 +1,59 @@ package nginx import ( - "github.com/0xJacky/Nginx-UI/api" - "github.com/0xJacky/Nginx-UI/internal/nginx" - "github.com/gin-gonic/gin" - "net/http" + "github.com/0xJacky/Nginx-UI/api" + "github.com/0xJacky/Nginx-UI/internal/nginx" + "github.com/gin-gonic/gin" + "net/http" ) func BuildNginxConfig(c *gin.Context) { - var ngxConf nginx.NgxConfig - if !api.BindAndValid(c, &ngxConf) { - return - } - content, err := ngxConf.BuildConfig() - if err != nil { - api.ErrHandler(c, err) - return - } - c.JSON(http.StatusOK, gin.H{ - "content": content, - }) + var ngxConf nginx.NgxConfig + if !api.BindAndValid(c, &ngxConf) { + return + } + content, err := ngxConf.BuildConfig() + if err != nil { + api.ErrHandler(c, err) + return + } + c.JSON(http.StatusOK, gin.H{ + "content": content, + }) } func TokenizeNginxConfig(c *gin.Context) { - var json struct { - Content string `json:"content" binding:"required"` - } + var json struct { + Content string `json:"content" binding:"required"` + } - if !api.BindAndValid(c, &json) { - return - } + if !api.BindAndValid(c, &json) { + return + } - ngxConfig, err := nginx.ParseNgxConfigByContent(json.Content) - if err != nil { - api.ErrHandler(c, err) - return - } - c.JSON(http.StatusOK, ngxConfig) + ngxConfig, err := nginx.ParseNgxConfigByContent(json.Content) + if err != nil { + api.ErrHandler(c, err) + return + } + c.JSON(http.StatusOK, ngxConfig) } func FormatNginxConfig(c *gin.Context) { - var json struct { - Content string `json:"content" binding:"required"` - } + var json struct { + Content string `json:"content" binding:"required"` + } - if !api.BindAndValid(c, &json) { - return - } - content, err := nginx.FmtCode(json.Content) - if err != nil { - api.ErrHandler(c, err) - return - } - c.JSON(http.StatusOK, gin.H{ - "content": content, - }) + if !api.BindAndValid(c, &json) { + return + } + content, err := nginx.FmtCode(json.Content) + if err != nil { + api.ErrHandler(c, err) + return + } + c.JSON(http.StatusOK, gin.H{ + "content": content, + }) } diff --git a/app/src/App.vue b/app/src/App.vue index 5bb33144..e4c972a8 100644 --- a/app/src/App.vue +++ b/app/src/App.vue @@ -90,6 +90,7 @@ loadTranslations(route) .ant-menu { border-right: 0 !important; } + &.ant-layout-sider-has-trigger { padding-bottom: 0; } diff --git a/go.mod b/go.mod index 321b95b1..69ec0134 100644 --- a/go.mod +++ b/go.mod @@ -34,7 +34,7 @@ require ( github.com/shopspring/decimal v1.4.0 github.com/spf13/cast v1.7.0 github.com/stretchr/testify v1.9.0 - github.com/tufanbarisyildirim/gonginx v0.0.0-20240109151651-bb3e845a7a2a + github.com/tufanbarisyildirim/gonginx v0.0.0-20241013191809-e73b7dd454e8 go.uber.org/zap v1.27.0 golang.org/x/crypto v0.28.0 gopkg.in/guregu/null.v4 v4.0.0 @@ -268,5 +268,3 @@ require ( sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect ) - -replace github.com/tufanbarisyildirim/gonginx v0.0.0-20240109151651-bb3e845a7a2a => github.com/0xJacky/gonginx v0.0.0-20240119024214-c0d76957d0c7 diff --git a/go.sum b/go.sum index 06e29da3..d22b7b24 100644 --- a/go.sum +++ b/go.sum @@ -39,7 +39,6 @@ cloud.google.com/go v0.104.0/go.mod h1:OO6xxXdJyvuJPcEPBLN9BJPD+jep5G1+2U5B5gkRY cloud.google.com/go v0.105.0/go.mod h1:PrLgOJNe5nfE9UMxKxgXj4mD3voiP+YQ6gdt6KMFOKM= cloud.google.com/go v0.107.0/go.mod h1:wpc2eNrD7hXUTy8EKS10jkxpZBjASrORK7goS+3YX2I= cloud.google.com/go v0.110.0/go.mod h1:SJnCLqQ0FCFGSZMUNUf84MV3Aia54kn7pi8st7tMzaY= -cloud.google.com/go v0.115.1 h1:Jo0SM9cQnSkYfp44+v+NQXHpcHqlnRJk2qxh6yvxxxQ= cloud.google.com/go/accessapproval v1.4.0/go.mod h1:zybIuC3KpDOvotz59lFe5qxRZx6C75OtwbisN56xYB4= cloud.google.com/go/accessapproval v1.5.0/go.mod h1:HFy3tuiGvMdcd/u+Cu5b9NkO1pEICJ46IR82PoUdplw= cloud.google.com/go/accessapproval v1.6.0/go.mod h1:R0EiYnwV5fsRFiKZkPHr6mwyk2wxUJ30nL4j2pcFY2E= @@ -179,7 +178,6 @@ cloud.google.com/go/compute v1.14.0/go.mod h1:YfLtxrj9sU4Yxv+sXzZkyPjEyPBZfXHUvj cloud.google.com/go/compute v1.15.1/go.mod h1:bjjoF/NtFUrkD/urWfdHaKuOPDR5nWIs63rR+SXhcpA= cloud.google.com/go/compute v1.18.0/go.mod h1:1X7yHxec2Ga+Ss6jPyjxRxpu2uu7PLgsOVXvgU0yacs= cloud.google.com/go/compute v1.19.0/go.mod h1:rikpw2y+UMidAe9tISo04EHNOIf42RLYF/q8Bs93scU= -cloud.google.com/go/compute v1.28.1 h1:XwPcZjgMCnU2tkwY10VleUjSAfpTj9RDn+kGrbYsi8o= cloud.google.com/go/compute/metadata v0.1.0/go.mod h1:Z1VN+bulIf6bt4P/C37K4DyZYZEXYonfTBHHFPO/4UU= cloud.google.com/go/compute/metadata v0.2.0/go.mod h1:zFmK7XCadkQkj6TtorcaGlCW1hT1fIilQDwofLpJ20k= cloud.google.com/go/compute/metadata v0.2.1/go.mod h1:jgHgmJd2RKBGzXqF5LR2EZMGxBkeanZ9wwa75XHJgOM= @@ -608,8 +606,6 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= gioui.org v0.0.0-20210308172011-57750fc8a0a6/go.mod h1:RSH6KIUZ0p2xy5zHDxgAM4zumjgTw83q2ge/PI+yyw8= git.sr.ht/~sbinet/gg v0.3.1/go.mod h1:KGYtlADtqsqANL9ueOFkWymvzUvLMQllU5Ixo+8v3pc= -github.com/0xJacky/gonginx v0.0.0-20240119024214-c0d76957d0c7 h1:Ry+bJ2b0JDOHz3LpNJwLbk8ucj2lLHv/oQhfua25EPQ= -github.com/0xJacky/gonginx v0.0.0-20240119024214-c0d76957d0c7/go.mod h1:4fTjBxMoWGOIVnGFSTS9GAZ0yMyiGzTdATQS0krQv18= github.com/0xJacky/pofile v0.2.1 h1:ceNyprJOpo7wPPR0rCOuR1gfjYiS8t9YBc73tSLnlDc= github.com/0xJacky/pofile v0.2.1/go.mod h1:hOZmte1hWostNs9KCwFRhKM7hf0d19zfWosopngij74= github.com/AdamSLevy/jsonrpc2/v14 v14.1.0 h1:Dy3M9aegiI7d7PF1LUdjbVigJReo+QOceYsMyFh9qoE= @@ -753,7 +749,6 @@ github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyY github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/census-instrumentation/opencensus-proto v0.3.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/census-instrumentation/opencensus-proto v0.4.1/go.mod h1:4T9NM4+4Vw91VeyqjLS6ao50K5bOcLKN6Q42XnYaRYw= -github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= @@ -803,8 +798,6 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dgraph-io/ristretto v0.2.0 h1:XAfl+7cmoUDWW/2Lx8TGZQjjxIQ2Ley9DSf52dru4WE= -github.com/dgraph-io/ristretto v0.2.0/go.mod h1:8uBHCU/PBV4Ag0CJrP47b9Ofby5dqWNh4FicAdoqFNU= github.com/dgraph-io/ristretto v1.0.0 h1:SYG07bONKMlFDUYu5pEu3DGAh8c2OFNzKm6G9J4Si84= github.com/dgraph-io/ristretto v1.0.0/go.mod h1:jTi2FiYEhQ1NsMmA7DeBykizjOuY88NhKBkepyu1jPc= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= @@ -1577,6 +1570,8 @@ github.com/tklauser/numcpus v0.9.0/go.mod h1:SN6Nq1O3VychhC1npsWostA+oW+VOQTxZrS github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/transip/gotransip/v6 v6.26.0 h1:Aejfvh8rSp8Mj2GX/RpdBjMCv+Iy/DmgfNgczPDP550= github.com/transip/gotransip/v6 v6.26.0/go.mod h1:x0/RWGRK/zob817O3tfO2xhFoP1vu8YOHORx6Jpk80s= +github.com/tufanbarisyildirim/gonginx v0.0.0-20241013191809-e73b7dd454e8 h1:7yw1yHkylDcu/CwY4hEEe8MycDLo7B9LjcqwhoL8NrM= +github.com/tufanbarisyildirim/gonginx v0.0.0-20241013191809-e73b7dd454e8/go.mod h1:itu4KWRgrfEwGcfNka+rV4houuirUau53i0diN4lG5g= github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= @@ -2467,8 +2462,8 @@ gorm.io/hints v1.1.2 h1:b5j0kwk5p4+3BtDtYqqfY+ATSxjj+6ptPgVveuynn9o= gorm.io/hints v1.1.2/go.mod h1:/ARdpUHAtyEMCh5NNi3tI7FsGh+Cj/MIUlvNxCNCFWg= gorm.io/plugin/dbresolver v1.5.3 h1:wFwINGZZmttuu9h7XpvbDHd8Lf9bb8GNzp/NpAMV2wU= gorm.io/plugin/dbresolver v1.5.3/go.mod h1:TSrVhaUg2DZAWP3PrHlDlITEJmNOkL0tFTjvTEsQ4XE= -gotest.tools/v3 v3.4.0 h1:ZazjZUfuVeZGLAmlKKuyv3IKP5orXcwtOwDQH6YVr6o= -gotest.tools/v3 v3.4.0/go.mod h1:CtbdzLSsqVhDgMtKsx03ird5YTGB3ar27v0u/yKBW5g= +gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU= +gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/internal/cert/channel_writer.go b/internal/cert/channel_writer.go index d9d79d39..7f0ecafa 100644 --- a/internal/cert/channel_writer.go +++ b/internal/cert/channel_writer.go @@ -1,19 +1,19 @@ package cert type ChannelWriter struct { - Ch chan []byte + Ch chan []byte } func NewChannelWriter() *ChannelWriter { - return &ChannelWriter{ - Ch: make(chan []byte, 1024), - } + return &ChannelWriter{ + Ch: make(chan []byte, 1024), + } } func (cw *ChannelWriter) Write(p []byte) (n int, err error) { - n = len(p) - temp := make([]byte, n) - copy(temp, p) - cw.Ch <- temp - return n, nil + n = len(p) + temp := make([]byte, n) + copy(temp, p) + cw.Ch <- temp + return n, nil } diff --git a/internal/cosy/cosy.go b/internal/cosy/cosy.go index 4d81ce61..282b6f59 100644 --- a/internal/cosy/cosy.go +++ b/internal/cosy/cosy.go @@ -1,117 +1,117 @@ package cosy import ( - "github.com/0xJacky/Nginx-UI/internal/logger" - "github.com/gin-gonic/gin" - "github.com/go-playground/validator/v10" - "gorm.io/gorm" + "github.com/0xJacky/Nginx-UI/internal/logger" + "github.com/gin-gonic/gin" + "github.com/go-playground/validator/v10" + "gorm.io/gorm" ) var validate *validator.Validate func init() { - validate = validator.New() + validate = validator.New() } type Ctx[T any] struct { - ctx *gin.Context - rules gin.H - Payload map[string]interface{} - Model T - OriginModel T - table string - tableArgs []interface{} - abort bool - nextHandler *gin.HandlerFunc - skipAssociationsOnCreate bool - beforeDecodeHookFunc []func(ctx *Ctx[T]) - beforeExecuteHookFunc []func(ctx *Ctx[T]) - executedHookFunc []func(ctx *Ctx[T]) - gormScopes []func(tx *gorm.DB) *gorm.DB - preloads []string - scan func(tx *gorm.DB) any - transformer func(*T) any - permanentlyDelete bool - SelectedFields []string - itemKey string + ctx *gin.Context + rules gin.H + Payload map[string]interface{} + Model T + OriginModel T + table string + tableArgs []interface{} + abort bool + nextHandler *gin.HandlerFunc + skipAssociationsOnCreate bool + beforeDecodeHookFunc []func(ctx *Ctx[T]) + beforeExecuteHookFunc []func(ctx *Ctx[T]) + executedHookFunc []func(ctx *Ctx[T]) + gormScopes []func(tx *gorm.DB) *gorm.DB + preloads []string + scan func(tx *gorm.DB) any + transformer func(*T) any + permanentlyDelete bool + SelectedFields []string + itemKey string } func Core[T any](c *gin.Context) *Ctx[T] { - return &Ctx[T]{ - ctx: c, - gormScopes: make([]func(tx *gorm.DB) *gorm.DB, 0), - beforeExecuteHookFunc: make([]func(ctx *Ctx[T]), 0), - beforeDecodeHookFunc: make([]func(ctx *Ctx[T]), 0), - itemKey: "`id`", - skipAssociationsOnCreate: true, - } + return &Ctx[T]{ + ctx: c, + gormScopes: make([]func(tx *gorm.DB) *gorm.DB, 0), + beforeExecuteHookFunc: make([]func(ctx *Ctx[T]), 0), + beforeDecodeHookFunc: make([]func(ctx *Ctx[T]), 0), + itemKey: "`id`", + skipAssociationsOnCreate: true, + } } func (c *Ctx[T]) SetTable(table string, args ...interface{}) *Ctx[T] { - c.table = table - c.tableArgs = args - return c + c.table = table + c.tableArgs = args + return c } func (c *Ctx[T]) SetItemKey(key string) *Ctx[T] { - c.itemKey = key - return c + c.itemKey = key + return c } func (c *Ctx[T]) SetValidRules(rules gin.H) *Ctx[T] { - c.rules = rules + c.rules = rules - return c + return c } func (c *Ctx[T]) SetPreloads(args ...string) *Ctx[T] { - c.preloads = append(c.preloads, args...) - return c + c.preloads = append(c.preloads, args...) + return c } func (c *Ctx[T]) validate() (errs gin.H) { - c.Payload = make(gin.H) + c.Payload = make(gin.H) - _ = c.ctx.ShouldBindJSON(&c.Payload) + _ = c.ctx.ShouldBindJSON(&c.Payload) - errs = validate.ValidateMap(c.Payload, c.rules) + errs = validate.ValidateMap(c.Payload, c.rules) - if len(errs) > 0 { - logger.Debug(errs) - for k := range errs { - errs[k] = c.rules[k] - } - return - } - // Make sure that the key in c.Payload is also the key of rules - validated := make(map[string]interface{}) + if len(errs) > 0 { + logger.Debug(errs) + for k := range errs { + errs[k] = c.rules[k] + } + return + } + // Make sure that the key in c.Payload is also the key of rules + validated := make(map[string]interface{}) - for k, v := range c.Payload { - if _, ok := c.rules[k]; ok { - validated[k] = v - } - } + for k, v := range c.Payload { + if _, ok := c.rules[k]; ok { + validated[k] = v + } + } - c.Payload = validated + c.Payload = validated - return + return } func (c *Ctx[T]) SetScan(scan func(tx *gorm.DB) any) *Ctx[T] { - c.scan = scan - return c + c.scan = scan + return c } func (c *Ctx[T]) SetTransformer(t func(m *T) any) *Ctx[T] { - c.transformer = t - return c + c.transformer = t + return c } func (c *Ctx[T]) AbortWithError(err error) { - c.abort = true - errHandler(c.ctx, err) + c.abort = true + errHandler(c.ctx, err) } func (c *Ctx[T]) Abort() { - c.abort = true + c.abort = true } diff --git a/internal/nginx/build_config.go b/internal/nginx/build_config.go index a4343b81..916535c2 100644 --- a/internal/nginx/build_config.go +++ b/internal/nginx/build_config.go @@ -3,7 +3,7 @@ package nginx import ( "bufio" "fmt" - "github.com/tufanbarisyildirim/gonginx" + "github.com/tufanbarisyildirim/gonginx/dumper" "github.com/tufanbarisyildirim/gonginx/parser" "strings" ) @@ -83,11 +83,11 @@ func (c *NgxConfig) BuildConfig() (content string, err error) { content += fmt.Sprintf("%sserver {\n%s}\n\n", comments, server) } p := parser.NewStringParser(content, parser.WithSkipValidDirectivesErr()) - config, err := p.Parse() + cfg, err := p.Parse() if err != nil { return } - content = gonginx.DumpConfig(config, gonginx.IndentedStyle) + content = dumper.DumpConfig(cfg, dumper.IndentedStyle) return } diff --git a/internal/nginx/format_code.go b/internal/nginx/format_code.go index 84a98042..90f22356 100644 --- a/internal/nginx/format_code.go +++ b/internal/nginx/format_code.go @@ -1,12 +1,12 @@ package nginx import ( - "github.com/tufanbarisyildirim/gonginx" + "github.com/tufanbarisyildirim/gonginx/dumper" "github.com/tufanbarisyildirim/gonginx/parser" ) func (c *NgxConfig) FmtCode() (fmtContent string) { - fmtContent = gonginx.DumpConfig(c.c, gonginx.IndentedStyle) + fmtContent = dumper.DumpConfig(c.c, dumper.IndentedStyle) return } @@ -16,6 +16,6 @@ func FmtCode(content string) (fmtContent string, err error) { if err != nil { return } - fmtContent = gonginx.DumpConfig(c, gonginx.IndentedStyle) + fmtContent = dumper.DumpConfig(c, dumper.IndentedStyle) return } diff --git a/internal/nginx/ngx_conf_parse_test.go b/internal/nginx/ngx_conf_parse_test.go index b4782fc5..964bc10e 100644 --- a/internal/nginx/ngx_conf_parse_test.go +++ b/internal/nginx/ngx_conf_parse_test.go @@ -2,7 +2,7 @@ package nginx import ( "fmt" - "github.com/tufanbarisyildirim/gonginx" + "github.com/tufanbarisyildirim/gonginx/config" "github.com/tufanbarisyildirim/gonginx/parser" "strings" "testing" @@ -32,7 +32,7 @@ func TestNgxConfParse(t *testing.T) { fmt.Println(c) } -func fn(block gonginx.IBlock, deep int) { +func fn(block config.IBlock, deep int) { if block == nil { return } diff --git a/internal/nginx/parse.go b/internal/nginx/parse.go index 148763ac..6ad816b1 100644 --- a/internal/nginx/parse.go +++ b/internal/nginx/parse.go @@ -2,7 +2,7 @@ package nginx import ( "github.com/pkg/errors" - "github.com/tufanbarisyildirim/gonginx" + "github.com/tufanbarisyildirim/gonginx/config" "github.com/tufanbarisyildirim/gonginx/parser" "strings" ) @@ -13,11 +13,11 @@ const ( Upstream = "upstream" ) -func (s *NgxServer) ParseServer(directive gonginx.IDirective) { +func (s *NgxServer) ParseServer(directive config.IDirective) { s.parseServer(directive) } -func (s *NgxServer) parseServer(directive gonginx.IDirective) { +func (s *NgxServer) parseServer(directive config.IDirective) { if directive.GetBlock() == nil { return } @@ -40,10 +40,10 @@ func (s *NgxServer) parseServer(directive gonginx.IDirective) { } } } -func (l *NgxLocation) ParseLocation(directive gonginx.IDirective, deep int) { +func (l *NgxLocation) ParseLocation(directive config.IDirective, deep int) { l.parseLocation(directive, deep) } -func (l *NgxLocation) parseLocation(directive gonginx.IDirective, deep int) { +func (l *NgxLocation) parseLocation(directive config.IDirective, deep int) { if directive.GetBlock() == nil { return } @@ -64,11 +64,11 @@ func (l *NgxLocation) parseLocation(directive gonginx.IDirective, deep int) { } } -func (d *NgxDirective) ParseDirective(directive gonginx.IDirective, deep int) { +func (d *NgxDirective) ParseDirective(directive config.IDirective, deep int) { d.parseDirective(directive, deep) } -func (d *NgxDirective) parseDirective(directive gonginx.IDirective, deep int) { +func (d *NgxDirective) parseDirective(directive config.IDirective, deep int) { if directive.GetBlock() != nil { d.Params += directive.GetName() + " " d.Directive = "" @@ -97,7 +97,7 @@ func (d *NgxDirective) parseDirective(directive gonginx.IDirective, deep int) { } } -func (u *NgxUpstream) parseUpstream(directive gonginx.IDirective) { +func (u *NgxUpstream) parseUpstream(directive config.IDirective) { if directive.GetBlock() == nil { return } @@ -111,7 +111,7 @@ func (u *NgxUpstream) parseUpstream(directive gonginx.IDirective) { } } -func (c *NgxConfig) parseCustom(directive gonginx.IDirective) { +func (c *NgxConfig) parseCustom(directive config.IDirective) { if directive.GetBlock() == nil { return } @@ -127,7 +127,7 @@ func buildComment(c []string) string { return strings.ReplaceAll(strings.Join(c, "\n"), "#", "") } -func parse(block gonginx.IBlock, ngxConfig *NgxConfig) (err error) { +func parse(block config.IBlock, ngxConfig *NgxConfig) (err error) { if block == nil { err = errors.New("block is nil") return diff --git a/internal/nginx/type.go b/internal/nginx/type.go index e583ea07..163d198d 100644 --- a/internal/nginx/type.go +++ b/internal/nginx/type.go @@ -1,7 +1,7 @@ package nginx import ( - "github.com/tufanbarisyildirim/gonginx" + "github.com/tufanbarisyildirim/gonginx/config" "path" "strings" ) @@ -12,7 +12,7 @@ type NgxConfig struct { Upstreams []*NgxUpstream `json:"upstreams"` Servers []*NgxServer `json:"servers"` Custom string `json:"custom"` - c *gonginx.Config + c *config.Config } type NgxServer struct { diff --git a/model/config.go b/model/config.go index 46ffe118..9e27dcf0 100644 --- a/model/config.go +++ b/model/config.go @@ -2,8 +2,8 @@ package model type Config struct { Model - Name string `json:"name"` - Filepath string `json:"filepath"` - SyncNodeIds []int `json:"sync_node_ids" gorm:"serializer:json"` - SyncOverwrite bool `json:"sync_overwrite"` + Name string `json:"name"` + Filepath string `json:"filepath"` + SyncNodeIds []int `json:"sync_node_ids" gorm:"serializer:json"` + SyncOverwrite bool `json:"sync_overwrite"` }