first comm

This commit is contained in:
2024-10-02 08:29:09 +08:00
parent d8b0c2f766
commit 2e57b24335
10 changed files with 295 additions and 66 deletions

44
api/frontend/handlers.go Normal file
View File

@ -0,0 +1,44 @@
package frontend
import (
"context"
"github.com/cloudwego/hertz/pkg/app"
"os"
"server-status-be/dao"
"strings"
)
func OnGetServerStatus(ctx context.Context, c *app.RequestContext) {
ret := make(map[string]interface{})
for k, v := range dao.GetAll() {
if strings.HasPrefix(k, "report.") {
reportName := strings.TrimPrefix(k, "report.")
ret[reportName] = v
}
}
c.JSON(200, ret)
ctx.Done()
}
func OnGetStaticFile(ctx context.Context, c *app.RequestContext) {
file := c.Param("file")
fp := "./web/" + file
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)
}
ctx.Done()
}