diff --git a/config.json b/config.json index 513961b..d2f5626 100644 --- a/config.json +++ b/config.json @@ -40,40 +40,18 @@ ] }, "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, "name": "cloudops", - "directives": [ - "Gzip_Response" - ], + "paths": [ + + { + "path": "/static", + "root": "/home/kingecg/work/angular13/cloudops/static", + "default": "index.html" + }, { "path": "/", "upstreams": [ diff --git a/handler/file.go b/handler/file.go index 2df563a..7ed6711 100644 --- a/handler/file.go +++ b/handler/file.go @@ -2,6 +2,7 @@ package handler import ( "errors" + "fmt" "io/fs" "mime" "net/http" @@ -13,19 +14,24 @@ import ( ) type FileHandler struct { + WPath string Root string Default string 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) { l := gologger.GetLogger("filehandler") - + l.Debug("access:", name) if strings.HasPrefix(name, "../") { 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) // if rPath == f.Root { // if f.Default == "" { diff --git a/server/server.go b/server/server.go index c390543..4aa5fa3 100644 --- a/server/server.go +++ b/server/server.go @@ -241,7 +241,13 @@ func (s *ServerMux) AddDirective(directiveStr string) { 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) { l := logger.GetLogger("Access") data := map[string]interface{}{} @@ -252,7 +258,12 @@ func (s *ServerMux) ServeHTTP(w http.ResponseWriter, r *http.Request) { for _, p := range s.paths { if strings.HasPrefix(r.URL.Path, 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) // ctx := newRequest.Context() // 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) // } - return + // return } } l.Error(fmt.Sprintf("404: %s", r.URL.Path)) @@ -309,6 +320,7 @@ func NewServeMux(c *model.HttpServerConfig) *ServerMux { if httpPath.Root != "" { // 创建一个新的文件处理程序 fileHandler := handler.NewFileHandler(handler.FileHandler{ + WPath: httpPath.Path, // 设置文件处理程序的根目录 Root: httpPath.Root, // 设置文件处理程序的默认文件