diff --git a/config.json b/config.json index ffc5b51..e4aaa5d 100644 --- a/config.json +++ b/config.json @@ -16,11 +16,14 @@ } }, "admin" : { - "port" : 8088 + "name": "admin", + "port" : 8088, + "username": "admin", + "password": "admin" }, "servers":[{ "port" : 8080, - "servername":"test", + "name":"test", "paths":[ { "path": "/", diff --git a/gohttp.go b/gohttp.go index 2b92ac2..1b10cff 100644 --- a/gohttp.go +++ b/gohttp.go @@ -21,16 +21,12 @@ func (g *GoHttp) Start() { g.logger = gologger.GetLogger("Server") g.logger.Info("start gohttpd") // if g.conf != nil { - port := model.DefaultAdminConfig.Port - if conf.Admin != nil { - port = conf.Admin.Port - } - g.makeServer("admin", port, admin.AdminServerMux) + g.makeServer(conf.Admin, admin.AdminServerMux) for _, server := range conf.Servers { sHandler := g.assembleServerMux(server.Paths) - g.makeServer(server.ServerName, server.Port, sHandler) + g.makeServer(server, sHandler) } for _, listener := range server.ServerManager { @@ -41,11 +37,17 @@ func (g *GoHttp) Start() { g.logger.Info("gohttpd start success") } -func (g *GoHttp) makeServer(name string, port int, mux http.Handler) { +func (g *GoHttp) makeServer(conf *model.HttpServerConfig, mux http.Handler) { s := &http.Server{ Handler: mux, } - server.AddServer(name, s, port) + name := conf.Name + port := conf.Port + ss := &server.Server{ + Server: s, + Conf: conf, + } + server.AddServer(name, ss, port) } func (g *GoHttp) assembleServerMux(p []model.HttpPath) http.Handler { @@ -78,7 +80,31 @@ func LoadConfig(configPath string) { // read content from cpath content, _ := os.ReadFile(cpath) json.Unmarshal(content, &model.Config) + //normalize path in config + for _, appender := range model.Config.Logging.Appenders { + if appender.Type == "file" { + appender.Options["file"] = NormalizePath(appender.Options["file"].(string)) + } + } + normalizeServer(model.Config.Admin) + for _, server := range model.Config.Servers { + normalizeServer(server) + } gologger.Configure(model.Config.Logging) logger := gologger.GetLogger("Server") logger.Info("Load config success") } + +func normalizeServer(server *model.HttpServerConfig) { + for _, path := range server.Paths { + if path.Root != "" { + path.Root = NormalizePath(path.Root) + } + } + if server.CertFile != "" { + server.CertFile = NormalizePath(server.CertFile) + } + if server.KeyFile != "" { + server.KeyFile = NormalizePath(server.KeyFile) + } +} diff --git a/main.go b/main.go index e805037..4a7ba10 100644 --- a/main.go +++ b/main.go @@ -14,8 +14,10 @@ func main() { daemon.Start() } +var waiter chan os.Signal + func start(g *godaemon.GoDaemon) { - var waiter = make(chan os.Signal, 1) // buffered channel + waiter = make(chan os.Signal, 1) // buffered channel signal.Notify(waiter, syscall.SIGTERM, syscall.SIGINT) httpd := &GoHttp{} @@ -24,6 +26,7 @@ func start(g *godaemon.GoDaemon) { // blocks here until there's a signal <-waiter + httpd.logger.Info("Exit") } func stop(g *godaemon.GoDaemon) { diff --git a/model/model.go b/model/model.go index 5055517..8979ef5 100644 --- a/model/model.go +++ b/model/model.go @@ -23,6 +23,8 @@ type HttpServerConfig struct { Paths []HttpPath Username string `json:"username"` Password string `json:"password"` + CertFile string `json:"certfile"` + KeyFile string `json:"keyfile"` } type GoHttpdConfig struct { diff --git a/server/manager.go b/server/manager.go index ceaf22e..435943e 100644 --- a/server/manager.go +++ b/server/manager.go @@ -10,7 +10,7 @@ import ( "bufio" "io" - "git.pyer.club/kingecg/gologger" + "git.pyer.club/kingecg/gohttpd/model" logger "git.pyer.club/kingecg/gologger" "github.com/soheilhy/cmux" ) @@ -30,13 +30,17 @@ func makeMatcher(name string, s *ServerListener) cmux.Matcher { } } +type Server struct { + Conf *model.HttpServerConfig + *http.Server +} type ServerListener struct { port int listener cmux.CMux - servers map[string]*http.Server + servers map[string]*Server } -func (s *ServerListener) AddServer(name string, server *http.Server) { +func (s *ServerListener) AddServer(name string, server *Server) { s.servers[name] = server } @@ -51,6 +55,9 @@ func (s *ServerListener) StartServer(name string) error { } l := s.listener.Match(makeMatcher(name, s)) + if server.Conf.CertFile != "" && server.Conf.KeyFile != "" { + return server.ServeTLS(l, server.Conf.CertFile, server.Conf.KeyFile) + } return server.Serve(l) } @@ -75,7 +82,7 @@ func (s *ServerListener) ShutDown() { } func (s *ServerListener) Serve() { - l := gologger.GetLogger("Listener") + l := logger.GetLogger("Listener") l.Debug("listen on :", s.port) go s.listener.Serve() } @@ -89,11 +96,11 @@ func NewServerListener(port int) *ServerListener { l.Error("Listen error:", err) } muxer := cmux.New(l) - s := &ServerListener{port: port, listener: muxer, servers: make(map[string]*http.Server)} + s := &ServerListener{port: port, listener: muxer, servers: make(map[string]*Server)} return s } -func AddServer(name string, server *http.Server, port int) { +func AddServer(name string, server *Server, port int) { listenStr := fmt.Sprintf(":%d", port) listener, ok := ServerManager[listenStr] if !ok { diff --git a/server/server.go b/server/server.go index 90d6f87..cfc8ef5 100644 --- a/server/server.go +++ b/server/server.go @@ -2,9 +2,12 @@ package server import ( "context" + "fmt" "net/http" "sort" "strings" + + "git.pyer.club/kingecg/gologger" ) type RequestCtxKey string @@ -25,11 +28,14 @@ func (route *Route) ServeHTTP(w http.ResponseWriter, r *http.Request) { } func (route *Route) Match(r *http.Request) bool { + l := gologger.GetLogger("Route") + l.Debug(fmt.Sprintf("match route: %s %s", r.Method, r.URL.Path)) if route.Method != "" && route.Method != r.Method { + l.Debug("method not match") return false } - if route.matcher != nil { + if route.matcher != nil && route.matcher.Reg != nil { params, matched := MatchUrlParam(r.URL.Path, route.matcher) if matched { ctx := r.Context() @@ -39,6 +45,7 @@ func (route *Route) Match(r *http.Request) bool { } return true } + l.Debug("Not match matcher reg") return false } if route.Path == "" { diff --git a/server/url.go b/server/url.go index e7e4235..2c46ed3 100644 --- a/server/url.go +++ b/server/url.go @@ -4,6 +4,8 @@ import ( "net/url" "regexp" "strings" + + "git.pyer.club/kingecg/gologger" ) type UrlParamMatcher struct { @@ -49,6 +51,8 @@ func ParseUrl(u string) UrlParamMatcher { } func MatchUrlParam(u string, matcher *UrlParamMatcher) (map[string]string, bool) { + l := gologger.GetLogger("URLMatcher") + l.Debug("Match for ", u) if matcher.Reg != nil { uo, _ := url.Parse(u) if uo.Path == "" || uo.Path == "/" { diff --git a/target/config.json b/target/config.json new file mode 100644 index 0000000..e4aaa5d --- /dev/null +++ b/target/config.json @@ -0,0 +1,43 @@ +{ + "logging" :{ + "appenders": { + "out" :{ + "type": "file", + "options":{ + "file": "gohttpd.log" + } + } + }, + "categories": { + "default": { + "appenders": [ "out" ], + "level": "debug" + } + } + }, + "admin" : { + "name": "admin", + "port" : 8088, + "username": "admin", + "password": "admin" + }, + "servers":[{ + "port" : 8080, + "name":"test", + "paths":[ + { + "path": "/", + "root": "/home/kingecg/code/gohttp/public/", + "default": "index.html" + }, + { + "path": "/ws", + "upstreams":["http://localhost:3000"], + "pathrewrite": { + "replace": "/ws", + "with": "/" + } + } + ] + }] +} \ No newline at end of file diff --git a/target/gohttpd b/target/gohttpd new file mode 100755 index 0000000..adb6cc7 Binary files /dev/null and b/target/gohttpd differ diff --git a/target/gohttpd.log b/target/gohttpd.log new file mode 100644 index 0000000..3da8881 --- /dev/null +++ b/target/gohttpd.log @@ -0,0 +1,12 @@ +[2023-12-12 15:27:25] Server : info - Load config success +[2023-12-12 15:27:25] Server : info - Load config success +[2023-12-12 15:27:25] daemon : debug - Starting new task +[2023-12-12 15:27:25] Server : info - Load config success +[2023-12-12 15:27:25] Server : info - start gohttpd +[2023-12-12 15:27:25] Listener : debug - listen on :8088 +[2023-12-12 15:27:25] Listener : debug - listen on :8080 +[2023-12-12 15:27:25] Server : info - gohttpd start success +[2023-12-12 15:27:37] Route : debug - match route: GET /about +[2023-12-12 18:31:09] Server : info - Exit +[2023-12-12 18:31:09] daemon : debug - Stop it +[2023-12-12 18:31:09] daemon : debug - daemon is stopped, exit now diff --git a/target/gohttpd.pid b/target/gohttpd.pid new file mode 100644 index 0000000..c432996 --- /dev/null +++ b/target/gohttpd.pid @@ -0,0 +1 @@ +39527 \ No newline at end of file diff --git a/util.go b/util.go index 4ae961c..106eaa2 100644 --- a/util.go +++ b/util.go @@ -13,3 +13,14 @@ func GetExecDir() string { } return filepath.Dir(execPath) } + +func NormalizePath(path string) string { + // return filepath.ToSlash(filepath.Clean(path)) + p := filepath.ToSlash(path) + if filepath.IsAbs(p) { + return p + } else { + return filepath.Join(GetExecDir(), p) + } + +}