Skip to content

Commit

Permalink
feat: add frontend config api
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvISsReimu committed Jun 15, 2022
1 parent a6eb9cb commit fa6d531
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 0 deletions.
22 changes: 22 additions & 0 deletions docs/swagger.json
Expand Up @@ -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": [
Expand Down
2 changes: 2 additions & 0 deletions internal/constant/service.go
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions internal/controller/v2/0module.go
Expand Up @@ -14,6 +14,7 @@ func Module() fx.Option {
RegisterReport,
RegisterAccount,
RegisterFormula,
RegisterFrontendConfig,
RegisterPrivate,
RegisterSiteStats,
RegisterEventPeriod,
Expand Down
33 changes: 33 additions & 0 deletions 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)
}
6 changes: 6 additions & 0 deletions internal/model/cache/caches.go
Expand Up @@ -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]
Expand Down Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions internal/service/0module.go
Expand Up @@ -17,6 +17,7 @@ func Module() fx.Option {
NewReport,
NewAccount,
NewFormula,
NewFrontendConfig,
NewActivity,
NewDropInfo,
NewShortURL,
Expand Down
40 changes: 40 additions & 0 deletions 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
}

0 comments on commit fa6d531

Please sign in to comment.