Compare commits
No commits in common. "f86fa0112e0ee99b0685052ed86dc4020b1283e5" and "618a1862985ee84ebf031869613eecf5fe5910c6" have entirely different histories.
f86fa0112e
...
618a186298
10
config.json
10
config.json
|
@ -6,14 +6,11 @@
|
||||||
"options":{
|
"options":{
|
||||||
"file": "gohttpd.log"
|
"file": "gohttpd.log"
|
||||||
}
|
}
|
||||||
},
|
|
||||||
"stdout":{
|
|
||||||
"type": "console"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"categories": {
|
"categories": {
|
||||||
"default": {
|
"default": {
|
||||||
"appenders": [ "out" ,"stdout"],
|
"appenders": [ "out" ],
|
||||||
"level": "debug"
|
"level": "debug"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,11 +20,6 @@
|
||||||
"port" : 8088,
|
"port" : 8088,
|
||||||
"username": "admin",
|
"username": "admin",
|
||||||
"password": "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": [
|
"paths": [
|
||||||
{
|
{
|
||||||
"path": "/",
|
"path": "/",
|
||||||
|
|
|
@ -32,7 +32,7 @@ type HttpServerConfig struct {
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
CertFile string `json:"certfile"`
|
CertFile string `json:"certfile"`
|
||||||
KeyFile string `json:"keyfile"`
|
KeyFile string `json:"keyfile"`
|
||||||
Directives []string `json:"directives"`
|
Directives string `json:"directives"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type GoHttpdConfig struct {
|
type GoHttpdConfig struct {
|
||||||
|
|
|
@ -1,21 +1,11 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import "net/http"
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"git.pyer.club/kingecg/gologger"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Directive func(args ...string) Middleware
|
type Directive func(args ...string) Middleware
|
||||||
|
|
||||||
var Add_Header Directive = func(args ...string) Middleware {
|
var Add_Header Directive = func(args ...string) Middleware {
|
||||||
return func(w http.ResponseWriter, r *http.Request, next func()) {
|
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])
|
w.Header().Add(args[0], args[1])
|
||||||
next()
|
next()
|
||||||
}
|
}
|
||||||
|
@ -23,11 +13,7 @@ var Add_Header Directive = func(args ...string) Middleware {
|
||||||
|
|
||||||
var Set_Header Directive = func(args ...string) Middleware {
|
var Set_Header Directive = func(args ...string) Middleware {
|
||||||
return func(w http.ResponseWriter, r *http.Request, next func()) {
|
return func(w http.ResponseWriter, r *http.Request, next func()) {
|
||||||
l := gologger.GetLogger("Directive")
|
w.Header().Set(args[0], args[1])
|
||||||
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()
|
next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -211,30 +211,24 @@ func (s *ServerMux) Handle(pattern string, handler http.Handler) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ServerMux) AddDirective(directiveStr string) {
|
func (s *ServerMux) AddDirective(directiveStr string) {
|
||||||
l := logger.GetLogger("ServerMux")
|
//TODO: 根据字符串内容生成一个中间件链,等directive实现再来补充逻辑
|
||||||
strs := strings.Split(directiveStr, " ")
|
strs := strings.Split(directiveStr, " ")
|
||||||
directiveName := strs[0]
|
directiveName := strs[0]
|
||||||
params := strs[1:]
|
params := strs[1:]
|
||||||
directive, ok := DirectiveMap[directiveName]
|
directive, ok := DirectiveMap[directiveName]
|
||||||
if ok {
|
if ok {
|
||||||
l.Debug(fmt.Sprintf("add directive: %s", directiveName))
|
|
||||||
s.directiveHandlers.Add(directive(params...))
|
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) {
|
func (s *ServerMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
l := logger.GetLogger("ServerMux")
|
|
||||||
for _, p := range s.paths {
|
for _, p := range s.paths {
|
||||||
if strings.HasPrefix(r.URL.Path, p) {
|
if strings.HasPrefix(r.URL.Path, p) {
|
||||||
l.Info(fmt.Sprintf("match path: %s", p))
|
|
||||||
s.directiveHandlers.ServeHTTP(w, r)
|
s.directiveHandlers.ServeHTTP(w, r)
|
||||||
s.handlers[p].ServeHTTP(w, r)
|
s.handlers[p].ServeHTTP(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
l.Info(fmt.Sprintf("not match path: %s", r.URL.Path))
|
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue