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:
28
api/handlers.go
Normal file
28
api/handlers.go
Normal file
@ -0,0 +1,28 @@
|
||||
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
|
||||
}
|
||||
}
|
40
api/router.go
Normal file
40
api/router.go
Normal file
@ -0,0 +1,40 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.liteyuki.icu/backend/golite/hertz"
|
||||
"git.liteyuki.icu/backend/golite/logger"
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"github.com/cloudwego/hertz/pkg/app/server"
|
||||
"github.com/cloudwego/hertz/pkg/common/config"
|
||||
)
|
||||
|
||||
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)
|
||||
{
|
||||
be.GET("/", func(ctx context.Context, c *app.RequestContext) { c.JSON(200, "Hello, be") })
|
||||
be.POST("/report", func(ctx context.Context, c *app.RequestContext) {})
|
||||
}
|
||||
|
||||
// fe api 前端接口
|
||||
fe := h.Group("/fe")
|
||||
{
|
||||
fe.GET("/", func(ctx context.Context, c *app.RequestContext) { c.JSON(200, "Hello, fe") })
|
||||
}
|
||||
}
|
||||
|
||||
func Run() {
|
||||
go func() {
|
||||
err := h.Run()
|
||||
if err != nil {
|
||||
logger.Error("Run error: ", err)
|
||||
}
|
||||
}()
|
||||
|
||||
select {}
|
||||
}
|
Reference in New Issue
Block a user