fix path match
This commit is contained in:
parent
5fdb7de83d
commit
039643e08e
38
config.json
38
config.json
|
@ -40,40 +40,18 @@
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"servers": [
|
"servers": [
|
||||||
{
|
|
||||||
"port": 3001,
|
|
||||||
"name": "installui",
|
|
||||||
"directives": [
|
|
||||||
"Gzip_Response"
|
|
||||||
],
|
|
||||||
"paths":[
|
|
||||||
{
|
|
||||||
"path": "/",
|
|
||||||
"upstreams": [
|
|
||||||
"http://localhost:4200"
|
|
||||||
],
|
|
||||||
"directives": [
|
|
||||||
"HostSchemas $target"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"path":"/ib",
|
|
||||||
"upstreams": [
|
|
||||||
"http://localhost:8080"
|
|
||||||
],
|
|
||||||
"directives": [
|
|
||||||
"HostSchemas $target"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"port": 3000,
|
"port": 3000,
|
||||||
"name": "cloudops",
|
"name": "cloudops",
|
||||||
"directives": [
|
|
||||||
"Gzip_Response"
|
|
||||||
],
|
|
||||||
"paths": [
|
"paths": [
|
||||||
|
|
||||||
|
{
|
||||||
|
"path": "/static",
|
||||||
|
"root": "/home/kingecg/work/angular13/cloudops/static",
|
||||||
|
"default": "index.html"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"path": "/",
|
"path": "/",
|
||||||
"upstreams": [
|
"upstreams": [
|
||||||
|
|
|
@ -2,6 +2,7 @@ package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"mime"
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -13,19 +14,24 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type FileHandler struct {
|
type FileHandler struct {
|
||||||
|
WPath string
|
||||||
Root string
|
Root string
|
||||||
Default string
|
Default string
|
||||||
http.FileSystem
|
http.FileSystem
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f FileHandler) String() string {
|
||||||
|
return fmt.Sprintf("FileHandler:{WPath:%s,Root:%s,Default:%s}", f.WPath, f.Root, f.Default)
|
||||||
|
}
|
||||||
func (f FileHandler) Open(name string) (http.File, error) {
|
func (f FileHandler) Open(name string) (http.File, error) {
|
||||||
l := gologger.GetLogger("filehandler")
|
l := gologger.GetLogger("filehandler")
|
||||||
|
l.Debug("access:", name)
|
||||||
if strings.HasPrefix(name, "../") {
|
if strings.HasPrefix(name, "../") {
|
||||||
return nil, errors.New("not permitted")
|
return nil, errors.New("not permitted")
|
||||||
}
|
}
|
||||||
|
relatedPath := strings.TrimPrefix(name, f.WPath)
|
||||||
|
|
||||||
rPath := filepath.Join(f.Root, strings.TrimPrefix(name, "/"))
|
rPath := filepath.Join(f.Root, strings.TrimPrefix(relatedPath, "/"))
|
||||||
l.Debug("access:", rPath)
|
l.Debug("access:", rPath)
|
||||||
// if rPath == f.Root {
|
// if rPath == f.Root {
|
||||||
// if f.Default == "" {
|
// if f.Default == "" {
|
||||||
|
|
|
@ -241,7 +241,13 @@ func (s *ServerMux) AddDirective(directiveStr string) {
|
||||||
l.Error(fmt.Sprintf("directive not found: %s", directiveName))
|
l.Error(fmt.Sprintf("directive not found: %s", directiveName))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func (s *ServerMux) getHandler(path string) http.Handler {
|
||||||
|
h, ok := s.handlers[path]
|
||||||
|
if ok {
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
func (s *ServerMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (s *ServerMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
l := logger.GetLogger("Access")
|
l := logger.GetLogger("Access")
|
||||||
data := map[string]interface{}{}
|
data := map[string]interface{}{}
|
||||||
|
@ -252,7 +258,12 @@ func (s *ServerMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
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))
|
l.Info(fmt.Sprintf("match path: %s", p))
|
||||||
s.handlers[p].ServeHTTP(w, newRequest)
|
fhandler := s.getHandler(p)
|
||||||
|
if fhandler != nil {
|
||||||
|
fhandler.ServeHTTP(w, newRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// s.handlers[p].ServeHTTP(w, newRequest)
|
||||||
// s.directiveHandlers.ServeHTTP(w, newRequest)
|
// s.directiveHandlers.ServeHTTP(w, newRequest)
|
||||||
// ctx := newRequest.Context()
|
// ctx := newRequest.Context()
|
||||||
// m := ctx.Value(RequestCtxKey("data")).(map[string]interface{})
|
// m := ctx.Value(RequestCtxKey("data")).(map[string]interface{})
|
||||||
|
@ -268,7 +279,7 @@ func (s *ServerMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
// s.handlers[p].ServeHTTP(w, newRequest)
|
// s.handlers[p].ServeHTTP(w, newRequest)
|
||||||
// }
|
// }
|
||||||
|
|
||||||
return
|
// return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
l.Error(fmt.Sprintf("404: %s", r.URL.Path))
|
l.Error(fmt.Sprintf("404: %s", r.URL.Path))
|
||||||
|
@ -309,6 +320,7 @@ func NewServeMux(c *model.HttpServerConfig) *ServerMux {
|
||||||
if httpPath.Root != "" {
|
if httpPath.Root != "" {
|
||||||
// 创建一个新的文件处理程序
|
// 创建一个新的文件处理程序
|
||||||
fileHandler := handler.NewFileHandler(handler.FileHandler{
|
fileHandler := handler.NewFileHandler(handler.FileHandler{
|
||||||
|
WPath: httpPath.Path,
|
||||||
// 设置文件处理程序的根目录
|
// 设置文件处理程序的根目录
|
||||||
Root: httpPath.Root,
|
Root: httpPath.Root,
|
||||||
// 设置文件处理程序的默认文件
|
// 设置文件处理程序的默认文件
|
||||||
|
|
Loading…
Reference in New Issue