feat: 增加nginx config 格式化工具
This commit is contained in:

committed by
zhengkunwang223

parent
7dad47464d
commit
a1ac689a5e
107
backend/utils/nginx/components/server.go
Normal file
107
backend/utils/nginx/components/server.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package components
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
Comment string
|
||||
Listens []*ServerListen
|
||||
Directives []IDirective
|
||||
}
|
||||
|
||||
func NewServer(directive IDirective) (*Server, error) {
|
||||
server := &Server{}
|
||||
if block := directive.GetBlock(); block != nil {
|
||||
server.Comment = block.GetComment()
|
||||
directives := block.GetDirectives()
|
||||
for _, dir := range directives {
|
||||
if dir.GetName() == "listen" {
|
||||
server.Listens = append(server.Listens, NewServerListen(dir.GetParameters()))
|
||||
} else {
|
||||
server.Directives = append(server.Directives, dir)
|
||||
}
|
||||
}
|
||||
return server, nil
|
||||
}
|
||||
return nil, errors.New("server directive must have a block")
|
||||
}
|
||||
|
||||
func (s *Server) GetName() string {
|
||||
return "server"
|
||||
}
|
||||
|
||||
func (s *Server) GetParameters() []string {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
func (s *Server) GetBlock() IBlock {
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Server) GetComment() string {
|
||||
return s.Comment
|
||||
}
|
||||
|
||||
func (s *Server) GetDirectives() []IDirective {
|
||||
directives := make([]IDirective, 0)
|
||||
for _, ls := range s.Listens {
|
||||
directives = append(directives, ls)
|
||||
}
|
||||
directives = append(directives, s.Directives...)
|
||||
return directives
|
||||
}
|
||||
|
||||
func (s *Server) AddListen(bind string, defaultServer bool, params ...string) {
|
||||
listen := &ServerListen{
|
||||
Bind: bind,
|
||||
Parameters: params,
|
||||
}
|
||||
if defaultServer {
|
||||
listen.DefaultServer = DefaultServer
|
||||
}
|
||||
s.Listens = append(s.Listens, listen)
|
||||
}
|
||||
|
||||
func (s *Server) RemoveListenByBind(bind string) {
|
||||
index := 0
|
||||
listens := s.Listens
|
||||
for _, listen := range s.Listens {
|
||||
if listen.Bind != bind || len(listen.Parameters) > 0 {
|
||||
listens[index] = listen
|
||||
index++
|
||||
}
|
||||
}
|
||||
s.Listens = listens
|
||||
}
|
||||
|
||||
func (s *Server) FindDirectives(directiveName string) []IDirective {
|
||||
directives := make([]IDirective, 0)
|
||||
for _, directive := range s.Directives {
|
||||
if directive.GetName() == directiveName {
|
||||
directives = append(directives, directive)
|
||||
}
|
||||
if directive.GetBlock() != nil {
|
||||
directives = append(directives, directive.GetBlock().FindDirectives(directiveName)...)
|
||||
}
|
||||
}
|
||||
|
||||
return directives
|
||||
}
|
||||
|
||||
func (s *Server) UpdateDirectives(directiveName string, directive Directive) {
|
||||
directives := make([]IDirective, 0)
|
||||
for _, dir := range s.Directives {
|
||||
if dir.GetName() == directiveName {
|
||||
directives = append(directives, &directive)
|
||||
} else {
|
||||
directives = append(directives, dir)
|
||||
}
|
||||
}
|
||||
s.Directives = directives
|
||||
}
|
||||
|
||||
func (s *Server) AddDirectives(directive Directive) {
|
||||
directives := append(s.Directives, &directive)
|
||||
s.Directives = directives
|
||||
}
|
Reference in New Issue
Block a user