Skip to content

Commit

Permalink
feat: allow config context
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Sep 7, 2022
1 parent 8cffc5b commit d894c97
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
15 changes: 11 additions & 4 deletions driver/factory.go
Expand Up @@ -2,6 +2,7 @@ package driver

import (
"context"
"github.com/ory/hydra/x/servicelocatorx"
"github.com/ory/x/servicelocator"

"github.com/ory/x/configx"
Expand Down Expand Up @@ -66,16 +67,22 @@ func New(ctx context.Context, opts ...OptionsModifier) Registry {
}

l := servicelocator.Logger(ctx, logrusx.New("Ory Hydra", config.Version))
c, err := config.New(ctx, l, o.opts...)
if err != nil {
l.WithError(err).Fatal("Unable to instantiate configuration.")
ctxter := servicelocator.Contextualizer(ctx, &contextx.Default{})

c := servicelocatorx.ConfigFromContext(ctx, nil)
if c == nil {
var err error
c, err = config.New(ctx, l, o.opts...)
if err != nil {
l.WithError(err).Fatal("Unable to instantiate configuration.")
}
}

if o.validate {
config.MustValidate(ctx, l, c)
}

r, err := NewRegistryFromDSN(ctx, c, l, o.skipNetworkInit, false, servicelocator.Contextualizer(ctx, &contextx.Default{}))
r, err := NewRegistryFromDSN(ctx, c, l, o.skipNetworkInit, false, ctxter)
if err != nil {
l.WithError(err).Fatal("Unable to create service registry.")
}
Expand Down
25 changes: 25 additions & 0 deletions x/servicelocatorx/config.go
@@ -0,0 +1,25 @@
package servicelocatorx

import (
"context"
"github.com/ory/hydra/driver/config"
)

type key int

const (
keyConfig key = iota + 1
)

// ContextWithConfig returns a new context with the provided config.
func ContextWithConfig(ctx context.Context, c *config.DefaultProvider) context.Context {
return context.WithValue(ctx, keyConfig, c)
}

// ConfigFromContext returns the config from the context.
func ConfigFromContext(ctx context.Context, fallback *config.DefaultProvider) *config.DefaultProvider {
if c, ok := ctx.Value(keyConfig).(*config.DefaultProvider); ok {
return c
}
return fallback
}

0 comments on commit d894c97

Please sign in to comment.