Skip to content

Commit

Permalink
Merge pull request #47 from rarimo/add-maintenance
Browse files Browse the repository at this point in the history
Add maintenance
  • Loading branch information
2pa-arch authored Feb 17, 2025
2 parents 1387ce7 + 90414e2 commit aa8e5ea
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 4 deletions.
3 changes: 3 additions & 0 deletions config-testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ db:
listener:
addr: localhost:8000

maintenance:
is_maintenance: false

event_types:
types:
- name: free_weekly
Expand Down
3 changes: 3 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ db:
listener:
addr: localhost:8000

maintenance:
is_maintenance: false

event_types:
types:
- name: passport_scan
Expand Down
15 changes: 15 additions & 0 deletions docs/spec/components/schemas/Maintenance.yaml
Original file line number Diff line number Diff line change
@@ -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."
7 changes: 7 additions & 0 deletions docs/spec/components/schemas/MaintenanceKey.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type: object
required:
- type
properties:
type:
type: string
enum: [ maintenance ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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'
11 changes: 7 additions & 4 deletions internal/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Config interface {
Levels() Levels
Verifier() *zk.Verifier
PointPrice() PointsPrice
Maintenance() Maintenance
}

type config struct {
Expand All @@ -39,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 {
Expand Down
27 changes: 27 additions & 0 deletions internal/config/maintenance.go
Original file line number Diff line number Diff line change
@@ -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.maintenance.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)
}
11 changes: 11 additions & 0 deletions internal/service/handlers/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
verifierCtxKey
levelsCtxKey
countriesConfigCtxKey
maintenanceCtxKey
)

func CtxLog(entry *logan.Entry) func(context.Context) context.Context {
Expand Down Expand Up @@ -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)
}
21 changes: 21 additions & 0 deletions internal/service/handlers/maintenance.go
Original file line number Diff line number Diff line change
@@ -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,
},
},
})
}
2 changes: 2 additions & 0 deletions internal/service/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
)
Expand All @@ -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) {
Expand Down
43 changes: 43 additions & 0 deletions resources/model_maintenance.go
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 10 additions & 0 deletions resources/model_maintenance_attributes.go
Original file line number Diff line number Diff line change
@@ -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"`
}
1 change: 1 addition & 0 deletions resources/model_resource_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit aa8e5ea

Please sign in to comment.