Compare commits

...

2 Commits

Author SHA1 Message Date
kingecg f86fa0112e remove debug log 2023-12-15 21:46:06 +08:00
kingecg 7aa4511286 fix directive 2023-12-15 21:45:04 +08:00
4 changed files with 37 additions and 9 deletions

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

View File

@ -32,7 +32,7 @@ type HttpServerConfig struct {
Password string `json:"password"`
CertFile string `json:"certfile"`
KeyFile string `json:"keyfile"`
Directives string `json:"directives"`
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)
}