diff --git a/Makefile b/Makefile index e4b7f769..69ef46a3 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,6 @@ all: \ backend/config/deployment/config.go: platform/local/deployment.yml backend/config/deployment/directives.yml gonfique generate \ - -pkg deployment \ - -directives backend/config/deployment/directives.yml \ + -config backend/config/deployment/directives.yml \ -in platform/local/deployment.yml \ -out backend/config/deployment/config.go \ No newline at end of file diff --git a/backend/config/deployment/config.go b/backend/config/deployment/config.go index 46737452..8aa4789d 100644 --- a/backend/config/deployment/config.go +++ b/backend/config/deployment/config.go @@ -1,55 +1,79 @@ -// Code generated by gonfique. DO NOT EDIT. +// Code generated by gonfique v2.0.0-alpha.9. DO NOT EDIT. package deployment import ( "fmt" - "gopkg.in/yaml.v3" "os" "time" + + "gopkg.in/yaml.v3" ) +// exported for cors +type Cors struct { + AllowOrigin string `yaml:"allow-origin"` +} + +// exported for environment +type Environment string + +// exported for ports +type Ports struct { + Gateway int `yaml:"gateway"` + Internal int `yaml:"internal"` + Objectives int `yaml:"objectives"` + Pdp int `yaml:"pdp"` + Profiles int `yaml:"profiles"` + Registration int `yaml:"registration"` + Registry int `yaml:"registry"` + Sessions int `yaml:"sessions"` + Tags int `yaml:"tags"` + Users int `yaml:"users"` +} + +// exported for reception +type Reception struct { + RequestTimeout time.Duration `yaml:"request-timeout"` +} + +// exported for registry +type Registry struct { + ClearanceDelay time.Duration `yaml:"clearance-delay"` + ClearancePeriod time.Duration `yaml:"clearance-period"` + InstanceTimeout time.Duration `yaml:"instance-timeout"` +} + +// exported for registry-file +type RegistryFile struct { + UpdatePeriod time.Duration `yaml:"update-period"` +} + +// exported for router type Router struct { - Cors struct { - AllowOrigin string `yaml:"allow-origin"` - } `yaml:"cors"` GracePeriod time.Duration `yaml:"grace-period"` IdleTimeout time.Duration `yaml:"idle-timeout"` ReadTimeout time.Duration `yaml:"read-timeout"` WriteTimeout time.Duration `yaml:"write-timeout"` } + +// exported for sidecar +type Sidecar struct { + QueryingTickerDelay time.Duration `yaml:"querying-ticker-delay"` + QueryingTickerPeriod time.Duration `yaml:"querying-ticker-period"` + RechekingTickerDelay time.Duration `yaml:"recheking-ticker-delay"` + RechekingTickerPeriod time.Duration `yaml:"recheking-ticker-period"` +} + type Config struct { - Environment string `yaml:"environment"` - Ports struct { - Gateway int `yaml:"gateway"` - Internal int `yaml:"internal"` - Objectives int `yaml:"objectives"` - Pdp int `yaml:"pdp"` - Profiles int `yaml:"profiles"` - Registration int `yaml:"registration"` - Registry int `yaml:"registry"` - Sessions int `yaml:"sessions"` - Tags int `yaml:"tags"` - Users int `yaml:"users"` - } `yaml:"ports"` - Reception struct { - RequestTimeout time.Duration `yaml:"request-timeout"` - } `yaml:"reception"` - Registry struct { - ClearanceDelay time.Duration `yaml:"clearance-delay"` - ClearancePeriod time.Duration `yaml:"clearance-period"` - InstanceTimeout time.Duration `yaml:"instance-timeout"` - } `yaml:"registry"` - RegistryFile struct { - UpdatePeriod time.Duration `yaml:"update-period"` - } `yaml:"registry-file"` - Router Router `yaml:"router"` - Sidecar struct { - QueryingTickerDelay time.Duration `yaml:"querying-ticker-delay"` - QueryingTickerPeriod time.Duration `yaml:"querying-ticker-period"` - RechekingTickerDelay time.Duration `yaml:"recheking-ticker-delay"` - RechekingTickerPeriod time.Duration `yaml:"recheking-ticker-period"` - } `yaml:"sidecar"` + Cors Cors `yaml:"cors"` + Environment Environment `yaml:"environment"` + Ports Ports `yaml:"ports"` + Reception Reception `yaml:"reception"` + Registry Registry `yaml:"registry"` + RegistryFile RegistryFile `yaml:"registry-file"` + Router Router `yaml:"router"` + Sidecar Sidecar `yaml:"sidecar"` } func ReadConfig(path string) (*Config, error) { diff --git a/backend/config/deployment/directives.yml b/backend/config/deployment/directives.yml index f1870af2..cb14159b 100644 --- a/backend/config/deployment/directives.yml +++ b/backend/config/deployment/directives.yml @@ -1,2 +1,12 @@ -router: - declare: Router +meta: + package: deployment + type: Config + +rules: + "*": {export: true} + + "sidecar.*": {replace: time.Duration time} + "registry.*": {replace: time.Duration time} + "registry-file.*": {replace: time.Duration time} + "reception.*": {replace: time.Duration time} + "router.*": {replace: time.Duration time} \ No newline at end of file diff --git a/backend/internal/web/reception/agent.go b/backend/internal/web/reception/agent.go index 12f5a440..c4edb974 100644 --- a/backend/internal/web/reception/agent.go +++ b/backend/internal/web/reception/agent.go @@ -2,28 +2,29 @@ package reception import ( "fmt" + "net/http" + "net/url" + "logbook/config/deployment" "logbook/internal/logger" "logbook/internal/web/forwarder" "logbook/models" - "net/http" - "net/url" "go.ufukty.com/gohandlers/pkg/gohandlers" ) // [Agent] is the registration Agent which helps services, and gateways to register their handlers and forwarders appropriately type Agent struct { - deplcfg *deployment.Config - r *http.ServeMux - l *logger.Logger + c *deployment.Config + r *http.ServeMux + l *logger.Logger } -func NewAgent(deplcfg *deployment.Config, l *logger.Logger) *Agent { +func NewAgent(c *deployment.Config, l *logger.Logger) *Agent { return &Agent{ - deplcfg: deplcfg, - r: http.NewServeMux(), - l: l.Sub("agent"), + c: c, + r: http.NewServeMux(), + l: l.Sub("agent"), } } @@ -42,7 +43,7 @@ type Lister interface { } func (ag *Agent) RegisterEndpoints(public, private Lister) error { - origin, err := url.JoinPath(ag.deplcfg.Router.Cors.AllowOrigin) + origin, err := url.JoinPath(ag.c.Cors.AllowOrigin) if err != nil { return fmt.Errorf("url.JoinPath: %w", err) } @@ -52,7 +53,7 @@ func (ag *Agent) RegisterEndpoints(public, private Lister) error { if public != nil { for hn, info := range public.ListHandlers() { c := newCors(info.Ref, origin, []string{info.Method}, corsheaders) - pl := newReceptionist(ag.deplcfg, ag.l.Sub(info.Path), c) + pl := newReceptionist(ag.c, ag.l.Sub(info.Path), c) ag.l.Printf("registering: %s (%s, OPTIONS %s) -> %p\n", hn, info.Method, info.Path, pl) for _, method := range []string{info.Method, "OPTIONS"} { @@ -64,15 +65,15 @@ func (ag *Agent) RegisterEndpoints(public, private Lister) error { if private != nil { for hn, info := range private.ListHandlers() { - pl := newReceptionist(ag.deplcfg, ag.l.Sub(info.Path), info.Ref) + pl := newReceptionist(ag.c, ag.l.Sub(info.Path), info.Ref) pattern := fmt.Sprintf("%s %s", info.Method, info.Path) ag.l.Printf("registering: %s (%s) -> %p\n", hn, pattern, pl) ag.r.Handle(pattern, pl) } } - ag.r.Handle("GET /ping", newReceptionist(ag.deplcfg, ag.l.Sub("ping"), http.HandlerFunc(pong))) - ag.r.Handle("GET /", newReceptionist(ag.deplcfg, ag.l.Sub("not-found"), http.HandlerFunc(http.NotFound))) + ag.r.Handle("GET /ping", newReceptionist(ag.c, ag.l.Sub("ping"), http.HandlerFunc(pong))) + ag.r.Handle("GET /", newReceptionist(ag.c, ag.l.Sub("not-found"), http.HandlerFunc(http.NotFound))) return nil } @@ -81,11 +82,11 @@ func (ag *Agent) RegisterForwarders(fwds map[models.Service]*forwarder.LoadBalan for addr, fwd := range fwds { ag.l.Printf("registering forwarder for: %s -> %p\n", addr, fwd) l := ag.l.Sub(fmt.Sprintf("strip-prefix(%s)", addr)) - ag.r.Handle(string(addr)+"/", newReceptionist(ag.deplcfg, l, http.StripPrefix(string(addr), fwd))) + ag.r.Handle(string(addr)+"/", newReceptionist(ag.c, l, http.StripPrefix(string(addr), fwd))) } - ag.r.Handle("/ping", newReceptionist(ag.deplcfg, ag.l.Sub("ping"), http.HandlerFunc(pong))) - ag.r.Handle("/", newReceptionist(ag.deplcfg, ag.l.Sub("not-found"), http.HandlerFunc(http.NotFound))) + ag.r.Handle("/ping", newReceptionist(ag.c, ag.l.Sub("ping"), http.HandlerFunc(pong))) + ag.r.Handle("/", newReceptionist(ag.c, ag.l.Sub("not-found"), http.HandlerFunc(http.NotFound))) return nil } diff --git a/dev/macos.sh b/dev/macos.sh index e83bd099..3a03bd95 100644 --- a/dev/macos.sh +++ b/dev/macos.sh @@ -19,7 +19,7 @@ which go || which stringer || go install "golang.org/x/tools/cmd/stringer@latest" which gonfique || - go install "github.com/ufukty/gonfique@v1.3.1" + go install "go.ufukty.com/gonfique/v2@v2.0.0-alpha.9" which sqlc || go install "github.com/sqlc-dev/sqlc/cmd/sqlc@latest" which govalid || diff --git a/platform/local/deployment.yml b/platform/local/deployment.yml index 2bdbb47d..5b031017 100644 --- a/platform/local/deployment.yml +++ b/platform/local/deployment.yml @@ -5,8 +5,9 @@ router: read-timeout: 1s idle-timeout: 1s grace-period: 1s - cors: - allow-origin: "*" + +cors: + allow-origin: "*" reception: request-timeout: 2s