From eb14de5d77684ed83d9ef7789117b2a67b4c2f52 Mon Sep 17 00:00:00 2001 From: Maksym Hrynenko Date: Thu, 6 Jun 2024 15:07:48 +0300 Subject: [PATCH 1/3] add: router config prefix --- internal/config/main.go | 1 + internal/config/routing.go | 29 +++++++++++++++++++++++++++++ internal/service/router.go | 10 ++++++---- 3 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 internal/config/routing.go diff --git a/internal/config/main.go b/internal/config/main.go index a1f317a..44e7cff 100644 --- a/internal/config/main.go +++ b/internal/config/main.go @@ -16,6 +16,7 @@ type Config struct { airdrop comfig.Once verifier comfig.Once + routing comfig.Once getter kv.Getter } diff --git a/internal/config/routing.go b/internal/config/routing.go new file mode 100644 index 0000000..ab19c76 --- /dev/null +++ b/internal/config/routing.go @@ -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) +} diff --git a/internal/service/router.go b/internal/service/router.go index a6a1923..cfc0c7f 100644 --- a/internal/service/router.go +++ b/internal/service/router.go @@ -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") From e8baa5409589a04a4fcc159d5f83b5fe74ff665b Mon Sep 17 00:00:00 2001 From: Maksym Hrynenko Date: Thu, 6 Jun 2024 15:07:48 +0300 Subject: [PATCH 2/3] add: router config prefix --- internal/config/main.go | 1 + internal/config/routing.go | 29 +++++++++++++++++++++++++++++ internal/service/router.go | 10 ++++++---- 3 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 internal/config/routing.go diff --git a/internal/config/main.go b/internal/config/main.go index 27a65f9..488d9c2 100644 --- a/internal/config/main.go +++ b/internal/config/main.go @@ -17,6 +17,7 @@ type Config struct { airdrop comfig.Once verifier comfig.Once + routing comfig.Once getter kv.Getter } diff --git a/internal/config/routing.go b/internal/config/routing.go new file mode 100644 index 0000000..ab19c76 --- /dev/null +++ b/internal/config/routing.go @@ -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) +} diff --git a/internal/service/router.go b/internal/service/router.go index ca0e688..af37969 100644 --- a/internal/service/router.go +++ b/internal/service/router.go @@ -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") From 8d24ec51054a84c25a05ee0ff9c7fefad9dd46ee Mon Sep 17 00:00:00 2001 From: Maksym Hrynenko Date: Thu, 6 Jun 2024 15:16:11 +0300 Subject: [PATCH 3/3] add: routing config example --- config.yaml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/config.yaml b/config.yaml index bf92fb5..e750535 100644 --- a/config.yaml +++ b/config.yaml @@ -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 @@ -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: