完成主体功能

下一步任务:
1. 实现通过api更新配置
2. 实现热加载
This commit is contained in:
kingecg 2023-12-09 17:08:31 +08:00
parent b6e7396ef5
commit dc6069c8a1
1 changed files with 25 additions and 3 deletions

View File

@ -6,6 +6,7 @@ import (
"os" "os"
"git.pyer.club/kingecg/gohttpd/admin" "git.pyer.club/kingecg/gohttpd/admin"
handler "git.pyer.club/kingecg/gohttpd/hander"
"git.pyer.club/kingecg/gohttpd/model" "git.pyer.club/kingecg/gohttpd/model"
"git.pyer.club/kingecg/gohttpd/server" "git.pyer.club/kingecg/gohttpd/server"
"git.pyer.club/kingecg/gologger" "git.pyer.club/kingecg/gologger"
@ -26,8 +27,10 @@ func (g *GoHttp) Start() {
g.makeServer("admin", port, admin.AdminServerMux) 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 { for _, listener := range server.ServerManager {
listener.Start() listener.Start()
@ -37,13 +40,32 @@ func (g *GoHttp) Start() {
g.logger.Info("gohttpd start success") 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{ s := &http.Server{
Handler: mux, Handler: mux,
} }
server.AddServer(name, s, port) 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) Stop() {}
func (g *GoHttp) LoadConfig(configPath string) { func (g *GoHttp) LoadConfig(configPath string) {