From af39eeed276215a7932f746389a13c75b1296455 Mon Sep 17 00:00:00 2001 From: 2pa-arch <2pa-arch@users.noreply.github.com> Date: Fri, 14 Feb 2025 17:41:06 +0200 Subject: [PATCH 1/3] added maintenance status endpoint --- config-testing.yaml | 3 ++ config.yaml | 3 ++ docs/spec/components/schemas/Maintenance.yaml | 15 +++++++ .../components/schemas/MaintenanceKey.yaml | 7 +++ ...rime-points-svc@v1@public@maintenance.yaml | 20 +++++++++ internal/config/main.go | 1 + internal/config/maintenance.go | 27 ++++++++++++ internal/service/handlers/ctx.go | 11 +++++ internal/service/handlers/maintenance.go | 21 +++++++++ internal/service/router.go | 2 + resources/model_maintenance.go | 43 +++++++++++++++++++ resources/model_maintenance_attributes.go | 10 +++++ resources/model_resource_type.go | 1 + 13 files changed, 164 insertions(+) create mode 100644 docs/spec/components/schemas/Maintenance.yaml create mode 100644 docs/spec/components/schemas/MaintenanceKey.yaml create mode 100644 docs/spec/paths/integrations@rarime-points-svc@v1@public@maintenance.yaml create mode 100644 internal/config/maintenance.go create mode 100644 internal/service/handlers/maintenance.go create mode 100644 resources/model_maintenance.go create mode 100644 resources/model_maintenance_attributes.go diff --git a/config-testing.yaml b/config-testing.yaml index 76dcfee..e270459 100644 --- a/config-testing.yaml +++ b/config-testing.yaml @@ -8,6 +8,9 @@ db: listener: addr: localhost:8000 +maintenance: + is_maintenance: false + event_types: types: - name: free_weekly diff --git a/config.yaml b/config.yaml index 604c251..666f90e 100644 --- a/config.yaml +++ b/config.yaml @@ -8,6 +8,9 @@ db: listener: addr: localhost:8000 +maintenance: + is_maintenance: false + event_types: types: - name: passport_scan diff --git a/docs/spec/components/schemas/Maintenance.yaml b/docs/spec/components/schemas/Maintenance.yaml new file mode 100644 index 0000000..6c7b6c4 --- /dev/null +++ b/docs/spec/components/schemas/Maintenance.yaml @@ -0,0 +1,15 @@ +allOf: + - $ref: '#/components/schemas/MaintenanceKey' + - type: object + required: + - attributes + properties: + attributes: + type: object + required: + - maintenance + properties: + maintenance: + type: bool + example: false + description: "true if the service is under maintenance and false otherwise." \ No newline at end of file diff --git a/docs/spec/components/schemas/MaintenanceKey.yaml b/docs/spec/components/schemas/MaintenanceKey.yaml new file mode 100644 index 0000000..5dfcee4 --- /dev/null +++ b/docs/spec/components/schemas/MaintenanceKey.yaml @@ -0,0 +1,7 @@ +type: object +required: + - type +properties: + type: + type: string + enum: [ maintenance ] diff --git a/docs/spec/paths/integrations@rarime-points-svc@v1@public@maintenance.yaml b/docs/spec/paths/integrations@rarime-points-svc@v1@public@maintenance.yaml new file mode 100644 index 0000000..37159ca --- /dev/null +++ b/docs/spec/paths/integrations@rarime-points-svc@v1@public@maintenance.yaml @@ -0,0 +1,20 @@ +get: + tags: + - Maintenance + summary: Get maintenance status + description: Returns true if the service is under maintenance and false otherwise. + operationId: getMaintenanceStatus + responses: + 200: + description: Success + content: + application/vnd.api+json: + schema: + type: object + required: + - data + properties: + data: + $ref: '#/components/schemas/Maintenance' + 500: + $ref: '#/components/responses/internalError' diff --git a/internal/config/main.go b/internal/config/main.go index a2d12ea..240f1bc 100644 --- a/internal/config/main.go +++ b/internal/config/main.go @@ -26,6 +26,7 @@ type Config interface { Levels() Levels Verifier() *zk.Verifier PointPrice() PointsPrice + Maintenance() Maintenance } type config struct { diff --git a/internal/config/maintenance.go b/internal/config/maintenance.go new file mode 100644 index 0000000..5739335 --- /dev/null +++ b/internal/config/maintenance.go @@ -0,0 +1,27 @@ +package config + +import ( + "fmt" + + "gitlab.com/distributed_lab/figure/v3" + "gitlab.com/distributed_lab/kit/kv" +) + +type Maintenance struct { + IsMaintenance bool `fig:"is_maintenance"` +} + +func (c *config) Maintenance() Maintenance { + return c.pointPrice.Do(func() interface{} { + var cfg Maintenance + + err := figure.Out(&cfg). + From(kv.MustGetStringMap(c.getter, "maintenance")). + Please() + if err != nil { + panic(fmt.Errorf("failed to figure out is_maintenance: %w", err)) + } + + return cfg + }).(Maintenance) +} diff --git a/internal/service/handlers/ctx.go b/internal/service/handlers/ctx.go index f464b06..15f3885 100644 --- a/internal/service/handlers/ctx.go +++ b/internal/service/handlers/ctx.go @@ -30,6 +30,7 @@ const ( verifierCtxKey levelsCtxKey countriesConfigCtxKey + maintenanceCtxKey ) func CtxLog(entry *logan.Entry) func(context.Context) context.Context { @@ -161,3 +162,13 @@ func CtxCountriesConfig(config countrier.Config) func(context.Context) context.C func CountriesConfig(r *http.Request) countrier.Config { return r.Context().Value(countriesConfigCtxKey).(countrier.Config) } + +func CtxMaintenance(maintenance config.Maintenance) func(context.Context) context.Context { + return func(ctx context.Context) context.Context { + return context.WithValue(ctx, maintenanceCtxKey, maintenance) + } +} + +func MaintenanceConfig(r *http.Request) config.Maintenance { + return r.Context().Value(maintenanceCtxKey).(config.Maintenance) +} diff --git a/internal/service/handlers/maintenance.go b/internal/service/handlers/maintenance.go new file mode 100644 index 0000000..c5e5c13 --- /dev/null +++ b/internal/service/handlers/maintenance.go @@ -0,0 +1,21 @@ +package handlers + +import ( + "net/http" + + "github.com/rarimo/rarime-points-svc/resources" + "gitlab.com/distributed_lab/ape" +) + +func MaintenanceHandler(w http.ResponseWriter, r *http.Request) { + ape.Render(w, resources.MaintenanceResponse{ + Data: resources.Maintenance{ + Key: resources.Key{ + Type: resources.MAINTENANCE, + }, + Attributes: resources.MaintenanceAttributes{ + Maintenance: MaintenanceConfig(r).IsMaintenance, + }, + }, + }) +} diff --git a/internal/service/router.go b/internal/service/router.go index 6b48600..079ff3f 100644 --- a/internal/service/router.go +++ b/internal/service/router.go @@ -25,6 +25,7 @@ func Run(ctx context.Context, cfg config.Config) { handlers.CtxLevels(cfg.Levels()), handlers.CtxVerifier(cfg.Verifier()), handlers.CtxCountriesConfig(cfg.Countries()), + handlers.CtxMaintenance(cfg.Maintenance()), ), handlers.DBCloneMiddleware(cfg.DB()), ) @@ -51,6 +52,7 @@ func Run(ctx context.Context, cfg config.Config) { r.Get("/point_price", handlers.GetPointPrice) r.Get("/countries_config", handlers.GetCountriesConfig) r.Get("/event_types", handlers.ListEventTypes) + r.Get("/maintenance", handlers.MaintenanceHandler) }) // must be accessible only within the cluster r.Route("/private", func(r chi.Router) { diff --git a/resources/model_maintenance.go b/resources/model_maintenance.go new file mode 100644 index 0000000..a45749e --- /dev/null +++ b/resources/model_maintenance.go @@ -0,0 +1,43 @@ +/* + * GENERATED. Do not modify. Your changes might be overwritten! + */ + +package resources + +import "encoding/json" + +type Maintenance struct { + Key + Attributes MaintenanceAttributes `json:"attributes"` +} +type MaintenanceResponse struct { + Data Maintenance `json:"data"` + Included Included `json:"included"` +} + +type MaintenanceListResponse struct { + Data []Maintenance `json:"data"` + Included Included `json:"included"` + Links *Links `json:"links"` + Meta json.RawMessage `json:"meta,omitempty"` +} + +func (r *MaintenanceListResponse) PutMeta(v interface{}) (err error) { + r.Meta, err = json.Marshal(v) + return err +} + +func (r *MaintenanceListResponse) GetMeta(out interface{}) error { + return json.Unmarshal(r.Meta, out) +} + +// MustMaintenance - returns Maintenance from include collection. +// if entry with specified key does not exist - returns nil +// if entry with specified key exists but type or ID mismatches - panics +func (c *Included) MustMaintenance(key Key) *Maintenance { + var maintenance Maintenance + if c.tryFindEntry(key, &maintenance) { + return &maintenance + } + return nil +} diff --git a/resources/model_maintenance_attributes.go b/resources/model_maintenance_attributes.go new file mode 100644 index 0000000..eff0087 --- /dev/null +++ b/resources/model_maintenance_attributes.go @@ -0,0 +1,10 @@ +/* + * GENERATED. Do not modify. Your changes might be overwritten! + */ + +package resources + +type MaintenanceAttributes struct { + // true if the service is under maintenance and false otherwise. + Maintenance bool `json:"maintenance"` +} diff --git a/resources/model_resource_type.go b/resources/model_resource_type.go index 71889b9..8ba2c52 100644 --- a/resources/model_resource_type.go +++ b/resources/model_resource_type.go @@ -16,6 +16,7 @@ const ( EVENT ResourceType = "event" EVENT_TYPE ResourceType = "event_type" JOIN_PROGRAM ResourceType = "join_program" + MAINTENANCE ResourceType = "maintenance" PASSPORT_EVENT_STATE ResourceType = "passport_event_state" POINT_PRICE ResourceType = "point_price" VERIFY_PASSPORT ResourceType = "verify_passport" From f5f2489d793aef4d0d442893102d8f44730e9f78 Mon Sep 17 00:00:00 2001 From: 2pa-arch <2pa-arch@users.noreply.github.com> Date: Fri, 14 Feb 2025 18:15:36 +0200 Subject: [PATCH 2/3] added maintenance Once --- internal/config/main.go | 10 ++++++---- internal/config/maintenance.go | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/internal/config/main.go b/internal/config/main.go index 240f1bc..e790041 100644 --- a/internal/config/main.go +++ b/internal/config/main.go @@ -40,10 +40,12 @@ type config struct { sbtcheck.SbtChecker countrier.Countrier - levels comfig.Once - verifier comfig.Once - pointPrice comfig.Once - getter kv.Getter + levels comfig.Once + verifier comfig.Once + pointPrice comfig.Once + maintenance comfig.Once + + getter kv.Getter } func New(getter kv.Getter) Config { diff --git a/internal/config/maintenance.go b/internal/config/maintenance.go index 5739335..98df9a5 100644 --- a/internal/config/maintenance.go +++ b/internal/config/maintenance.go @@ -12,7 +12,7 @@ type Maintenance struct { } func (c *config) Maintenance() Maintenance { - return c.pointPrice.Do(func() interface{} { + return c.maintenance.Do(func() interface{} { var cfg Maintenance err := figure.Out(&cfg). From 90414e25fd665f1cd9ae0a5a3a690f90d4bfda11 Mon Sep 17 00:00:00 2001 From: 2pa-arch <2pa-arch@users.noreply.github.com> Date: Fri, 14 Feb 2025 18:27:54 +0200 Subject: [PATCH 3/3] change docs --- .../integrations@rarime-points-svc@v1@public@maintenance.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/docs/spec/paths/integrations@rarime-points-svc@v1@public@maintenance.yaml b/docs/spec/paths/integrations@rarime-points-svc@v1@public@maintenance.yaml index 37159ca..8ce6393 100644 --- a/docs/spec/paths/integrations@rarime-points-svc@v1@public@maintenance.yaml +++ b/docs/spec/paths/integrations@rarime-points-svc@v1@public@maintenance.yaml @@ -16,5 +16,3 @@ get: properties: data: $ref: '#/components/schemas/Maintenance' - 500: - $ref: '#/components/responses/internalError'