Compare commits
2 Commits
618a186298
...
f86fa0112e
Author | SHA1 | Date |
---|---|---|
kingecg | f86fa0112e | |
kingecg | 7aa4511286 |
10
config.json
10
config.json
|
@ -6,11 +6,14 @@
|
||||||
"options":{
|
"options":{
|
||||||
"file": "gohttpd.log"
|
"file": "gohttpd.log"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"stdout":{
|
||||||
|
"type": "console"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"categories": {
|
"categories": {
|
||||||
"default": {
|
"default": {
|
||||||
"appenders": [ "out" ],
|
"appenders": [ "out" ,"stdout"],
|
||||||
"level": "debug"
|
"level": "debug"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,6 +23,11 @@
|
||||||
"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": "/",
|
||||||
|
|
|
@ -28,11 +28,11 @@ type HttpServerConfig struct {
|
||||||
Port int `json:"port"`
|
Port int `json:"port"`
|
||||||
Host string `json:"host"`
|
Host string `json:"host"`
|
||||||
Paths []HttpPath
|
Paths []HttpPath
|
||||||
Username string `json:"username"`
|
Username string `json:"username"`
|
||||||
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,11 +1,21 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import "net/http"
|
import (
|
||||||
|
"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()
|
||||||
}
|
}
|
||||||
|
@ -13,7 +23,11 @@ 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()) {
|
||||||
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()
|
next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -211,24 +211,30 @@ func (s *ServerMux) Handle(pattern string, handler http.Handler) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ServerMux) AddDirective(directiveStr string) {
|
func (s *ServerMux) AddDirective(directiveStr string) {
|
||||||
//TODO: 根据字符串内容生成一个中间件链,等directive实现再来补充逻辑
|
l := logger.GetLogger("ServerMux")
|
||||||
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