feat: 增加网站创建功能
This commit is contained in:

committed by
zhengkunwang223

parent
a1ac689a5e
commit
ef789cdfea
@@ -16,9 +16,11 @@ func NewServer(directive IDirective) (*Server, error) {
|
||||
server.Comment = block.GetComment()
|
||||
directives := block.GetDirectives()
|
||||
for _, dir := range directives {
|
||||
if dir.GetName() == "listen" {
|
||||
|
||||
switch dir.GetName() {
|
||||
case "listen":
|
||||
server.Listens = append(server.Listens, NewServerListen(dir.GetParameters()))
|
||||
} else {
|
||||
default:
|
||||
server.Directives = append(server.Directives, dir)
|
||||
}
|
||||
}
|
||||
@@ -63,6 +65,56 @@ func (s *Server) AddListen(bind string, defaultServer bool, params ...string) {
|
||||
s.Listens = append(s.Listens, listen)
|
||||
}
|
||||
|
||||
func (s *Server) UpdateListen(bind string, defaultServer bool, params ...string) {
|
||||
listen := &ServerListen{
|
||||
Bind: bind,
|
||||
Parameters: params,
|
||||
}
|
||||
if defaultServer {
|
||||
listen.DefaultServer = DefaultServer
|
||||
}
|
||||
var newListens []*ServerListen
|
||||
for _, li := range s.Listens {
|
||||
if li.Bind == bind {
|
||||
newListens = append(newListens, listen)
|
||||
} else {
|
||||
newListens = append(newListens, li)
|
||||
}
|
||||
}
|
||||
|
||||
s.Listens = newListens
|
||||
}
|
||||
|
||||
func (s *Server) UpdateServerName(names []string) {
|
||||
serverNameDirective := Directive{
|
||||
Name: "server_name",
|
||||
Parameters: names,
|
||||
}
|
||||
s.UpdateDirectives("server_name", serverNameDirective)
|
||||
}
|
||||
|
||||
func (s *Server) UpdateRootProxy(proxy []string) {
|
||||
//TODD 根据ID修改
|
||||
dirs := s.FindDirectives("location")
|
||||
for _, dir := range dirs {
|
||||
location, ok := dir.(*Location)
|
||||
if ok && location.Match == "/" {
|
||||
newDir := Directive{
|
||||
Name: "location",
|
||||
Parameters: []string{"/"},
|
||||
Block: &Block{},
|
||||
}
|
||||
block := &Block{}
|
||||
block.Directives = append(block.Directives, &Directive{
|
||||
Name: "proxy_pass",
|
||||
Parameters: proxy,
|
||||
})
|
||||
newDir.Block = block
|
||||
s.UpdateDirectives("location", newDir)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) RemoveListenByBind(bind string) {
|
||||
index := 0
|
||||
listens := s.Listens
|
||||
@@ -105,3 +157,19 @@ func (s *Server) AddDirectives(directive Directive) {
|
||||
directives := append(s.Directives, &directive)
|
||||
s.Directives = directives
|
||||
}
|
||||
|
||||
func (s *Server) RemoveDirectives(names []string) {
|
||||
nameMaps := make(map[string]struct{}, len(names))
|
||||
for _, name := range names {
|
||||
nameMaps[name] = struct{}{}
|
||||
}
|
||||
directives := s.GetDirectives()
|
||||
var newDirectives []IDirective
|
||||
for _, dir := range directives {
|
||||
if _, ok := nameMaps[dir.GetName()]; ok {
|
||||
continue
|
||||
}
|
||||
newDirectives = append(newDirectives, dir)
|
||||
}
|
||||
s.Directives = newDirectives
|
||||
}
|
||||
|
Reference in New Issue
Block a user