Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: configurable routing #4

Merged
merged 5 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ db:
listener:
addr: :8000

# Routing config is used for deploying several service instances under one domain to differentiate them
# by configurable prefix. Note that prefix should be formatted as `/integrations/configured-service-prefix/`
routing:
prefix: "/integrations/configured-service-prefix/"

airdrop:
amount: amount
token_address: erc20_token_address
Expand All @@ -21,7 +26,7 @@ broadcaster:
verifier:
verification_key_path: "./verification_key.json"
allowed_age: 18
allowed_citizenships: ["UKR"]
allowed_citizenships: [ "UKR" ]
allowed_event_id: "event_id"
allowed_query_selector: "query_selector"
# at least one of these should be correct to pass:
Expand Down
1 change: 1 addition & 0 deletions internal/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Config struct {

airdrop comfig.Once
verifier comfig.Once
routing comfig.Once
getter kv.Getter
}

Expand Down
29 changes: 29 additions & 0 deletions internal/config/routing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package config

import (
"gitlab.com/distributed_lab/figure/v3"
"gitlab.com/distributed_lab/kit/kv"
"gitlab.com/distributed_lab/logan/v3"
"gitlab.com/distributed_lab/logan/v3/errors"
)

const routingYamlKey = "routing"

type Routing struct {
Prefix string `fig:"prefix,required"`
}

func (c *Config) NewRouting() Routing {
return c.routing.Do(func() interface{} {
var cfg Routing

err := figure.Out(&cfg).
From(kv.MustGetStringMap(c.getter, routingYamlKey)).
Please()
if err != nil {
panic(errors.Wrap(err, "failed to figure out config", logan.F{"key": routingYamlKey}))
}

return cfg
}).(Routing)
}
10 changes: 6 additions & 4 deletions internal/service/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ func Run(ctx context.Context, cfg *config.Config) {
),
handlers.DBCloneMiddleware(cfg.DB()),
)
r.Route("/integrations/evm-airdrop-svc/airdrops", func(r chi.Router) {
r.Post("/", handlers.CreateAirdrop)
r.Get("/{nullifier}", handlers.GetAirdrop)
r.Get("/params", handlers.GetAirdropParams)
r.Route(cfg.NewRouting().Prefix, func(r chi.Router) {
r.Route("/airdrops", func(r chi.Router) {
r.Post("/", handlers.CreateAirdrop)
r.Get("/{nullifier}", handlers.GetAirdrop)
r.Get("/params", handlers.GetAirdropParams)
})
})

cfg.Log().Info("Service started")
Expand Down
Loading