Skip to content

Commit

Permalink
fix: make servicelocator explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Aug 19, 2022
1 parent a115486 commit 4f841da
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 65 deletions.
6 changes: 5 additions & 1 deletion cmd/cliclient/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cliclient
import (
"github.com/pkg/errors"

"github.com/ory/x/servicelocatorx"

"github.com/ory/x/contextx"

"github.com/ory/x/configx"
Expand Down Expand Up @@ -36,7 +38,9 @@ func (h *CleanupHandler) CleanupSQL(cmd *cobra.Command, args []string) error {
d, err := driver.NewWithoutInit(
cmd.Context(),
cmd.ErrOrStderr(),
opts...,
servicelocatorx.NewOptions(),
nil,
opts,
)
if len(d.Config().DSN(cmd.Context())) == 0 {
return errors.New(`required config value "dsn" was not set`)
Expand Down
20 changes: 15 additions & 5 deletions cmd/cliclient/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"strings"

"github.com/ory/x/servicelocatorx"

"github.com/pkg/errors"

"github.com/ory/x/contextx"
Expand Down Expand Up @@ -35,8 +37,12 @@ func (h *MigrateHandler) MigrateSQL(cmd *cobra.Command, args []string) error {
d, err = driver.NewWithoutInit(
cmd.Context(),
cmd.ErrOrStderr(),
configx.WithFlags(cmd.Flags()),
configx.SkipValidation())
servicelocatorx.NewOptions(),
nil,
[]configx.OptionModifier{
configx.WithFlags(cmd.Flags()),
configx.SkipValidation(),
})
if err != nil {
return err
}
Expand All @@ -57,9 +63,13 @@ func (h *MigrateHandler) MigrateSQL(cmd *cobra.Command, args []string) error {
d, err = driver.NewWithoutInit(
cmd.Context(),
cmd.ErrOrStderr(),
configx.WithFlags(cmd.Flags()),
configx.SkipValidation(),
configx.WithValue(config.ViperKeyDSN, args[0]))
servicelocatorx.NewOptions(),
nil,
[]configx.OptionModifier{
configx.WithFlags(cmd.Flags()),
configx.SkipValidation(),
configx.WithValue(config.ViperKeyDSN, args[0]),
})
if err != nil {
return err
}
Expand Down
7 changes: 5 additions & 2 deletions cmd/courier/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package courier
import (
"github.com/spf13/cobra"

"github.com/ory/kratos/driver"
"github.com/ory/x/servicelocatorx"

"github.com/ory/x/configx"
)

Expand All @@ -16,8 +19,8 @@ func NewCourierCmd() *cobra.Command {
return c
}

func RegisterCommandRecursive(parent *cobra.Command) {
func RegisterCommandRecursive(parent *cobra.Command, slOpts []servicelocatorx.Option, dOpts []driver.RegistryOption) {
c := NewCourierCmd()
parent.AddCommand(c)
c.AddCommand(NewWatchCmd())
c.AddCommand(NewWatchCmd(slOpts, dOpts))
}
6 changes: 4 additions & 2 deletions cmd/courier/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
cx "context"
"net/http"

"github.com/ory/x/servicelocatorx"

"golang.org/x/sync/errgroup"

"github.com/spf13/cobra"
Expand All @@ -17,12 +19,12 @@ import (
"github.com/ory/x/reqlog"
)

func NewWatchCmd() *cobra.Command {
func NewWatchCmd(slOpts []servicelocatorx.Option, dOpts []driver.RegistryOption) *cobra.Command {
var c = &cobra.Command{
Use: "watch",
Short: "Starts the Ory Kratos message courier",
RunE: func(cmd *cobra.Command, args []string) error {
r, err := driver.New(cmd.Context(), cmd.ErrOrStderr(), configx.WithFlags(cmd.Flags()))
r, err := driver.New(cmd.Context(), cmd.ErrOrStderr(), servicelocatorx.NewOptions(slOpts...), dOpts, []configx.OptionModifier{configx.WithFlags(cmd.Flags())})
if err != nil {
return err
}
Expand Down
37 changes: 11 additions & 26 deletions cmd/daemon/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"crypto/tls"
"net/http"

"github.com/ory/x/servicelocatorx"

"github.com/pkg/errors"
"golang.org/x/net/context"

"github.com/ory/x/servicelocator"

"golang.org/x/sync/errgroup"

"github.com/ory/kratos/schema"
Expand Down Expand Up @@ -52,7 +52,6 @@ import (
)

type options struct {
mwf []func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)
ctx stdctx.Context
}

Expand All @@ -67,31 +66,21 @@ func NewOptions(ctx stdctx.Context, opts []Option) *options {

type Option func(*options)

func WithRootMiddleware(m func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)) Option {
return func(o *options) {
o.mwf = append(o.mwf, m)
}
}

func WithContext(ctx stdctx.Context) Option {
return func(o *options) {
o.ctx = ctx
}
}

func ServePublic(r driver.Registry, cmd *cobra.Command, args []string, opts ...Option) error {
func ServePublic(r driver.Registry, cmd *cobra.Command, args []string, slOpts *servicelocatorx.Options, opts []Option) error {
modifiers := NewOptions(cmd.Context(), opts)
ctx := modifiers.ctx

c := r.Config()
l := r.Logger()
n := negroni.New()

for _, mw := range servicelocator.HTTPMiddlewares(ctx) {
n.Use(mw)
}

for _, mw := range modifiers.mwf {
for _, mw := range slOpts.HTTPMiddlewares() {
n.UseFunc(mw)
}

Expand Down Expand Up @@ -166,19 +155,15 @@ func ServePublic(r driver.Registry, cmd *cobra.Command, args []string, opts ...O
return nil
}

func ServeAdmin(r driver.Registry, cmd *cobra.Command, args []string, opts ...Option) error {
func ServeAdmin(r driver.Registry, cmd *cobra.Command, args []string, slOpts *servicelocatorx.Options, opts []Option) error {
modifiers := NewOptions(cmd.Context(), opts)
ctx := modifiers.ctx

c := r.Config()
l := r.Logger()
n := negroni.New()

for _, mw := range servicelocator.HTTPMiddlewares(ctx) {
n.Use(mw)
}

for _, mw := range modifiers.mwf {
for _, mw := range slOpts.HTTPMiddlewares() {
n.UseFunc(mw)
}

Expand Down Expand Up @@ -306,7 +291,7 @@ func sqa(ctx stdctx.Context, cmd *cobra.Command, d driver.Registry) *metricsx.Se
)
}

func bgTasks(d driver.Registry, cmd *cobra.Command, args []string, opts ...Option) error {
func bgTasks(d driver.Registry, cmd *cobra.Command, args []string, slOpts *servicelocatorx.Options, opts []Option) error {
modifiers := NewOptions(cmd.Context(), opts)
ctx := modifiers.ctx

Expand All @@ -316,7 +301,7 @@ func bgTasks(d driver.Registry, cmd *cobra.Command, args []string, opts ...Optio
return nil
}

func ServeAll(d driver.Registry, opts ...Option) func(cmd *cobra.Command, args []string) error {
func ServeAll(d driver.Registry, slOpts *servicelocatorx.Options, opts []Option) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
mods := NewOptions(cmd.Context(), opts)
ctx := mods.ctx
Expand All @@ -326,13 +311,13 @@ func ServeAll(d driver.Registry, opts ...Option) func(cmd *cobra.Command, args [
opts = append(opts, WithContext(ctx))

g.Go(func() error {
return ServePublic(d, cmd, args, opts...)
return ServePublic(d, cmd, args, slOpts, opts)
})
g.Go(func() error {
return ServeAdmin(d, cmd, args, opts...)
return ServeAdmin(d, cmd, args, slOpts, opts)
})
g.Go(func() error {
return bgTasks(d, cmd, args, opts...)
return bgTasks(d, cmd, args, slOpts, opts)
})
return g.Wait()
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewRootCmd() (cmd *cobra.Command) {
cmd = &cobra.Command{
Use: "kratos",
}
courier.RegisterCommandRecursive(cmd)
courier.RegisterCommandRecursive(cmd, nil, nil)
cmd.AddCommand(identities.NewGetCmd(cmd))
cmd.AddCommand(identities.NewDeleteCmd(cmd))
cmd.AddCommand(jsonnet.NewFormatCmd())
Expand All @@ -37,7 +37,7 @@ func NewRootCmd() (cmd *cobra.Command) {
cmd.AddCommand(jsonnet.NewLintCmd())
cmd.AddCommand(identities.NewListCmd(cmd))
migrate.RegisterCommandRecursive(cmd)
serve.RegisterCommandRecursive(cmd)
serve.RegisterCommandRecursive(cmd, nil, nil)
cleanup.RegisterCommandRecursive(cmd)
remote.RegisterCommandRecursive(cmd)
cmd.AddCommand(identities.NewValidateCmd())
Expand Down
13 changes: 7 additions & 6 deletions cmd/serve/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package serve
import (
"github.com/ory/kratos/driver/config"
"github.com/ory/x/configx"
"github.com/ory/x/servicelocatorx"

"github.com/spf13/cobra"

Expand All @@ -25,15 +26,15 @@ import (
)

// serveCmd represents the serve command
func NewServeCmd() (serveCmd *cobra.Command) {
func NewServeCmd(slOpts []servicelocatorx.Option, dOpts []driver.RegistryOption) (serveCmd *cobra.Command) {
serveCmd = &cobra.Command{
Use: "serve",
Short: "Run the Ory Kratos server",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
opts := configx.ConfigOptionsFromContext(ctx)

d, err := driver.New(ctx, cmd.ErrOrStderr(), append(opts, configx.WithFlags(cmd.Flags()))...)
sl := servicelocatorx.NewOptions(slOpts...)
d, err := driver.New(ctx, cmd.ErrOrStderr(), sl, dOpts, append(opts, configx.WithFlags(cmd.Flags())))
if err != nil {
return err
}
Expand All @@ -56,7 +57,7 @@ DON'T DO THIS IN PRODUCTION!
d.Logger().Warnf("Config version is '%s' but kratos runs on version '%s'", configVersion, config.Version)
}

return daemon.ServeAll(d)(cmd, args)
return daemon.ServeAll(d, sl, nil)(cmd, args)
},
}
configx.RegisterFlags(serveCmd.PersistentFlags())
Expand All @@ -67,6 +68,6 @@ DON'T DO THIS IN PRODUCTION!
return serveCmd
}

func RegisterCommandRecursive(parent *cobra.Command) {
parent.AddCommand(NewServeCmd())
func RegisterCommandRecursive(parent *cobra.Command, slOpts []servicelocatorx.Option, dOpts []driver.RegistryOption) {
parent.AddCommand(NewServeCmd(slOpts, dOpts))
}
19 changes: 10 additions & 9 deletions driver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@ import (
"context"
"io"

"github.com/ory/kratos/x/servicelocatorx"
"github.com/ory/x/contextx"
"github.com/ory/x/servicelocator"
"github.com/ory/x/servicelocatorx"

"github.com/ory/kratos/driver/config"
"github.com/ory/x/configx"
"github.com/ory/x/logrusx"
)

func New(ctx context.Context, stdOutOrErr io.Writer, opts ...configx.OptionModifier) (Registry, error) {
r, err := NewWithoutInit(ctx, stdOutOrErr, opts...)
func New(ctx context.Context, stdOutOrErr io.Writer, sl *servicelocatorx.Options, dOpts []RegistryOption, opts []configx.OptionModifier) (Registry, error) {
r, err := NewWithoutInit(ctx, stdOutOrErr, sl, dOpts, opts)
if err != nil {
return nil, err
}

ctxter := servicelocator.Contextualizer(ctx, &contextx.Default{})
ctxter := sl.Contextualizer()
if err := r.Init(ctx, ctxter); err != nil {
r.Logger().WithError(err).Error("Unable to initialize service registry.")
return nil, err
Expand All @@ -28,10 +26,13 @@ func New(ctx context.Context, stdOutOrErr io.Writer, opts ...configx.OptionModif
return r, nil
}

func NewWithoutInit(ctx context.Context, stdOutOrErr io.Writer, opts ...configx.OptionModifier) (Registry, error) {
l := logrusx.New("Ory Kratos", config.Version)
func NewWithoutInit(ctx context.Context, stdOutOrErr io.Writer, sl *servicelocatorx.Options, dOpts []RegistryOption, opts []configx.OptionModifier) (Registry, error) {
l := sl.Logger()
if l == nil {
l = logrusx.New("Ory Kratos", config.Version)
}

c := servicelocatorx.ConfigFromContext(ctx, nil)
c := newOptions(dOpts).config
if c == nil {
var err error
c, err = config.New(ctx, l, stdOutOrErr, opts...)
Expand Down
10 changes: 8 additions & 2 deletions driver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"testing"

"github.com/ory/x/servicelocatorx"

"github.com/gofrs/uuid"

"github.com/ory/x/configx"
Expand All @@ -21,8 +23,12 @@ func TestDriverNew(t *testing.T) {
r, err := driver.New(
context.Background(),
os.Stderr,
configx.WithValue(config.ViperKeyDSN, config.DefaultSQLiteMemoryDSN),
configx.SkipValidation())
servicelocatorx.NewOptions(),
nil,
[]configx.OptionModifier{
configx.WithValue(config.ViperKeyDSN, config.DefaultSQLiteMemoryDSN),
configx.SkipValidation(),
})
require.NoError(t, err)

assert.EqualValues(t, config.DefaultSQLiteMemoryDSN, r.Config().DSN(ctx))
Expand Down
7 changes: 7 additions & 0 deletions driver/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func NewRegistryFromDSN(ctx context.Context, c *config.Config, l *logrusx.Logger

type options struct {
skipNetworkInit bool
config *config.Config
}

type RegistryOption func(*options)
Expand All @@ -162,6 +163,12 @@ func SkipNetworkInit(o *options) {
o.skipNetworkInit = true
}

func WithConfig(config *config.Config) func(o *options) {
return func(o *options) {
o.config = config
}
}

func newOptions(os []RegistryOption) *options {
o := new(options)
for _, f := range os {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ require (
github.com/ory/kratos-client-go v0.6.3-alpha.1
github.com/ory/mail/v3 v3.0.0
github.com/ory/nosurf v1.2.7
github.com/ory/x v0.0.453
github.com/ory/x v0.0.454
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2
github.com/pkg/errors v0.9.1
github.com/pquerna/otp v1.3.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1488,8 +1488,8 @@ github.com/ory/sessions v1.2.2-0.20220110165800-b09c17334dc2 h1:zm6sDvHy/U9XrGpi
github.com/ory/sessions v1.2.2-0.20220110165800-b09c17334dc2/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/ory/viper v1.7.5 h1:+xVdq7SU3e1vNaCsk/ixsfxE4zylk1TJUiJrY647jUE=
github.com/ory/viper v1.7.5/go.mod h1:ypOuyJmEUb3oENywQZRgeAMwqgOyDqwboO1tj3DjTaM=
github.com/ory/x v0.0.453 h1:57/UpP55cON7w+L/qloIn1kKKqyjWbBK6KaCvT4LqTA=
github.com/ory/x v0.0.453/go.mod h1:i3TlzVVChaun6sfVscSqGyPr7IuzC3C0aSgS+ODSbNQ=
github.com/ory/x v0.0.454 h1:hDKNrFFMBkBrSHlTY1w+IhZ9CnxMpMz8RRaiaRtxQgA=
github.com/ory/x v0.0.454/go.mod h1:i3TlzVVChaun6sfVscSqGyPr7IuzC3C0aSgS+ODSbNQ=
github.com/otiai10/copy v1.2.0/go.mod h1:rrF5dJ5F0t/EWSYODDu4j9/vEeYHMkc8jt0zJChqQWw=
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs=
Expand Down
Loading

0 comments on commit 4f841da

Please sign in to comment.