fix normalize bug
This commit is contained in:
parent
f437bb300c
commit
8d943ce89a
|
@ -0,0 +1,25 @@
|
|||
{
|
||||
// 使用 IntelliSense 了解相关属性。
|
||||
// 悬停以查看现有属性的描述。
|
||||
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Launch",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
"program": "${workspaceFolder}",
|
||||
"env": {
|
||||
"GODEBUG": "cgocheck=0",
|
||||
"_go_daemon": "g_dtasj"
|
||||
},
|
||||
"args": [
|
||||
"run",
|
||||
"${file}"
|
||||
],
|
||||
"showLog": true,
|
||||
"trace": "log",
|
||||
}
|
||||
]
|
||||
}
|
1
Makefile
1
Makefile
|
@ -5,6 +5,7 @@ all:build
|
|||
build:
|
||||
go build -o ${BINARY_NAME}
|
||||
cp config.json target/
|
||||
cp -r adminui target/
|
||||
|
||||
clean:
|
||||
go clean
|
||||
|
|
|
@ -20,7 +20,7 @@ func setConfig(w http.ResponseWriter, r *http.Request) {
|
|||
var AdminServerMux *server.RestMux
|
||||
|
||||
func init() {
|
||||
AdminServerMux = server.NewRestMux("/")
|
||||
AdminServerMux = server.NewRestMux("/api")
|
||||
AdminServerMux.Use(server.BasicAuth)
|
||||
AdminServerMux.HandleFunc("GET", "/about", http.HandlerFunc(about))
|
||||
postConfigRoute := AdminServerMux.HandleFunc("POST", "/config", http.HandlerFunc(setConfig))
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
<html>
|
||||
<body>
|
||||
<h1>GoHttpd Running...</h1>
|
||||
</body>
|
||||
</html>
|
|
@ -19,7 +19,14 @@
|
|||
"name": "admin",
|
||||
"port" : 8088,
|
||||
"username": "admin",
|
||||
"password": "admin"
|
||||
"password": "admin",
|
||||
"paths": [
|
||||
{
|
||||
"path": "/",
|
||||
"root": "./adminui",
|
||||
"default": "index.html"
|
||||
}
|
||||
]
|
||||
},
|
||||
"servers":[{
|
||||
"port" : 8080,
|
||||
|
|
|
@ -21,8 +21,9 @@ func (g *GoHttp) Start() {
|
|||
g.logger = gologger.GetLogger("Server")
|
||||
g.logger.Info("start gohttpd")
|
||||
// if g.conf != nil {
|
||||
|
||||
g.makeServer(conf.Admin, admin.AdminServerMux)
|
||||
adminHandler := g.assembleServerMux(conf.Admin.Paths)
|
||||
adminHandler.(*http.ServeMux).Handle("/api/", admin.AdminServerMux)
|
||||
g.makeServer(conf.Admin, adminHandler)
|
||||
|
||||
for _, server := range conf.Servers {
|
||||
sHandler := g.assembleServerMux(server.Paths)
|
||||
|
@ -96,9 +97,9 @@ func LoadConfig(configPath string) {
|
|||
}
|
||||
|
||||
func normalizeServer(server *model.HttpServerConfig) {
|
||||
for _, path := range server.Paths {
|
||||
for index, path := range server.Paths {
|
||||
if path.Root != "" {
|
||||
path.Root = NormalizePath(path.Root)
|
||||
server.Paths[index].Root = NormalizePath(path.Root)
|
||||
}
|
||||
}
|
||||
if server.CertFile != "" {
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
[2023-12-13 10:56:53] Server : info - Load config success
|
||||
[2023-12-13 10:56:53] Server : info - start gohttpd
|
||||
[2023-12-13 10:56:53] Listener : debug - listen on :8088
|
||||
[2023-12-13 10:56:53] Listener : debug - listen on :8080
|
||||
[2023-12-13 10:56:53] Server : info - gohttpd start success
|
||||
[2023-12-13 10:57:01] filehandler : debug - access:/Users/chengguang/code/gohttp/adminui
|
||||
[2023-12-13 10:57:01] filehandler : debug - access:/Users/chengguang/code/gohttp/adminui/index.html
|
|
@ -2,12 +2,13 @@ package handler
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"git.pyer.club/kingecg/gologger"
|
||||
)
|
||||
|
||||
type FileHandler struct {
|
||||
|
@ -17,11 +18,14 @@ type FileHandler struct {
|
|||
}
|
||||
|
||||
func (f FileHandler) Open(name string) (http.File, error) {
|
||||
l := gologger.GetLogger("filehandler")
|
||||
|
||||
if strings.HasPrefix(name, "../") {
|
||||
return nil, errors.New("not permitted")
|
||||
}
|
||||
|
||||
rPath := filepath.Join(f.Root, strings.TrimPrefix(name, "/"))
|
||||
l.Debug("access:", rPath)
|
||||
// if rPath == f.Root {
|
||||
// if f.Default == "" {
|
||||
// return nil, errors.New("not permit list dir")
|
||||
|
@ -31,6 +35,7 @@ func (f FileHandler) Open(name string) (http.File, error) {
|
|||
|
||||
fInfo, _, err := FileExists(rPath)
|
||||
if err != nil {
|
||||
l.Error("access file error:", rPath, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -44,7 +49,7 @@ func (f FileHandler) Open(name string) (http.File, error) {
|
|||
|
||||
fp, err := os.Open(rPath)
|
||||
if err != nil {
|
||||
fmt.Println("error")
|
||||
l.Error("open file fail", err)
|
||||
return nil, err
|
||||
}
|
||||
return fp, nil
|
||||
|
|
7
main.go
7
main.go
|
@ -6,10 +6,17 @@ import (
|
|||
"syscall"
|
||||
|
||||
"git.pyer.club/kingecg/godaemon"
|
||||
"git.pyer.club/kingecg/gologger"
|
||||
)
|
||||
|
||||
func main() {
|
||||
LoadConfig("")
|
||||
l := gologger.GetLogger("main")
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
l.Error("panic", err)
|
||||
}
|
||||
}()
|
||||
daemon := godaemon.NewGoDaemon(start, stop)
|
||||
daemon.Start()
|
||||
}
|
||||
|
|
|
@ -1,43 +0,0 @@
|
|||
{
|
||||
"logging" :{
|
||||
"appenders": {
|
||||
"out" :{
|
||||
"type": "file",
|
||||
"options":{
|
||||
"file": "gohttpd.log"
|
||||
}
|
||||
}
|
||||
},
|
||||
"categories": {
|
||||
"default": {
|
||||
"appenders": [ "out" ],
|
||||
"level": "debug"
|
||||
}
|
||||
}
|
||||
},
|
||||
"admin" : {
|
||||
"name": "admin",
|
||||
"port" : 8088,
|
||||
"username": "admin",
|
||||
"password": "admin"
|
||||
},
|
||||
"servers":[{
|
||||
"port" : 8080,
|
||||
"name":"test",
|
||||
"paths":[
|
||||
{
|
||||
"path": "/",
|
||||
"root": "/home/kingecg/code/gohttp/public/",
|
||||
"default": "index.html"
|
||||
},
|
||||
{
|
||||
"path": "/ws",
|
||||
"upstreams":["http://localhost:3000"],
|
||||
"pathrewrite": {
|
||||
"replace": "/ws",
|
||||
"with": "/"
|
||||
}
|
||||
}
|
||||
]
|
||||
}]
|
||||
}
|
BIN
target/gohttpd
BIN
target/gohttpd
Binary file not shown.
|
@ -1,12 +0,0 @@
|
|||
[2023-12-12 15:27:25] Server : info - Load config success
|
||||
[2023-12-12 15:27:25] Server : info - Load config success
|
||||
[2023-12-12 15:27:25] daemon : debug - Starting new task
|
||||
[2023-12-12 15:27:25] Server : info - Load config success
|
||||
[2023-12-12 15:27:25] Server : info - start gohttpd
|
||||
[2023-12-12 15:27:25] Listener : debug - listen on :8088
|
||||
[2023-12-12 15:27:25] Listener : debug - listen on :8080
|
||||
[2023-12-12 15:27:25] Server : info - gohttpd start success
|
||||
[2023-12-12 15:27:37] Route : debug - match route: GET /about
|
||||
[2023-12-12 18:31:09] Server : info - Exit
|
||||
[2023-12-12 18:31:09] daemon : debug - Stop it
|
||||
[2023-12-12 18:31:09] daemon : debug - daemon is stopped, exit now
|
|
@ -1 +0,0 @@
|
|||
39527
|
|
@ -0,0 +1,53 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNormalizePath(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
path string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "Test case 1",
|
||||
path: "/path/to/file",
|
||||
want: "/path/to/file",
|
||||
},
|
||||
{
|
||||
name: "Test case 2",
|
||||
path: "path/to/file",
|
||||
want: GetExecDir() + "/path/to/file",
|
||||
},
|
||||
{
|
||||
name: "Test case 3",
|
||||
path: "../path/to/file",
|
||||
want: filepath.Join(GetExecDir(), "../path/to/file"),
|
||||
},
|
||||
{
|
||||
name: "Test case 4",
|
||||
path: "./path/to/file",
|
||||
want: GetExecDir() + "/path/to/file",
|
||||
},
|
||||
{
|
||||
name: "Test case 5",
|
||||
path: "/absolute/path/to/file",
|
||||
want: "/absolute/path/to/file",
|
||||
},
|
||||
{
|
||||
name: "Test case 6",
|
||||
path: "relative/path/to/file",
|
||||
want: GetExecDir() + "/relative/path/to/file",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := NormalizePath(tt.path); got != tt.want {
|
||||
t.Errorf("NormalizePath() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue