Files
nebula/mount.go

41 lines
891 B
Go
Raw Permalink Normal View History

package nebula
import (
"net/http"
)
// Handler creates a new http.Handler with nebula middleware applied.
// The handler parameter is wrapped with HTMX context and config middleware.
func Handler(handler http.Handler, opts ...Option) http.Handler {
cfg := DefaultConfig()
for _, opt := range opts {
opt(&cfg)
}
var h http.Handler = handler
h = Middleware(opts...)(h)
if cfg.SSE.Enabled {
h = SSEMiddleware()(h)
}
return h
}
// Mount registers a handler at the configured base path on the provided mux.
// Use this to mount your application routes with nebula middleware.
func Mount(mux *http.ServeMux, handler http.Handler, opts ...Option) {
cfg := DefaultConfig()
for _, opt := range opts {
opt(&cfg)
}
h := Handler(handler, opts...)
if cfg.BasePath == "/" {
mux.Handle("/", h)
} else {
mux.Handle(cfg.BasePath+"/", http.StripPrefix(cfg.BasePath, h))
}
}