🐛 添加前端页面

This commit is contained in:
远野千束 2024-10-02 13:46:10 +08:00
parent 7fb9feee5b
commit 33618b9d00
18 changed files with 47 additions and 19 deletions

View File

@ -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)
}

View File

Before

Width:  |  Height:  |  Size: 710 B

After

Width:  |  Height:  |  Size: 710 B

View File

Before

Width:  |  Height:  |  Size: 470 B

After

Width:  |  Height:  |  Size: 470 B

View File

Before

Width:  |  Height:  |  Size: 576 B

After

Width:  |  Height:  |  Size: 576 B

View File

@ -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() {

2
go.mod
View File

@ -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

4
go.sum
View File

@ -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=

View File

@ -1,6 +1,8 @@
package main
import "server-status-be/api"
import (
"server-status-be/api"
)
func main() {
api.Run()