Skip to content

Commit 1b8debe

Browse files
zepatrikory-bot
authored andcommitted
chore(kratos): use httprouter from ory/x
GitOrigin-RevId: c81d237585c5147079574e5e997d2a0bd8efde24
1 parent ecf73dc commit 1b8debe

91 files changed

Lines changed: 366 additions & 943 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cmd/daemon/serve.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ import (
3636
"github.com/ory/kratos/x"
3737
"github.com/ory/kratos/x/nosurfx"
3838
"github.com/ory/x/healthx"
39+
"github.com/ory/x/httprouterx"
3940
"github.com/ory/x/metricsx"
4041
"github.com/ory/x/networkx"
4142
"github.com/ory/x/otelx"
4243
"github.com/ory/x/otelx/semconv"
43-
prometheus "github.com/ory/x/prometheusx"
44+
"github.com/ory/x/prometheusx"
4445
"github.com/ory/x/reqlog"
4546
"github.com/ory/x/urlx"
4647
)
@@ -49,6 +50,8 @@ func init() {
4950
graceful.DefaultShutdownTimeout = 120 * time.Second
5051
}
5152

53+
var prometheusManager = prometheusx.NewMetricsManagerWithPrefix("kratos", prometheusx.HTTPMetrics, config.Version, config.Commit, config.Date)
54+
5255
func servePublic(ctx context.Context, r *driver.RegistryDefault, cmd *cobra.Command) (func() error, error) {
5356
cfg := r.Config().ServePublic(ctx)
5457
l := r.Logger()
@@ -67,9 +70,10 @@ func servePublic(ctx context.Context, r *driver.RegistryDefault, cmd *cobra.Comm
6770
n.UseFunc(semconv.Middleware)
6871
n.Use(publicLogger)
6972
n.Use(x.HTTPLoaderContextMiddleware(r))
73+
n.UseFunc(httprouterx.NoCacheNegroni)
7074
n.Use(sqa(ctx, cmd, r))
7175

72-
router := x.NewRouterPublic(r)
76+
router := httprouterx.NewRouterPublic(prometheusManager)
7377
csrf := nosurfx.NewCSRFHandler(otelx.SpanNameRecorderMiddleware(router), r)
7478

7579
// we need to always load the CORS middleware even if it is disabled, to allow hot-enabling CORS
@@ -90,7 +94,7 @@ func servePublic(ctx context.Context, r *driver.RegistryDefault, cmd *cobra.Comm
9094
csrf.DisablePath(healthx.AliveCheckPath)
9195
csrf.DisablePath(healthx.ReadyCheckPath)
9296
csrf.DisablePath(healthx.VersionPath)
93-
csrf.DisablePath(prometheus.MetricsPrometheusPath)
97+
csrf.DisablePath(prometheusx.MetricsPrometheusPath)
9498

9599
r.RegisterPublicRoutes(ctx, router)
96100

@@ -154,15 +158,20 @@ func serveAdmin(ctx context.Context, r *driver.RegistryDefault, cmd *cobra.Comma
154158
adminLogger := reqlog.NewMiddlewareFromLogger(l, "admin#"+cfg.BaseURL.String())
155159

156160
if cfg.RequestLog.DisableHealth {
157-
adminLogger.ExcludePaths(x.AdminPrefix+healthx.AliveCheckPath, x.AdminPrefix+healthx.ReadyCheckPath, x.AdminPrefix+prometheus.MetricsPrometheusPath)
161+
adminLogger.ExcludePaths(
162+
httprouterx.AdminPrefix+healthx.AliveCheckPath,
163+
httprouterx.AdminPrefix+healthx.ReadyCheckPath,
164+
httprouterx.AdminPrefix+prometheusx.MetricsPrometheusPath,
165+
)
158166
}
159167
n.UseFunc(semconv.Middleware)
160168
n.Use(adminLogger)
161-
n.UseFunc(x.RedirectAdminMiddleware)
169+
n.UseFunc(httprouterx.AddAdminPrefixIfNotPresentNegroni)
170+
n.UseFunc(httprouterx.NoCacheNegroni)
162171
n.Use(x.HTTPLoaderContextMiddleware(r))
163172
n.Use(sqa(ctx, cmd, r))
164173

165-
router := x.NewRouterAdmin(r)
174+
router := httprouterx.NewRouterAdminWithPrefix(prometheusManager)
166175
r.RegisterAdminRoutes(ctx, router)
167176

168177
n.UseHandler(router)
@@ -271,8 +280,8 @@ func sqa(ctx context.Context, cmd *cobra.Command, d driver.Registry) *metricsx.S
271280

272281
session.RouteWhoami,
273282

274-
x.AdminPrefix + "/" + schema.SchemasPath,
275-
x.AdminPrefix + identity.RouteCollection,
283+
httprouterx.AdminPrefix + "/" + schema.SchemasPath,
284+
httprouterx.AdminPrefix + identity.RouteCollection,
276285

277286
settings.RouteInitBrowserFlow,
278287
settings.RouteInitAPIFlow,
@@ -292,7 +301,7 @@ func sqa(ctx context.Context, cmd *cobra.Command, d driver.Registry) *metricsx.S
292301
link.RouteAdminCreateRecoveryLink,
293302

294303
errorx.RouteGet,
295-
prometheus.MetricsPrometheusPath,
304+
prometheusx.MetricsPrometheusPath,
296305
},
297306
BuildVersion: config.Version,
298307
BuildHash: config.Commit,

courier/handler.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,16 @@ package courier
66
import (
77
"net/http"
88

9-
"github.com/pkg/errors"
10-
11-
"github.com/ory/kratos/x/nosurfx"
12-
"github.com/ory/kratos/x/redir"
13-
149
"github.com/gofrs/uuid"
10+
"github.com/pkg/errors"
1511

1612
"github.com/ory/herodot"
17-
keysetpagination "github.com/ory/x/pagination/keysetpagination_v2"
18-
1913
"github.com/ory/kratos/driver/config"
2014
"github.com/ory/kratos/x"
15+
"github.com/ory/kratos/x/nosurfx"
16+
"github.com/ory/kratos/x/redir"
17+
"github.com/ory/x/httprouterx"
18+
keysetpagination "github.com/ory/x/pagination/keysetpagination_v2"
2119
)
2220

2321
const (
@@ -46,13 +44,13 @@ func NewHandler(r handlerDependencies) *Handler {
4644
return &Handler{r: r}
4745
}
4846

49-
func (h *Handler) RegisterPublicRoutes(public *x.RouterPublic) {
50-
h.r.CSRFHandler().IgnoreGlobs(x.AdminPrefix+AdminRouteListMessages, AdminRouteListMessages)
51-
public.GET(x.AdminPrefix+AdminRouteListMessages, redir.RedirectToAdminRoute(h.r))
52-
public.GET(x.AdminPrefix+AdminRouteGetMessage, redir.RedirectToAdminRoute(h.r))
47+
func (h *Handler) RegisterPublicRoutes(public *httprouterx.RouterPublic) {
48+
h.r.CSRFHandler().IgnoreGlobs(httprouterx.AdminPrefix+AdminRouteListMessages, AdminRouteListMessages)
49+
public.GET(httprouterx.AdminPrefix+AdminRouteListMessages, redir.RedirectToAdminRoute(h.r))
50+
public.GET(httprouterx.AdminPrefix+AdminRouteGetMessage, redir.RedirectToAdminRoute(h.r))
5351
}
5452

55-
func (h *Handler) RegisterAdminRoutes(admin *x.RouterAdmin) {
53+
func (h *Handler) RegisterAdminRoutes(admin *httprouterx.RouterAdmin) {
5654
admin.GET(AdminRouteListMessages, h.listCourierMessages)
5755
admin.GET(AdminRouteGetMessage, h.getCourierMessage)
5856
}

courier/handler_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,19 @@ import (
1414

1515
"github.com/go-faker/faker/v4"
1616
"github.com/gofrs/uuid"
17+
"github.com/stretchr/testify/assert"
18+
"github.com/stretchr/testify/require"
1719
"github.com/tidwall/gjson"
1820

1921
"github.com/ory/kratos/courier"
2022
"github.com/ory/kratos/driver/config"
2123
"github.com/ory/kratos/internal"
2224
"github.com/ory/kratos/internal/testhelpers"
23-
"github.com/ory/kratos/x"
25+
"github.com/ory/x/httprouterx"
2426
"github.com/ory/x/ioutilx"
2527
"github.com/ory/x/snapshotx"
2628
"github.com/ory/x/urlx"
2729
"github.com/ory/x/uuidx"
28-
29-
"github.com/stretchr/testify/assert"
30-
"github.com/stretchr/testify/require"
3130
)
3231

3332
var defaultPageToken = courier.Message{}.DefaultPageToken().Encrypt(nil)
@@ -74,7 +73,7 @@ func TestHandler(t *testing.T) {
7473
ts := adminTS
7574

7675
if tsName == "public" {
77-
href = x.AdminPrefix + href
76+
href = httprouterx.AdminPrefix + href
7877
ts = publicTS
7978
}
8079

@@ -130,7 +129,7 @@ func TestHandler(t *testing.T) {
130129
t.Run("endpoint="+tc.name, func(t *testing.T) {
131130
path := courier.AdminRouteListMessages + qs
132131
if tc.name == "public" {
133-
path = x.AdminPrefix + path
132+
path = httprouterx.AdminPrefix + path
134133
}
135134
resp, err := tc.s.Client().Get(tc.s.URL + path)
136135
require.NoError(t, err)

driver/config/handler.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ import (
99
"net/http"
1010

1111
"github.com/knadh/koanf/parsers/json"
12-
)
1312

14-
type router interface {
15-
HandlerFunc(method, path string, handler http.HandlerFunc)
16-
}
13+
"github.com/ory/x/httprouterx"
14+
)
1715

18-
func NewConfigHashHandler(c Provider, router router) {
19-
router.HandlerFunc("GET", "/health/config", func(w http.ResponseWriter, r *http.Request) {
16+
func RegisterConfigHashRoute(c Provider, router *httprouterx.RouterAdmin) {
17+
router.GET("/health/config", func(w http.ResponseWriter, r *http.Request) {
2018
w.Header().Set("Content-Type", "text/plain")
2119
if revision := c.Config().GetProvider(r.Context()).String("revision"); len(revision) > 0 {
2220
_, _ = fmt.Fprintf(w, "%s", revision)

driver/config/handler_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313

1414
"github.com/ory/kratos/driver/config"
1515
"github.com/ory/kratos/internal"
16-
"github.com/ory/kratos/x"
1716
"github.com/ory/x/contextx"
17+
"github.com/ory/x/httprouterx"
1818
)
1919

2020
type configProvider struct {
@@ -28,8 +28,8 @@ func (c *configProvider) Config() *config.Config {
2828
func TestNewConfigHashHandler(t *testing.T) {
2929
ctx := context.Background()
3030
cfg := internal.NewConfigurationWithDefaults(t)
31-
router := x.NewTestRouterPublic(t)
32-
config.NewConfigHashHandler(&configProvider{cfg: cfg}, router)
31+
router := httprouterx.NewTestRouterAdmin(t)
32+
config.RegisterConfigHashRoute(&configProvider{cfg: cfg}, router)
3333
ts := contextx.NewConfigurableTestServer(router)
3434
t.Cleanup(ts.Close)
3535

driver/registry.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ import (
77
"context"
88
"io/fs"
99

10-
"github.com/ory/pop/v6"
11-
"github.com/ory/x/configx"
12-
"github.com/ory/x/servicelocatorx"
13-
1410
"github.com/gorilla/sessions"
1511

1612
"github.com/ory/kratos/cipher"
@@ -36,13 +32,17 @@ import (
3632
"github.com/ory/kratos/x"
3733
"github.com/ory/kratos/x/nosurfx"
3834
"github.com/ory/nosurf"
35+
"github.com/ory/pop/v6"
36+
"github.com/ory/x/configx"
3937
"github.com/ory/x/contextx"
4038
"github.com/ory/x/healthx"
39+
"github.com/ory/x/httprouterx"
4140
"github.com/ory/x/jsonnetsecure"
4241
"github.com/ory/x/logrusx"
4342
"github.com/ory/x/otelx"
4443
"github.com/ory/x/popx"
4544
prometheus "github.com/ory/x/prometheusx"
45+
"github.com/ory/x/servicelocatorx"
4646
)
4747

4848
type Registry interface {
@@ -59,10 +59,8 @@ type Registry interface {
5959
CookieManager(ctx context.Context) sessions.StoreExact
6060
ContinuityCookieManager(ctx context.Context) sessions.StoreExact
6161

62-
RegisterRoutes(ctx context.Context, public *x.RouterPublic, admin *x.RouterAdmin)
63-
RegisterPublicRoutes(ctx context.Context, public *x.RouterPublic)
64-
RegisterAdminRoutes(ctx context.Context, admin *x.RouterAdmin)
65-
PrometheusManager() *prometheus.MetricsManager
62+
RegisterPublicRoutes(ctx context.Context, public *httprouterx.RouterPublic)
63+
RegisterAdminRoutes(ctx context.Context, admin *httprouterx.RouterAdmin)
6664
Tracer(context.Context) *otelx.Tracer
6765
SetTracer(*otelx.Tracer)
6866

@@ -181,7 +179,7 @@ type options struct {
181179
extraGoMigrations popx.Migrations
182180
replacementStrategies []NewStrategy
183181
extraHooks map[string]func(config.SelfServiceHook) any
184-
extraHandlers []NewHandlerRegistrar
182+
extraHandlers []NewHandler
185183
disableMigrationLogging bool
186184
jsonnetPool jsonnetsecure.Pool
187185
serviceLocatorOptions []servicelocatorx.Option
@@ -249,9 +247,9 @@ func WithExtraHooks(hooks map[string]func(config.SelfServiceHook) any) RegistryO
249247
}
250248
}
251249

252-
type NewHandlerRegistrar func(deps any) x.HandlerRegistrar
250+
type NewHandler func(deps any) x.Handler
253251

254-
func WithExtraHandlers(handlers ...NewHandlerRegistrar) RegistryOption {
252+
func WithExtraHandlers(handlers ...NewHandler) RegistryOption {
255253
return func(o *options) {
256254
o.extraHandlers = handlers
257255
}

0 commit comments

Comments
 (0)