From dc6069c8a18abec32e31a03ada9482af6e50d0d8 Mon Sep 17 00:00:00 2001 From: kingecg Date: Sat, 9 Dec 2023 17:08:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=B8=BB=E4=BD=93=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=20=E4=B8=8B=E4=B8=80=E6=AD=A5=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=EF=BC=9A=201.=20=E5=AE=9E=E7=8E=B0=E9=80=9A=E8=BF=87api?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=85=8D=E7=BD=AE=202.=20=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E7=83=AD=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gohttp.go | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/gohttp.go b/gohttp.go index 98c9525..2cabe15 100644 --- a/gohttp.go +++ b/gohttp.go @@ -6,6 +6,7 @@ import ( "os" "git.pyer.club/kingecg/gohttpd/admin" + handler "git.pyer.club/kingecg/gohttpd/hander" "git.pyer.club/kingecg/gohttpd/model" "git.pyer.club/kingecg/gohttpd/server" "git.pyer.club/kingecg/gologger" @@ -26,8 +27,10 @@ func (g *GoHttp) Start() { g.makeServer("admin", port, admin.AdminServerMux) - //TODO: create other server - // } + for _, server := range g.conf.Servers { + sHandler := g.assembleServerMux(server.Paths) + g.makeServer(server.ServerName, server.Port, sHandler) + } for _, listener := range server.ServerManager { listener.Start() @@ -37,13 +40,32 @@ func (g *GoHttp) Start() { g.logger.Info("gohttpd start success") } -func (g *GoHttp) makeServer(name string, port int, mux *server.RestMux) { +func (g *GoHttp) makeServer(name string, port int, mux http.Handler) { s := &http.Server{ Handler: mux, } server.AddServer(name, s, port) } +func (g *GoHttp) assembleServerMux(p []model.HttpPath) http.Handler { + s := http.NewServeMux() + for _, httpPath := range p { + if httpPath.Root != "" { + fileHandler := handler.NewFileHandler(handler.FileHandler{ + Root: httpPath.Root, + Default: httpPath.Default, + }) + s.Handle(httpPath.Path, fileHandler) + } else if len(httpPath.Upstreams) != 0 { + proxyHandler := handler.NewProxyHandler(&httpPath) + s.Handle(httpPath.Path, proxyHandler) + } else { + g.logger.Error("Not supportted server path type :", httpPath.Path) + } + } + return s +} + func (g *GoHttp) Stop() {} func (g *GoHttp) LoadConfig(configPath string) {