mirror of
https://github.com/snowykami/server-status-server.git
synced 2025-09-06 12:06:26 +00:00
✨ first comm
This commit is contained in:
60
api/backend/handlers.go
Normal file
60
api/backend/handlers.go
Normal file
@ -0,0 +1,60 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/LiteyukiStudio/go-logger/log"
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"server-status-be/dao"
|
||||
"server-status-be/service"
|
||||
)
|
||||
|
||||
// be api 后端接口
|
||||
func fmtReportKey(id string) string {
|
||||
return "report." + id
|
||||
}
|
||||
|
||||
func BeAuth(ctx context.Context, c *app.RequestContext) {
|
||||
// 获取Authorization Bearer Token
|
||||
token := string(c.GetHeader("Authorization"))
|
||||
if token == "" {
|
||||
c.JSON(401, "No Token")
|
||||
c.Abort()
|
||||
return
|
||||
} else {
|
||||
if service.Token != token {
|
||||
c.JSON(401, "Invalid Token: "+token)
|
||||
c.Abort()
|
||||
return
|
||||
} else {
|
||||
c.Next(ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func OnPostStatus(ctx context.Context, c *app.RequestContext) {
|
||||
report := dao.Report{}
|
||||
//err := c.BindJSON(&report)
|
||||
err := json.Unmarshal(c.GetRawData(), &report)
|
||||
log.Info(report)
|
||||
if err != nil {
|
||||
log.Error("Invalid JSON: ", err)
|
||||
c.JSON(400, "Invalid JSON: "+err.Error())
|
||||
return
|
||||
}
|
||||
dao.Save(fmtReportKey(report.Meta.ID), report)
|
||||
}
|
||||
|
||||
func OnDeleteHost(ctx context.Context, c *app.RequestContext) {
|
||||
id, _ := c.GetPostForm("id")
|
||||
if id == "" {
|
||||
c.JSON(400, "Invalid ID")
|
||||
return
|
||||
}
|
||||
err := dao.Delete(fmtReportKey(id))
|
||||
if err != nil {
|
||||
c.JSON(400, "Delete Error: "+err.Error())
|
||||
return
|
||||
}
|
||||
c.JSON(200, "Delete Success")
|
||||
}
|
44
api/frontend/handlers.go
Normal file
44
api/frontend/handlers.go
Normal 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()
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"server-status-be/dao"
|
||||
)
|
||||
|
||||
// be api 后端接口
|
||||
|
||||
func BeAuth(ctx context.Context, c *app.RequestContext) {
|
||||
// 获取Authorization Bearer Token
|
||||
token := string(c.GetHeader("Authorization"))
|
||||
if token == "" {
|
||||
c.JSON(401, "Unauthorized")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func OnPostReport(ctx context.Context, c *app.RequestContext) {
|
||||
report := dao.Report{}
|
||||
err := c.BindJSON(&report)
|
||||
if err != nil {
|
||||
c.JSON(400, "Invalid JSON")
|
||||
return
|
||||
}
|
||||
}
|
@ -4,37 +4,49 @@ import (
|
||||
"context"
|
||||
"git.liteyuki.icu/backend/golite/hertz"
|
||||
"git.liteyuki.icu/backend/golite/logger"
|
||||
"github.com/LiteyukiStudio/go-logger/log"
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"github.com/cloudwego/hertz/pkg/app/server"
|
||||
"github.com/cloudwego/hertz/pkg/common/config"
|
||||
"github.com/hertz-contrib/cors"
|
||||
"server-status-be/api/backend"
|
||||
"server-status-be/api/frontend"
|
||||
)
|
||||
|
||||
var h *server.Hertz
|
||||
|
||||
func init() {
|
||||
h = hertz.NewHertz([]config.Option{server.WithHostPorts(":8088")}, []app.HandlerFunc{})
|
||||
h.GET("/", func(ctx context.Context, c *app.RequestContext) { c.JSON(200, "Hello, status be") })
|
||||
// cv api 状态客户端接口
|
||||
be := h.Group("/be", BeAuth)
|
||||
h.Use(cors.Default())
|
||||
|
||||
client := h.Group("/client", backend.BeAuth)
|
||||
{
|
||||
be.GET("/", func(ctx context.Context, c *app.RequestContext) { c.JSON(200, "Hello, be") })
|
||||
be.POST("/report", func(ctx context.Context, c *app.RequestContext) {})
|
||||
client.GET("/ping", func(ctx context.Context, c *app.RequestContext) { c.JSON(200, "Hello, cv") })
|
||||
client.POST("/status", backend.OnPostStatus)
|
||||
client.DELETE("/host", backend.OnDeleteHost)
|
||||
}
|
||||
|
||||
// fe api 前端接口
|
||||
fe := h.Group("/fe")
|
||||
// api api 前端接口
|
||||
api := h.Group("/api")
|
||||
{
|
||||
fe.GET("/", func(ctx context.Context, c *app.RequestContext) { c.JSON(200, "Hello, fe") })
|
||||
api.GET("/", func(ctx context.Context, c *app.RequestContext) { c.JSON(200, "Hello, api") })
|
||||
api.GET("/status", frontend.OnGetServerStatus)
|
||||
|
||||
}
|
||||
|
||||
// 静态文件
|
||||
h.GET("/*file", frontend.OnGetStaticFile)
|
||||
}
|
||||
|
||||
func Run() {
|
||||
|
||||
go func() {
|
||||
err := h.Run()
|
||||
if err != nil {
|
||||
logger.Error("Run error: ", err)
|
||||
}
|
||||
}()
|
||||
|
||||
log.Info("Server running...")
|
||||
select {}
|
||||
}
|
||||
|
Reference in New Issue
Block a user