fix directive

This commit is contained in:
kingecg 2023-12-15 21:45:04 +08:00
parent 618a186298
commit 7aa4511286
11 changed files with 266 additions and 9 deletions

BIN
__debug_bin Executable file

Binary file not shown.

View File

@ -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": "/",

130
gohttpd.log Normal file
View File

@ -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

View File

@ -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 {

View File

@ -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()
}
}

View File

@ -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)
}

View File

@ -0,0 +1,5 @@
<html>
<body>
<h1>GoHttpd Running...</h1>
</body>
</html>

55
target/config.json Normal file
View File

@ -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": "/"
}
}
]
}]
}

BIN
target/gohttpd Executable file

Binary file not shown.

38
target/gohttpd.log Normal file
View File

@ -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

1
target/gohttpd.pid Normal file
View File

@ -0,0 +1 @@
115012