diff --git a/__debug_bin b/__debug_bin new file mode 100755 index 0000000..f7167c5 Binary files /dev/null and b/__debug_bin differ diff --git a/config.json b/config.json index c9cbfe0..2b896a6 100644 --- a/config.json +++ b/config.json @@ -6,11 +6,14 @@ "options":{ "file": "gohttpd.log" } + }, + "stdout":{ + "type": "console" } }, "categories": { "default": { - "appenders": [ "out" ], + "appenders": [ "out" ,"stdout"], "level": "debug" } } @@ -20,6 +23,11 @@ "port" : 8088, "username": "admin", "password": "admin", + "directives":[ + "Set-Header Access-Control-Allow-Origin *", + "Set-Header Access-Control-Allow-Methods GET, POST, PUT, DELETE, OPTIONS", + "Set-Header Access-Control-Allow-Headers Content-Type, Authorization, Content-Length, X-Requested-With" + ], "paths": [ { "path": "/", diff --git a/gohttpd.log b/gohttpd.log new file mode 100644 index 0000000..f5f8bb9 --- /dev/null +++ b/gohttpd.log @@ -0,0 +1,130 @@ +[2023-12-15 21:19:44] Server : info - Load config success +[2023-12-15 21:19:44] Server : info - start gohttpd +[2023-12-15 21:19:44] ServerMux : debug - NewServeMux +[2023-12-15 21:19:44] ServerMux : debug - NewServeMux +[2023-12-15 21:19:44] Listener : debug - listen on :8088 +[2023-12-15 21:19:44] Listener : debug - listen on :8080 +[2023-12-15 21:19:44] Server : info - gohttpd start success +[2023-12-15 21:20:07] Server : info - Load config success +[2023-12-15 21:20:07] Server : info - start gohttpd +[2023-12-15 21:20:07] ServerMux : debug - NewServeMux +[2023-12-15 21:20:07] ServerMux : debug - NewServeMux +[2023-12-15 21:20:07] Listener : debug - listen on :8088 +[2023-12-15 21:20:07] Listener : debug - listen on :8080 +[2023-12-15 21:20:07] Server : info - gohttpd start success +[2023-12-15 21:20:21] Server : info - Load config success +[2023-12-15 21:20:21] Server : info - start gohttpd +[2023-12-15 21:20:21] ServerMux : debug - NewServeMux +[2023-12-15 21:20:21] ServerMux : debug - NewServeMux +[2023-12-15 21:20:21] Listener : debug - listen on :8088 +[2023-12-15 21:20:21] Listener : debug - listen on :8080 +[2023-12-15 21:20:21] Server : info - gohttpd start success +[2023-12-15 21:20:32] Server : info - Load config success +[2023-12-15 21:20:32] Server : info - start gohttpd +[2023-12-15 21:20:32] ServerMux : debug - NewServeMux +[2023-12-15 21:22:08] Server : info - Load config success +[2023-12-15 21:22:08] Server : info - start gohttpd +[2023-12-15 21:22:08] ServerMux : debug - NewServeMux +[2023-12-15 21:22:44] Server : info - Load config success +[2023-12-15 21:22:44] Server : info - start gohttpd +[2023-12-15 21:22:44] ServerMux : debug - NewServeMux +[2023-12-15 21:28:05] Server : info - Load config success +[2023-12-15 21:28:05] Server : info - start gohttpd +[2023-12-15 21:28:05] ServerMux : debug - NewServeMux +[2023-12-15 21:28:08] ServerMux : debug - add directive: Set-Header +[2023-12-15 21:28:08] ServerMux : debug - add directive: Set-Header +[2023-12-15 21:28:08] ServerMux : debug - add directive: Set-Header +[2023-12-15 21:28:08] ServerMux : debug - NewServeMux +[2023-12-15 21:28:08] Listener : debug - listen on :8088 +[2023-12-15 21:28:08] Listener : debug - listen on :8080 +[2023-12-15 21:28:08] Server : info - gohttpd start success +[2023-12-15 21:28:16] ServerMux : info - match path: /api/ +[2023-12-15 21:28:18] Route : debug - match route: GET /about +[2023-12-15 21:28:18] URLMatcher : debug - Match for /about +[2023-12-15 21:28:18] Route : debug - Not match matcher reg +[2023-12-15 21:28:19] Route : debug - match route: GET /about +[2023-12-15 21:28:53] ServerMux : info - match path: /api/ +[2023-12-15 21:29:28] Route : debug - match route: GET /about +[2023-12-15 21:29:28] URLMatcher : debug - Match for /about +[2023-12-15 21:29:28] Route : debug - Not match matcher reg +[2023-12-15 21:29:28] Route : debug - match route: GET /about +[2023-12-15 21:31:20] Server : info - Load config success +[2023-12-15 21:31:20] Server : info - start gohttpd +[2023-12-15 21:31:20] ServerMux : debug - NewServeMux +[2023-12-15 21:31:20] ServerMux : debug - add directive: Set-Header +[2023-12-15 21:31:20] ServerMux : debug - add directive: Set-Header +[2023-12-15 21:31:20] ServerMux : debug - add directive: Set-Header +[2023-12-15 21:31:20] ServerMux : debug - NewServeMux +[2023-12-15 21:31:20] Listener : debug - listen on :8080 +[2023-12-15 21:31:20] Listener : debug - listen on :8088 +[2023-12-15 21:31:20] Server : info - gohttpd start success +[2023-12-15 21:31:30] ServerMux : info - match path: /api/ +[2023-12-15 21:31:30] Directive : debug - Set-HeaderAccess-Control-Allow-Origin:* +[2023-12-15 21:31:30] Directive : debug - Set-HeaderAccess-Control-Allow-Methods:GET, +[2023-12-15 21:31:30] Directive : debug - Set-HeaderAccess-Control-Allow-Headers:Content-Type, +[2023-12-15 21:31:30] Route : debug - match route: GET /about +[2023-12-15 21:31:30] URLMatcher : debug - Match for /about +[2023-12-15 21:31:30] Route : debug - Not match matcher reg +[2023-12-15 21:31:30] Route : debug - match route: GET /about +[2023-12-15 21:33:58] Server : info - Load config success +[2023-12-15 21:33:58] Server : info - start gohttpd +[2023-12-15 21:33:58] ServerMux : debug - NewServeMux +[2023-12-15 21:33:58] ServerMux : debug - add directive: Set-Header +[2023-12-15 21:33:58] ServerMux : debug - add directive: Set-Header +[2023-12-15 21:33:58] ServerMux : debug - add directive: Set-Header +[2023-12-15 21:33:58] ServerMux : debug - NewServeMux +[2023-12-15 21:33:58] Listener : debug - listen on :8088 +[2023-12-15 21:33:58] Listener : debug - listen on :8080 +[2023-12-15 21:33:58] Server : info - gohttpd start success +[2023-12-15 21:34:08] ServerMux : info - match path: /api/ +[2023-12-15 21:34:08] Directive : debug - Set-HeaderAccess-Control-Allow-Origin:* +[2023-12-15 21:34:08] Directive : debug - Set-HeaderAccess-Control-Allow-Methods:GET, +[2023-12-15 21:34:08] Directive : debug - Set-HeaderAccess-Control-Allow-Headers:Content-Type, +[2023-12-15 21:34:08] Route : debug - match route: GET /about +[2023-12-15 21:34:08] URLMatcher : debug - Match for /about +[2023-12-15 21:34:08] Route : debug - Not match matcher reg +[2023-12-15 21:34:08] Route : debug - match route: GET /about +[2023-12-15 21:34:38] ServerMux : info - match path: /api/ +[2023-12-15 21:34:38] Directive : debug - Set-HeaderAccess-Control-Allow-Origin:* +[2023-12-15 21:34:38] Directive : debug - Set-HeaderAccess-Control-Allow-Methods:GET, +[2023-12-15 21:34:38] Directive : debug - Set-HeaderAccess-Control-Allow-Headers:Content-Type, +[2023-12-15 21:34:38] Route : debug - match route: GET /about +[2023-12-15 21:34:38] URLMatcher : debug - Match for /about +[2023-12-15 21:34:38] Route : debug - Not match matcher reg +[2023-12-15 21:34:38] Route : debug - match route: GET /about +[2023-12-15 21:35:42] Server : info - Load config success +[2023-12-15 21:35:42] Server : info - start gohttpd +[2023-12-15 21:35:42] ServerMux : debug - NewServeMux +[2023-12-15 21:35:42] ServerMux : debug - add directive: Set-Header +[2023-12-15 21:35:42] ServerMux : debug - add directive: Set-Header +[2023-12-15 21:35:42] ServerMux : debug - add directive: Set-Header +[2023-12-15 21:35:42] ServerMux : debug - NewServeMux +[2023-12-15 21:35:42] Listener : debug - listen on :8088 +[2023-12-15 21:35:42] Listener : debug - listen on :8080 +[2023-12-15 21:35:42] Server : info - gohttpd start success +[2023-12-15 21:35:48] ServerMux : info - match path: /api/ +[2023-12-15 21:35:48] Directive : debug - Set-HeaderAccess-Control-Allow-Origin* +[2023-12-15 21:35:48] Directive : debug - Set-HeaderAccess-Control-Allow-MethodsGET, +[2023-12-15 21:35:48] Directive : debug - Set-HeaderAccess-Control-Allow-HeadersContent-Type, +[2023-12-15 21:35:48] Route : debug - match route: GET /about +[2023-12-15 21:35:48] URLMatcher : debug - Match for /about +[2023-12-15 21:35:48] Route : debug - Not match matcher reg +[2023-12-15 21:35:48] Route : debug - match route: GET /about +[2023-12-15 21:43:27] Server : info - Load config success +[2023-12-15 21:43:27] Server : info - start gohttpd +[2023-12-15 21:43:27] ServerMux : debug - NewServeMux +[2023-12-15 21:43:27] ServerMux : debug - add directive: Set-Header +[2023-12-15 21:43:27] ServerMux : debug - add directive: Set-Header +[2023-12-15 21:43:27] ServerMux : debug - add directive: Set-Header +[2023-12-15 21:43:27] ServerMux : debug - NewServeMux +[2023-12-15 21:43:27] Listener : debug - listen on :8088 +[2023-12-15 21:43:27] Listener : debug - listen on :8080 +[2023-12-15 21:43:27] Server : info - gohttpd start success +[2023-12-15 21:43:35] ServerMux : info - match path: /api/ +[2023-12-15 21:43:35] Directive : debug - Set-Header Access-Control-Allow-Origin:* +[2023-12-15 21:43:35] Directive : debug - Set-Header Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS +[2023-12-15 21:43:35] Directive : debug - Set-Header Access-Control-Allow-Headers:Content-Type, Authorization, Content-Length, X-Requested-With +[2023-12-15 21:43:35] Route : debug - match route: GET /about +[2023-12-15 21:43:35] URLMatcher : debug - Match for /about +[2023-12-15 21:43:35] Route : debug - Not match matcher reg +[2023-12-15 21:43:35] Route : debug - match route: GET /about diff --git a/model/model.go b/model/model.go index 90ad34e..9c76e59 100644 --- a/model/model.go +++ b/model/model.go @@ -28,11 +28,11 @@ type HttpServerConfig struct { Port int `json:"port"` Host string `json:"host"` Paths []HttpPath - Username string `json:"username"` - Password string `json:"password"` - CertFile string `json:"certfile"` - KeyFile string `json:"keyfile"` - Directives string `json:"directives"` + Username string `json:"username"` + Password string `json:"password"` + CertFile string `json:"certfile"` + KeyFile string `json:"keyfile"` + Directives []string `json:"directives"` } type GoHttpdConfig struct { diff --git a/server/directive.go b/server/directive.go index 2ef03eb..e6cb5bb 100644 --- a/server/directive.go +++ b/server/directive.go @@ -1,11 +1,21 @@ package server -import "net/http" +import ( + "fmt" + "net/http" + "strings" + + "git.pyer.club/kingecg/gologger" +) type Directive func(args ...string) Middleware var Add_Header Directive = func(args ...string) Middleware { return func(w http.ResponseWriter, r *http.Request, next func()) { + l := gologger.GetLogger("Directive") + p := args[1:] + params := strings.Join(p, " ") + l.Debug(fmt.Sprintf("Add-Header %s:%s", args[0], params)) w.Header().Add(args[0], args[1]) next() } @@ -13,7 +23,11 @@ var Add_Header Directive = func(args ...string) Middleware { var Set_Header Directive = func(args ...string) Middleware { return func(w http.ResponseWriter, r *http.Request, next func()) { - w.Header().Set(args[0], args[1]) + l := gologger.GetLogger("Directive") + p := args[1:] + params := strings.Join(p, " ") + l.Debug(fmt.Sprintf("Set-Header %s:%s", args[0], params)) + w.Header().Set(args[0], params) next() } } diff --git a/server/server.go b/server/server.go index e369b11..af8bee4 100644 --- a/server/server.go +++ b/server/server.go @@ -211,24 +211,30 @@ func (s *ServerMux) Handle(pattern string, handler http.Handler) { } func (s *ServerMux) AddDirective(directiveStr string) { - //TODO: 根据字符串内容生成一个中间件链,等directive实现再来补充逻辑 + l := logger.GetLogger("ServerMux") strs := strings.Split(directiveStr, " ") directiveName := strs[0] params := strs[1:] directive, ok := DirectiveMap[directiveName] if ok { + l.Debug(fmt.Sprintf("add directive: %s", directiveName)) s.directiveHandlers.Add(directive(params...)) + } else { + l.Error(fmt.Sprintf("directive not found: %s", directiveName)) } } func (s *ServerMux) ServeHTTP(w http.ResponseWriter, r *http.Request) { + l := logger.GetLogger("ServerMux") for _, p := range s.paths { if strings.HasPrefix(r.URL.Path, p) { + l.Info(fmt.Sprintf("match path: %s", p)) s.directiveHandlers.ServeHTTP(w, r) s.handlers[p].ServeHTTP(w, r) return } } + l.Info(fmt.Sprintf("not match path: %s", r.URL.Path)) http.NotFound(w, r) } diff --git a/target/adminui/index.html b/target/adminui/index.html new file mode 100644 index 0000000..30bd6d0 --- /dev/null +++ b/target/adminui/index.html @@ -0,0 +1,5 @@ + + +

GoHttpd Running...

+ + \ No newline at end of file diff --git a/target/config.json b/target/config.json new file mode 100644 index 0000000..88943a9 --- /dev/null +++ b/target/config.json @@ -0,0 +1,55 @@ +{ + "logging" :{ + "appenders": { + "out" :{ + "type": "file", + "options":{ + "file": "gohttpd.log" + } + } + }, + "categories": { + "default": { + "appenders": [ "out" ], + "level": "debug" + } + } + }, + "admin" : { + "name": "admin", + "port" : 8088, + "username": "admin", + "password": "admin", + "directives":[ + "Set_Header Access-Control-Allow-Origin *", + "Set_Header Access-Control-Allow-Methods GET, POST, PUT, DELETE, OPTIONS", + "Set_Header Access-Control-Allow-Headers Content-Type, Authorization, Content-Length, X-Requested-With" + ], + "paths": [ + { + "path": "/", + "root": "./adminui", + "default": "index.html" + } + ] + }, + "servers":[{ + "port" : 8080, + "name":"demo", + "paths":[ + { + "path": "/", + "root": "./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..6f6a8b5 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..345f4ba --- /dev/null +++ b/target/gohttpd.log @@ -0,0 +1,38 @@ +[2023-12-15 21:12:54] Server : info - Load config success +[2023-12-15 21:12:54] Server : info - Load config success +[2023-12-15 21:12:54] daemon : debug - Starting new task +[2023-12-15 21:12:54] Server : info - Load config success +[2023-12-15 21:12:54] Server : info - start gohttpd +[2023-12-15 21:12:54] ServerMux : debug - NewServeMux +[2023-12-15 21:12:54] ServerMux : debug - NewServeMux +[2023-12-15 21:12:54] Listener : debug - listen on :8088 +[2023-12-15 21:12:54] Listener : debug - listen on :8080 +[2023-12-15 21:12:54] Server : info - gohttpd start success +[2023-12-15 21:13:25] Server : info - Load config success +[2023-12-15 21:13:25] daemon : debug - Send signal:114284 terminated +[2023-12-15 21:13:25] daemon : debug - Stop it +[2023-12-15 21:13:25] Server : info - Exit +[2023-12-15 21:13:25] daemon : debug - daemon is stopped, exit now +[2023-12-15 21:15:49] Server : info - Load config success +[2023-12-15 21:15:49] Server : info - Load config success +[2023-12-15 21:15:49] daemon : debug - Starting new task +[2023-12-15 21:15:49] Server : info - Load config success +[2023-12-15 21:15:49] Server : info - start gohttpd +[2023-12-15 21:15:49] ServerMux : debug - NewServeMux +[2023-12-15 21:15:49] ServerMux : debug - NewServeMux +[2023-12-15 21:15:49] Listener : debug - listen on :8088 +[2023-12-15 21:15:49] Listener : debug - listen on :8080 +[2023-12-15 21:15:49] Server : info - gohttpd start success +[2023-12-15 21:16:18] filehandler : debug - access:/home/kingecg/code/gohttp/target/adminui +[2023-12-15 21:16:18] filehandler : debug - access:/home/kingecg/code/gohttp/target/adminui/index.html +[2023-12-15 21:16:18] filehandler : debug - access:/home/kingecg/code/gohttp/target/adminui/favicon.ico +[2023-12-15 21:16:18] filehandler : error - access file error:/home/kingecg/code/gohttp/target/adminui/favicon.icostat /home/kingecg/code/gohttp/target/adminui/favicon.ico: no such file or directory +[2023-12-15 21:17:10] Route : debug - match route: GET /about +[2023-12-15 21:17:10] URLMatcher : debug - Match for /about +[2023-12-15 21:17:10] Route : debug - Not match matcher reg +[2023-12-15 21:17:10] Route : debug - match route: GET /about +[2023-12-15 21:17:44] Server : info - Load config success +[2023-12-15 21:17:44] daemon : debug - Send signal:115012 terminated +[2023-12-15 21:17:44] daemon : debug - Stop it +[2023-12-15 21:17:44] Server : info - Exit +[2023-12-15 21:17:44] daemon : debug - daemon is stopped, exit now diff --git a/target/gohttpd.pid b/target/gohttpd.pid new file mode 100644 index 0000000..2df7c84 --- /dev/null +++ b/target/gohttpd.pid @@ -0,0 +1 @@ +115012 \ No newline at end of file