mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 02:15:48 +02:00
style: format go code with tab indent #605
This commit is contained in:
parent
96cff98c66
commit
598d91a417
15 changed files with 244 additions and 251 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue