diff --git a/.vscode/launch.json b/.vscode/launch.json
new file mode 100644
index 0000000..55c594c
--- /dev/null
+++ b/.vscode/launch.json
@@ -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",
+ }
+ ]
+}
\ No newline at end of file
diff --git a/Makefile b/Makefile
index db76383..b3009e0 100644
--- a/Makefile
+++ b/Makefile
@@ -5,6 +5,7 @@ all:build
build:
go build -o ${BINARY_NAME}
cp config.json target/
+ cp -r adminui target/
clean:
go clean
diff --git a/admin/admin.go b/admin/admin.go
index f928620..a76dc0b 100644
--- a/admin/admin.go
+++ b/admin/admin.go
@@ -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))
diff --git a/adminui/index.html b/adminui/index.html
new file mode 100644
index 0000000..30bd6d0
--- /dev/null
+++ b/adminui/index.html
@@ -0,0 +1,5 @@
+
+
+ GoHttpd Running...
+
+
\ No newline at end of file
diff --git a/config.json b/config.json
index e4aaa5d..8d16d4b 100644
--- a/config.json
+++ b/config.json
@@ -19,7 +19,14 @@
"name": "admin",
"port" : 8088,
"username": "admin",
- "password": "admin"
+ "password": "admin",
+ "paths": [
+ {
+ "path": "/",
+ "root": "./adminui",
+ "default": "index.html"
+ }
+ ]
},
"servers":[{
"port" : 8080,
diff --git a/gohttp.go b/gohttp.go
index 1b10cff..19472e8 100644
--- a/gohttp.go
+++ b/gohttp.go
@@ -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 != "" {
diff --git a/gohttpd.log b/gohttpd.log
new file mode 100644
index 0000000..dc8d015
--- /dev/null
+++ b/gohttpd.log
@@ -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
diff --git a/hander/file.go b/hander/file.go
index f6e6617..a07cd48 100644
--- a/hander/file.go
+++ b/hander/file.go
@@ -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
diff --git a/main.go b/main.go
index 4a7ba10..52b5d7c 100644
--- a/main.go
+++ b/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()
}
diff --git a/target/config.json b/target/config.json
deleted file mode 100644
index e4aaa5d..0000000
--- a/target/config.json
+++ /dev/null
@@ -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": "/"
- }
- }
- ]
- }]
-}
\ No newline at end of file
diff --git a/target/gohttpd b/target/gohttpd
deleted file mode 100755
index adb6cc7..0000000
Binary files a/target/gohttpd and /dev/null differ
diff --git a/target/gohttpd.log b/target/gohttpd.log
deleted file mode 100644
index 3da8881..0000000
--- a/target/gohttpd.log
+++ /dev/null
@@ -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
diff --git a/target/gohttpd.pid b/target/gohttpd.pid
deleted file mode 100644
index c432996..0000000
--- a/target/gohttpd.pid
+++ /dev/null
@@ -1 +0,0 @@
-39527
\ No newline at end of file
diff --git a/util_test.go b/util_test.go
new file mode 100644
index 0000000..d571aa0
--- /dev/null
+++ b/util_test.go
@@ -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)
+ }
+ })
+ }
+}