From 49ccc867217be0cc42e98adcece8272b66da3b57 Mon Sep 17 00:00:00 2001 From: kingecg Date: Sun, 10 Dec 2023 01:08:39 +0800 Subject: [PATCH] fix file handler and update logger --- .vscode/settings.json | 3 ++ config.json | 21 ++++++++- go.mod | 2 +- go.sum | 2 + hander/file.go | 23 ++++++---- public/index.html | 46 +++++++++++++++++++ server/manager.go | 12 ++++- .../git.pyer.club/kingecg/gologger/format.go | 4 +- vendor/modules.txt | 2 +- 9 files changed, 100 insertions(+), 15 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 public/index.html diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..3acaebb --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "editor.fontSize": 20 +} \ No newline at end of file diff --git a/config.json b/config.json index b1fd2f6..1045150 100644 --- a/config.json +++ b/config.json @@ -14,5 +14,24 @@ }, "admin" : { "port" : 8088 - } + }, + "servers":[{ + "port" : 8080, + "servername":"test", + "paths":[ + { + "path": "/", + "root": "/home/kingecg/code/gohttp/public/", + "default": "index.html" + }, + { + "path": "/ws", + "upstreams":["http://localhost:3000"], + "pathrewrite": { + "replace": "/ws", + "with": "/" + } + } + ] + }] } \ No newline at end of file diff --git a/go.mod b/go.mod index 09fb4b5..756f75d 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module git.pyer.club/kingecg/gohttpd go 1.19 require ( - git.pyer.club/kingecg/gologger v1.0.0 // indirect + git.pyer.club/kingecg/gologger v1.0.1 // indirect github.com/samber/lo v1.39.0 // indirect github.com/soheilhy/cmux v0.1.5 // indirect golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect diff --git a/go.sum b/go.sum index e2f7bef..21c0b7c 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ git.pyer.club/kingecg/gologger v1.0.0 h1:H3oFIJ1p7mlAgJgJ/wiM+hxn34x7IxY4YiafY8iMfAk= git.pyer.club/kingecg/gologger v1.0.0/go.mod h1:SNSl2jRHPzIpHSzdKOoVG798rtYMjPDPFyxUrEgivkY= +git.pyer.club/kingecg/gologger v1.0.1 h1:snCb0ePlfDUglX+CHwNzq5MRK5uNTnPUks1Dnapl/p8= +git.pyer.club/kingecg/gologger v1.0.1/go.mod h1:SNSl2jRHPzIpHSzdKOoVG798rtYMjPDPFyxUrEgivkY= github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA= github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= github.com/soheilhy/cmux v0.1.5 h1:jjzc5WVemNEDTLwv9tlmemhC73tI08BNOIGwBOo10Js= diff --git a/hander/file.go b/hander/file.go index 92ebcca..f6e6617 100644 --- a/hander/file.go +++ b/hander/file.go @@ -2,6 +2,7 @@ package handler import ( "errors" + "fmt" "io/fs" "net/http" "os" @@ -21,26 +22,32 @@ func (f FileHandler) Open(name string) (http.File, error) { } rPath := filepath.Join(f.Root, strings.TrimPrefix(name, "/")) - if rPath == f.Root { - if f.Default == "" { - return nil, errors.New("not permit list dir") - } - rPath = filepath.Join(rPath, f.Default) - } + // if rPath == f.Root { + // if f.Default == "" { + // return nil, errors.New("not permit list dir") + // } + // rPath = filepath.Join(rPath, f.Default) + // } fInfo, _, err := FileExists(rPath) if err != nil { return nil, err } - if fInfo.IsDir() { + if fInfo.IsDir() && rPath != strings.TrimSuffix(f.Root, "/") { return nil, errors.New("not permit list dir") } if fInfo.Mode() == fs.ModeSymlink { return nil, errors.New("not permit follow symbol link") } - return os.Open(rPath) + + fp, err := os.Open(rPath) + if err != nil { + fmt.Println("error") + return nil, err + } + return fp, nil } func FileExists(name string) (os.FileInfo, bool, error) { diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..6dd69ab --- /dev/null +++ b/public/index.html @@ -0,0 +1,46 @@ + + + + + + WenSocket + + + + + +
+ + + + \ No newline at end of file diff --git a/server/manager.go b/server/manager.go index ec89ae4..7df3f19 100644 --- a/server/manager.go +++ b/server/manager.go @@ -16,8 +16,11 @@ import ( var ServerManager map[string]*ServerListener = make(map[string]*ServerListener) -func makeMatcher(name string) cmux.Matcher { +func makeMatcher(name string, s *ServerListener) cmux.Matcher { return func(r io.Reader) bool { + if s.ServerCount() == 1 { + return true + } req, err := http.ReadRequest(bufio.NewReader(r)) if err != nil { return false @@ -35,13 +38,17 @@ func (s *ServerListener) AddServer(name string, server *http.Server) { s.servers[name] = server } +func (s *ServerListener) ServerCount() int { + return len(s.servers) +} + func (s *ServerListener) StartServer(name string) error { server, ok := s.servers[name] if !ok { panic("No named server") } - l := s.listener.Match(makeMatcher(name)) + l := s.listener.Match(makeMatcher(name, s)) return server.Serve(l) } @@ -62,6 +69,7 @@ func (s *ServerListener) ShutDown() { for _, server := range s.servers { server.Shutdown(context.Background()) } + s.listener.Close() } func (s *ServerListener) Serve() { diff --git a/vendor/git.pyer.club/kingecg/gologger/format.go b/vendor/git.pyer.club/kingecg/gologger/format.go index 48b3fa1..6f1cde3 100644 --- a/vendor/git.pyer.club/kingecg/gologger/format.go +++ b/vendor/git.pyer.club/kingecg/gologger/format.go @@ -4,12 +4,12 @@ import ( "fmt" ) -const logTemplate = "[%s] %s - %s\n" +const logTemplate = "[%s] %s : %s - %s\n" func format(logEvent LogEvent) string { data := logEvent.Ts.Format("2006-01-02 15:04:05") msg := fmt.Sprint(logEvent.Data...) - ret := fmt.Sprintf(logTemplate, data, getLogLevelStr(logEvent.Level), msg) + ret := fmt.Sprintf(logTemplate, data, logEvent.Category, getLogLevelStr(logEvent.Level), msg) return ret } diff --git a/vendor/modules.txt b/vendor/modules.txt index 97eb270..9d16269 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,4 +1,4 @@ -# git.pyer.club/kingecg/gologger v1.0.0 +# git.pyer.club/kingecg/gologger v1.0.1 ## explicit; go 1.19 git.pyer.club/kingecg/gologger # github.com/samber/lo v1.39.0