🐛 添加前端页面

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