feat: initial commit
This commit is contained in:
41
internal/http/handler/webui/handler.go
Normal file
41
internal/http/handler/webui/handler.go
Normal file
@ -0,0 +1,41 @@
|
||||
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{}
|
Reference in New Issue
Block a user