42 lines
721 B
Go

package webui
import (
"net/http"
"strings"
)
type Handler struct {
mux *http.ServeMux
}
// ServeHTTP implements http.Handler.
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.mux.ServeHTTP(w, r)
}
func NewHandler(funcs ...OptionFunc) *Handler {
opts := NewOptions(funcs...)
h := &Handler{
mux: http.NewServeMux(),
}
for mountpoint, handler := range opts.Mounts {
h.mount(mountpoint, handler)
}
return h
}
func (h *Handler) mount(prefix string, handler http.Handler) {
trimmed := strings.TrimSuffix(prefix, "/")
if len(trimmed) > 0 {
h.mux.Handle(prefix, http.StripPrefix(trimmed, handler))
} else {
h.mux.Handle(prefix, handler)
}
}
var _ http.Handler = &Handler{}