diff --git a/api/frontend/handlers.go b/api/frontend/handlers.go index f83d193..b088f62 100644 --- a/api/frontend/handlers.go +++ b/api/frontend/handlers.go @@ -2,12 +2,19 @@ package frontend import ( "context" + "embed" + "github.com/LiteyukiStudio/go-logger/log" "github.com/cloudwego/hertz/pkg/app" - "os" + "github.com/gabriel-vasile/mimetype" "server-status-be/dao" "strings" ) +//go:embed web/* +var webFiles embed.FS + +var fs2 app.FS + func OnGetServerStatus(ctx context.Context, c *app.RequestContext) { ret := make(map[string]interface{}) for k, v := range dao.GetAll() { @@ -21,25 +28,38 @@ func OnGetServerStatus(ctx context.Context, c *app.RequestContext) { } func OnGetStaticFile(ctx context.Context, c *app.RequestContext) { - c.Request.URI().Path() file := c.Param("file") - fp := "./web/" + file + var fp string if file == "" { fp = "web/index.html" - } - // 判断文件是否存在 - if _, err := os.Stat(fp); err != nil { - fp += ".html" - if _, err := os.Stat(fp); err != nil { - c.JSON(404, "File Not Found") - return - } else { - c.File(fp) - return - } } else { - c.File(fp) + fp = "web/" + file } - ctx.Done() + + data, err := webFiles.ReadFile(fp) + if err != nil { + log.Error("Read file error: ", err) + } + contentType := mimetype.Detect(data).String() + if strings.HasSuffix(fp, ".js") { + contentType = "application/javascript" + } else if strings.HasSuffix(fp, ".css") { + contentType = "text/css" + } else if strings.HasSuffix(fp, ".html") { + contentType = "text/html" + } else if strings.HasSuffix(fp, ".json") { + contentType = "application/json" + } else if strings.HasSuffix(fp, ".png") { + contentType = "image/png" + } else if strings.HasSuffix(fp, ".jpg") { + contentType = "image/jpeg" + } else if strings.HasSuffix(fp, ".ico") { + contentType = "image/x-icon" + } else if strings.HasSuffix(fp, ".svg") { + contentType = "image/svg+xml" + } + + log.Info("Get file: ", fp, " with content type: ", contentType) + c.Data(200, contentType, data) } diff --git a/web/apiroot b/api/frontend/web/apiroot similarity index 100% rename from web/apiroot rename to api/frontend/web/apiroot diff --git a/web/assets/index-B8yPNO6K.js b/api/frontend/web/assets/index-B8yPNO6K.js similarity index 100% rename from web/assets/index-B8yPNO6K.js rename to api/frontend/web/assets/index-B8yPNO6K.js diff --git a/web/assets/index-B8yPNO6K.js.hertz.gz b/api/frontend/web/assets/index-B8yPNO6K.js.hertz.gz similarity index 100% rename from web/assets/index-B8yPNO6K.js.hertz.gz rename to api/frontend/web/assets/index-B8yPNO6K.js.hertz.gz diff --git a/web/assets/index-D-XXFb5n.css b/api/frontend/web/assets/index-D-XXFb5n.css similarity index 100% rename from web/assets/index-D-XXFb5n.css rename to api/frontend/web/assets/index-D-XXFb5n.css diff --git a/web/assets/index-D-XXFb5n.css.hertz.gz b/api/frontend/web/assets/index-D-XXFb5n.css.hertz.gz similarity index 100% rename from web/assets/index-D-XXFb5n.css.hertz.gz rename to api/frontend/web/assets/index-D-XXFb5n.css.hertz.gz diff --git a/web/icon.svg b/api/frontend/web/icon.svg similarity index 100% rename from web/icon.svg rename to api/frontend/web/icon.svg diff --git a/web/icon.svg.hertz.gz b/api/frontend/web/icon.svg.hertz.gz similarity index 100% rename from web/icon.svg.hertz.gz rename to api/frontend/web/icon.svg.hertz.gz diff --git a/web/index.html b/api/frontend/web/index.html similarity index 100% rename from web/index.html rename to api/frontend/web/index.html diff --git a/web/index.html.hertz.gz b/api/frontend/web/index.html.hertz.gz similarity index 100% rename from web/index.html.hertz.gz rename to api/frontend/web/index.html.hertz.gz diff --git a/web/svg/location.svg b/api/frontend/web/svg/location.svg similarity index 100% rename from web/svg/location.svg rename to api/frontend/web/svg/location.svg diff --git a/web/svg/location.svg.hertz.gz b/api/frontend/web/svg/location.svg.hertz.gz similarity index 100% rename from web/svg/location.svg.hertz.gz rename to api/frontend/web/svg/location.svg.hertz.gz diff --git a/web/svg/system.svg b/api/frontend/web/svg/system.svg similarity index 100% rename from web/svg/system.svg rename to api/frontend/web/svg/system.svg diff --git a/web/svg/system.svg.hertz.gz b/api/frontend/web/svg/system.svg.hertz.gz similarity index 100% rename from web/svg/system.svg.hertz.gz rename to api/frontend/web/svg/system.svg.hertz.gz diff --git a/api/router.go b/api/router.go index 3bcb69b..0bc6f14 100644 --- a/api/router.go +++ b/api/router.go @@ -16,7 +16,7 @@ import ( var h *server.Hertz func init() { - h = hertz.NewHertz([]config.Option{server.WithHostPorts(":8088")}, []app.HandlerFunc{}) + h = hertz.NewHertz([]config.Option{server.WithHostPorts(":8090")}, []app.HandlerFunc{}) // cv api 状态客户端接口 h.Use(cors.Default()) @@ -32,11 +32,11 @@ func init() { { api.GET("/", func(ctx context.Context, c *app.RequestContext) { c.JSON(200, "Hello, api") }) api.GET("/status", frontend.OnGetServerStatus) - } // 静态文件 h.GET("/*file", frontend.OnGetStaticFile) + //h.StaticFS("/", fs) } func Run() { diff --git a/go.mod b/go.mod index 197f873..6d88ce3 100644 --- a/go.mod +++ b/go.mod @@ -21,6 +21,7 @@ require ( github.com/cloudwego/iasm v0.2.0 // indirect github.com/cloudwego/netpoll v0.6.4 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/gabriel-vasile/mimetype v1.4.5 // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/henrylee2cn/ameda v1.5.1 // indirect github.com/henrylee2cn/goutil v1.0.1 // indirect @@ -32,6 +33,7 @@ require ( github.com/tidwall/pretty v1.2.1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect golang.org/x/arch v0.10.0 // indirect + golang.org/x/net v0.27.0 // indirect golang.org/x/sys v0.25.0 // indirect golang.org/x/text v0.18.0 // indirect google.golang.org/protobuf v1.34.2 // indirect diff --git a/go.sum b/go.sum index 5c0e402..083b950 100644 --- a/go.sum +++ b/go.sum @@ -52,6 +52,8 @@ github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwV github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= +github.com/gabriel-vasile/mimetype v1.4.5 h1:J7wGKdGu33ocBOhGy0z653k/lFKLFDPJMG8Gql0kxn4= +github.com/gabriel-vasile/mimetype v1.4.5/go.mod h1:ibHel+/kbxn9x2407k1izTA1S81ku1z/DlgOW2QE0M4= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= @@ -128,6 +130,8 @@ golang.org/x/arch v0.10.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20221014081412-f15817d10f9b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= +golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= +golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/main.go b/main.go index 1911b3c..4fdde98 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,8 @@ package main -import "server-status-be/api" +import ( + "server-status-be/api" +) func main() { api.Run()