feat(http): allow passing middlewares via options
All checks were successful
arcad/edge/pipeline/head This commit looks good
All checks were successful
arcad/edge/pipeline/head This commit looks good
This commit is contained in:
parent
7e58551f6a
commit
c3535a4a9b
@ -216,15 +216,17 @@ func runApp(ctx context.Context, path string, address string, storageFile string
|
||||
authModule.WithJWT(dummyKeySet),
|
||||
),
|
||||
),
|
||||
appHTTP.WithHTTPMiddlewares(
|
||||
authModuleMiddleware.AnonymousUser(
|
||||
jwa.HS256, key,
|
||||
),
|
||||
),
|
||||
)
|
||||
if err := handler.Load(bundle); err != nil {
|
||||
return errors.Wrap(err, "could not load app bundle")
|
||||
}
|
||||
|
||||
router := chi.NewRouter()
|
||||
router.Use(authModuleMiddleware.AnonymousUser(
|
||||
jwa.HS256, key,
|
||||
))
|
||||
router.Use(middleware.Logger)
|
||||
router.Use(middleware.Compress(5))
|
||||
|
||||
|
@ -97,6 +97,10 @@ func NewHandler(funcs ...HandlerOptionFunc) *Handler {
|
||||
bus: opts.Bus,
|
||||
}
|
||||
|
||||
for _, middleware := range opts.HTTPMiddlewares {
|
||||
router.Use(middleware)
|
||||
}
|
||||
|
||||
router.Route("/edge", func(r chi.Router) {
|
||||
r.Route("/sdk", func(r chi.Router) {
|
||||
r.Get("/client.js", handler.handleSDKClient)
|
||||
|
@ -18,6 +18,7 @@ type HandlerOptions struct {
|
||||
UploadMaxFileSize int64
|
||||
HTTPClient *http.Client
|
||||
HTTPMounts []func(r chi.Router)
|
||||
HTTPMiddlewares []func(next http.Handler) http.Handler
|
||||
}
|
||||
|
||||
func defaultHandlerOptions() *HandlerOptions {
|
||||
@ -34,7 +35,8 @@ func defaultHandlerOptions() *HandlerOptions {
|
||||
HTTPClient: &http.Client{
|
||||
Timeout: time.Second * 30,
|
||||
},
|
||||
HTTPMounts: make([]func(r chi.Router), 0),
|
||||
HTTPMounts: make([]func(r chi.Router), 0),
|
||||
HTTPMiddlewares: make([]func(http.Handler) http.Handler, 0),
|
||||
}
|
||||
}
|
||||
|
||||
@ -75,3 +77,9 @@ func WithHTTPMounts(mounts ...func(r chi.Router)) HandlerOptionFunc {
|
||||
opts.HTTPMounts = mounts
|
||||
}
|
||||
}
|
||||
|
||||
func WithHTTPMiddlewares(middlewares ...func(http.Handler) http.Handler) HandlerOptionFunc {
|
||||
return func(opts *HandlerOptions) {
|
||||
opts.HTTPMiddlewares = middlewares
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user