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 @@ + + +
+ + +