-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
meta.go
52 lines (41 loc) · 1.06 KB
/
meta.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package meta
import (
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cache"
"go.uber.org/fx"
"exusiai.dev/backend-next/internal/pkg/bininfo"
"exusiai.dev/backend-next/internal/server/svr"
"exusiai.dev/backend-next/internal/service"
"exusiai.dev/gommon/constant"
)
type Meta struct {
fx.In
HealthService *service.Health
}
func RegisterMeta(meta *svr.Meta, c Meta) {
meta.Get("/bininfo", c.BinInfo)
meta.Get("/health", cache.New(cache.Config{
// cache it for a second to mitigate potential DDoS
Expiration: time.Second,
CacheHeader: constant.CacheHeader,
}), c.Health)
meta.Get("/ping", func(c *fiber.Ctx) error {
// only allow intranet access to prevent abuse
return c.SendString("pong")
})
}
func (c *Meta) BinInfo(ctx *fiber.Ctx) error {
return ctx.JSON(fiber.Map{
"version": bininfo.Version,
"build": bininfo.BuildTime,
})
}
func (c *Meta) Health(ctx *fiber.Ctx) error {
if err := c.HealthService.Ping(ctx.UserContext()); err != nil {
return err
}
return ctx.JSON(fiber.Map{
"status": "ok",
})
}