fix file handler and update logger

This commit is contained in:
kingecg 2023-12-10 01:08:39 +08:00
parent dc6069c8a1
commit 49ccc86721
9 changed files with 100 additions and 15 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"editor.fontSize": 20
}

View File

@ -14,5 +14,24 @@
}, },
"admin" : { "admin" : {
"port" : 8088 "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": "/"
} }
}
]
}]
} }

2
go.mod
View File

@ -3,7 +3,7 @@ module git.pyer.club/kingecg/gohttpd
go 1.19 go 1.19
require ( 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/samber/lo v1.39.0 // indirect
github.com/soheilhy/cmux v0.1.5 // indirect github.com/soheilhy/cmux v0.1.5 // indirect
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect

2
go.sum
View File

@ -1,5 +1,7 @@
git.pyer.club/kingecg/gologger v1.0.0 h1:H3oFIJ1p7mlAgJgJ/wiM+hxn34x7IxY4YiafY8iMfAk= 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.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 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA=
github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/soheilhy/cmux v0.1.5 h1:jjzc5WVemNEDTLwv9tlmemhC73tI08BNOIGwBOo10Js= github.com/soheilhy/cmux v0.1.5 h1:jjzc5WVemNEDTLwv9tlmemhC73tI08BNOIGwBOo10Js=

View File

@ -2,6 +2,7 @@ package handler
import ( import (
"errors" "errors"
"fmt"
"io/fs" "io/fs"
"net/http" "net/http"
"os" "os"
@ -21,26 +22,32 @@ func (f FileHandler) Open(name string) (http.File, error) {
} }
rPath := filepath.Join(f.Root, strings.TrimPrefix(name, "/")) rPath := filepath.Join(f.Root, strings.TrimPrefix(name, "/"))
if rPath == f.Root { // if rPath == f.Root {
if f.Default == "" { // if f.Default == "" {
return nil, errors.New("not permit list dir") // return nil, errors.New("not permit list dir")
} // }
rPath = filepath.Join(rPath, f.Default) // rPath = filepath.Join(rPath, f.Default)
} // }
fInfo, _, err := FileExists(rPath) fInfo, _, err := FileExists(rPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if fInfo.IsDir() { if fInfo.IsDir() && rPath != strings.TrimSuffix(f.Root, "/") {
return nil, errors.New("not permit list dir") return nil, errors.New("not permit list dir")
} }
if fInfo.Mode() == fs.ModeSymlink { if fInfo.Mode() == fs.ModeSymlink {
return nil, errors.New("not permit follow symbol link") 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) { func FileExists(name string) (os.FileInfo, bool, error) {

46
public/index.html Normal file
View File

@ -0,0 +1,46 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WenSocket</title>
<style>
div{
width: 200px;
height: 200px;
border:1px solid;
margin-top: 5px;
}
</style>
</head>
<body>
<input type="text" placeholder="请输入内容">
<button>提交</button>
<div></div>
<script>
const input = document.querySelector('input')
const button = document.querySelector('button')
const div = document.querySelector('div')
//创建WebSocket实例
const socket = new WebSocket("ws://localhost:8080/ws")
//监听服务是否链接
socket.addEventListener('open',()=>{
div.innerHTML = "服务链接成功"
})
//button触发点击事件将input框中的内容发送至websocket
//查看websocket是否接收到数据chrome F12打开控制台》Network》WS》echo.websocket.org》messages
button.addEventListener('click',()=>{
const value = input.value
socket.send(value)
})
// 将接收到的数据插入到div中
socket.addEventListener('message',(e)=>{
console.log(e)
div.innerHTML = e.data
})
</script>
</body>
</html>

View File

@ -16,8 +16,11 @@ import (
var ServerManager map[string]*ServerListener = make(map[string]*ServerListener) 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 { return func(r io.Reader) bool {
if s.ServerCount() == 1 {
return true
}
req, err := http.ReadRequest(bufio.NewReader(r)) req, err := http.ReadRequest(bufio.NewReader(r))
if err != nil { if err != nil {
return false return false
@ -35,13 +38,17 @@ func (s *ServerListener) AddServer(name string, server *http.Server) {
s.servers[name] = server s.servers[name] = server
} }
func (s *ServerListener) ServerCount() int {
return len(s.servers)
}
func (s *ServerListener) StartServer(name string) error { func (s *ServerListener) StartServer(name string) error {
server, ok := s.servers[name] server, ok := s.servers[name]
if !ok { if !ok {
panic("No named server") panic("No named server")
} }
l := s.listener.Match(makeMatcher(name)) l := s.listener.Match(makeMatcher(name, s))
return server.Serve(l) return server.Serve(l)
} }
@ -62,6 +69,7 @@ func (s *ServerListener) ShutDown() {
for _, server := range s.servers { for _, server := range s.servers {
server.Shutdown(context.Background()) server.Shutdown(context.Background())
} }
s.listener.Close()
} }
func (s *ServerListener) Serve() { func (s *ServerListener) Serve() {

View File

@ -4,12 +4,12 @@ import (
"fmt" "fmt"
) )
const logTemplate = "[%s] %s - %s\n" const logTemplate = "[%s] %s : %s - %s\n"
func format(logEvent LogEvent) string { func format(logEvent LogEvent) string {
data := logEvent.Ts.Format("2006-01-02 15:04:05") data := logEvent.Ts.Format("2006-01-02 15:04:05")
msg := fmt.Sprint(logEvent.Data...) 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 return ret
} }

2
vendor/modules.txt vendored
View File

@ -1,4 +1,4 @@
# git.pyer.club/kingecg/gologger v1.0.0 # git.pyer.club/kingecg/gologger v1.0.1
## explicit; go 1.19 ## explicit; go 1.19
git.pyer.club/kingecg/gologger git.pyer.club/kingecg/gologger
# github.com/samber/lo v1.39.0 # github.com/samber/lo v1.39.0