Compare commits

..

No commits in common. "f86fa0112e0ee99b0685052ed86dc4020b1283e5" and "618a1862985ee84ebf031869613eecf5fe5910c6" have entirely different histories.

4 changed files with 9 additions and 37 deletions

View File

@ -6,14 +6,11 @@
"options":{
"file": "gohttpd.log"
}
},
"stdout":{
"type": "console"
}
},
"categories": {
"default": {
"appenders": [ "out" ,"stdout"],
"appenders": [ "out" ],
"level": "debug"
}
}
@ -23,11 +20,6 @@
"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,21 +1,11 @@
package server
import (
"fmt"
"net/http"
"strings"
"git.pyer.club/kingecg/gologger"
)
import "net/http"
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()
}
@ -23,11 +13,7 @@ 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()) {
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)
w.Header().Set(args[0], args[1])
next()
}
}

View File

@ -211,30 +211,24 @@ func (s *ServerMux) Handle(pattern string, handler http.Handler) {
}
func (s *ServerMux) AddDirective(directiveStr string) {
l := logger.GetLogger("ServerMux")
//TODO: 根据字符串内容生成一个中间件链等directive实现再来补充逻辑
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)
}