From fa6d5310adbd0615c2bf85155e96444f7c4cb817 Mon Sep 17 00:00:00 2001 From: AlvISsReimu Date: Thu, 16 Jun 2022 01:43:37 +0800 Subject: [PATCH] feat: add frontend config api --- docs/swagger.json | 22 ++++++++++++++++ internal/constant/service.go | 2 ++ internal/controller/v2/0module.go | 1 + internal/controller/v2/config.go | 33 ++++++++++++++++++++++++ internal/model/cache/caches.go | 6 +++++ internal/service/0module.go | 1 + internal/service/frontend_config.go | 40 +++++++++++++++++++++++++++++ 7 files changed, 105 insertions(+) create mode 100644 internal/controller/v2/config.go create mode 100644 internal/service/frontend_config.go diff --git a/docs/swagger.json b/docs/swagger.json index b6471c08..4b5601dd 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -256,6 +256,28 @@ } } }, + "/PenguinStats/api/v2/config": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "FrontendConfig" + ], + "summary": "Get Frontend Config", + "responses": { + "200": { + "description": "" + }, + "500": { + "description": "An unexpected error occurred", + "schema": { + "$ref": "#/definitions/pgerr.PenguinError" + } + } + } + } + }, "/PenguinStats/api/v2/items": { "get": { "produces": [ diff --git a/internal/constant/service.go b/internal/constant/service.go index 6a980961..839c7e88 100644 --- a/internal/constant/service.go +++ b/internal/constant/service.go @@ -3,6 +3,8 @@ package constant const ( FormulaPropertyKey = "formula" + FrontendConfigPropertyKey = "frontend_config" + // SlimHeaderKey is to indicate whether the current request shall be ignored by Sentry transaction tracing. // This is typically used by probes to avoid useless data being sent to Sentry. SlimHeaderKey = "X-Slim" diff --git a/internal/controller/v2/0module.go b/internal/controller/v2/0module.go index dd1c5e7f..3071d9a7 100644 --- a/internal/controller/v2/0module.go +++ b/internal/controller/v2/0module.go @@ -14,6 +14,7 @@ func Module() fx.Option { RegisterReport, RegisterAccount, RegisterFormula, + RegisterFrontendConfig, RegisterPrivate, RegisterSiteStats, RegisterEventPeriod, diff --git a/internal/controller/v2/config.go b/internal/controller/v2/config.go new file mode 100644 index 00000000..b99a7262 --- /dev/null +++ b/internal/controller/v2/config.go @@ -0,0 +1,33 @@ +package v2 + +import ( + "github.com/gofiber/fiber/v2" + "go.uber.org/fx" + + "github.com/penguin-statistics/backend-next/internal/server/svr" + "github.com/penguin-statistics/backend-next/internal/service" +) + +type FrontendConfig struct { + fx.In + + FrontendConfigService *service.FrontendConfig +} + +func RegisterFrontendConfig(v2 *svr.V2, c FrontendConfig) { + v2.Get("/config", c.GetFrontendConfig) +} + +// @Summary Get FrontendConfig +// @Tags FrontendConfig +// @Produce json +// @Success 200 +// @Failure 500 {object} pgerr.PenguinError "An unexpected error occurred" +// @Router /PenguinStats/api/v2/config [GET] +func (c *FrontendConfig) GetFrontendConfig(ctx *fiber.Ctx) error { + formula, err := c.FrontendConfigService.GetFrontendConfig(ctx.Context()) + if err != nil { + return err + } + return ctx.JSON(formula) +} diff --git a/internal/model/cache/caches.go b/internal/model/cache/caches.go index 803ecc3e..7bba603c 100644 --- a/internal/model/cache/caches.go +++ b/internal/model/cache/caches.go @@ -27,6 +27,8 @@ var ( Formula *cache.Singular[json.RawMessage] + FrontendConfig *cache.Singular[json.RawMessage] + Items *cache.Singular[[]*model.Item] ItemByArkID *cache.Set[model.Item] ShimItems *cache.Singular[[]*modelv2.Item] @@ -129,6 +131,10 @@ func initializeCaches() { Formula = cache.NewSingular[json.RawMessage]("formula") SingularFlusherMap["formula"] = Formula.Delete + // frontend_config + FrontendConfig = cache.NewSingular[json.RawMessage]("frontendConfig") + SingularFlusherMap["frontendConfig"] = FrontendConfig.Delete + // item Items = cache.NewSingular[[]*model.Item]("items") ItemByArkID = cache.NewSet[model.Item]("item#arkItemId") diff --git a/internal/service/0module.go b/internal/service/0module.go index f0c37038..6e184fe9 100644 --- a/internal/service/0module.go +++ b/internal/service/0module.go @@ -17,6 +17,7 @@ func Module() fx.Option { NewReport, NewAccount, NewFormula, + NewFrontendConfig, NewActivity, NewDropInfo, NewShortURL, diff --git a/internal/service/frontend_config.go b/internal/service/frontend_config.go new file mode 100644 index 00000000..1da54f21 --- /dev/null +++ b/internal/service/frontend_config.go @@ -0,0 +1,40 @@ +package service + +import ( + "context" + "encoding/json" + "time" + + "github.com/penguin-statistics/backend-next/internal/constant" + "github.com/penguin-statistics/backend-next/internal/model/cache" + "github.com/penguin-statistics/backend-next/internal/repo" +) + +type FrontendConfig struct { + PropertyRepo *repo.Property +} + +func NewFrontendConfig(propertyRepo *repo.Property) *FrontendConfig { + return &FrontendConfig{ + PropertyRepo: propertyRepo, + } +} + +// Cache: (singular) frontend_config, 1 hr +func (s *FrontendConfig) GetFrontendConfig(ctx context.Context) (json.RawMessage, error) { + var frontendConfig json.RawMessage + err := cache.FrontendConfig.Get(&frontendConfig) + if err == nil { + return frontendConfig, nil + } + + property, err := s.PropertyRepo.GetPropertyByKey(ctx, constant.FrontendConfigPropertyKey) + if err != nil { + return nil, err + } + + msg := json.RawMessage([]byte(property.Value)) + go cache.FrontendConfig.Set(msg, time.Hour) + + return msg, nil +}