Skip to content

Commit

Permalink
feat: export and add config options
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Jan 11, 2021
1 parent 7ebc3a9 commit 4391fe5
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions cmd/daemon/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

"github.com/ory/analytics-go/v4"

"github.com/ory/x/flagx"
"github.com/ory/x/healthx"

"github.com/gorilla/context"
Expand All @@ -42,12 +41,36 @@ import (
"github.com/ory/kratos/x"
)

func servePublic(r driver.Registry, wg *sync.WaitGroup, cmd *cobra.Command, args []string) {
type options struct {
mwf []func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)
}

func newOptions(opts []Option) *options {
o := new(options)
for _, f := range opts {
f(o)
}
return o
}

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 ServePublic(r driver.Registry, wg *sync.WaitGroup, cmd *cobra.Command, args []string, opts ...Option) {
defer wg.Done()
modifiers := newOptions(opts)

c := r.Configuration(cmd.Context())
l := r.Logger()
n := negroni.New()
for _, mw := range modifiers.mwf {
n.UseFunc(mw)
}

router := x.NewRouterPublic()
csrf := x.NewCSRFHandler(
Expand All @@ -56,7 +79,7 @@ func servePublic(r driver.Registry, wg *sync.WaitGroup, cmd *cobra.Command, args
l,
stringsx.Coalesce(c.SelfPublicURL().Path, "/"),
c.SelfPublicURL().Hostname(),
!flagx.MustGetBool(cmd, "dev"),
!c.IsInsecureDevMode(),
)

n.UseFunc(x.CleanPath) // Prevent double slashes from breaking CSRF.
Expand Down Expand Up @@ -89,12 +112,16 @@ func servePublic(r driver.Registry, wg *sync.WaitGroup, cmd *cobra.Command, args
l.Println("Public httpd was shutdown gracefully")
}

func serveAdmin(r driver.Registry, wg *sync.WaitGroup, cmd *cobra.Command, args []string) {
func ServeAdmin(r driver.Registry, wg *sync.WaitGroup, cmd *cobra.Command, args []string, opts ...Option) {
defer wg.Done()
modifiers := newOptions(opts)

c := r.Configuration(cmd.Context())
l := r.Logger()
n := negroni.New()
for _, mw := range modifiers.mwf {
n.UseFunc(mw)
}

router := x.NewRouterAdmin()
r.RegisterAdminRoutes(router)
Expand Down Expand Up @@ -135,7 +162,7 @@ func sqa(cmd *cobra.Command, d driver.Registry) *metricsx.Service {
d.Configuration(cmd.Context()).SelfAdminURL().String(),
}, "|"),
),
IsDevelopment: flagx.MustGetBool(cmd, "dev"),
IsDevelopment: d.Configuration(cmd.Context()).IsInsecureDevMode(),
WriteKey: "qQlI6q8Q4WvkzTjKQSor4sHYOikHIvvi",
WhitelistedPaths: []string{
"/",
Expand Down Expand Up @@ -207,12 +234,12 @@ func bgTasks(d driver.Registry, wg *sync.WaitGroup, cmd *cobra.Command, args []s
d.Logger().Println("Courier worker was shutdown gracefully.")
}

func ServeAll(d driver.Registry) func(cmd *cobra.Command, args []string) {
func ServeAll(d driver.Registry, opts ...Option) func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
var wg sync.WaitGroup
wg.Add(3)
go servePublic(d, &wg, cmd, args)
go serveAdmin(d, &wg, cmd, args)
go ServePublic(d, &wg, cmd, args, opts...)
go ServeAdmin(d, &wg, cmd, args, opts...)
go bgTasks(d, &wg, cmd, args)
wg.Wait()
}
Expand Down

0 comments on commit 4391fe5

Please sign in to comment.