fix file handler and update logger
This commit is contained in:
parent
dc6069c8a1
commit
49ccc86721
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"editor.fontSize": 20
|
||||
}
|
19
config.json
19
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": "/"
|
||||
}
|
||||
}
|
||||
]
|
||||
}]
|
||||
}
|
2
go.mod
2
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
|
||||
|
|
2
go.sum
2
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=
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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>
|
|
@ -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() {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue