ev/httpmux.go

41 lines
706 B
Go
Raw Normal View History

package main
import (
"net/http"
"github.com/rs/cors"
)
type mux struct {
*http.ServeMux
api *http.ServeMux
}
func httpMux(fns ...interface{ RegisterHTTP(*http.ServeMux) }) http.Handler {
mux := newMux()
for _, fn := range fns {
fn.RegisterHTTP(mux.ServeMux)
if fn, ok := fn.(interface{ RegisterAPIv1(*http.ServeMux) }); ok {
fn.RegisterAPIv1(mux.api)
}
}
return cors.AllowAll().Handler(mux)
}
func newMux() *mux {
mux := &mux{
api: http.NewServeMux(),
ServeMux: http.NewServeMux(),
}
2022-09-07 16:00:10 -06:00
mux.Handle("/api/v1/", http.StripPrefix("/api/v1", mux.api))
return mux
}
2022-10-30 09:18:08 -06:00
type RegisterHTTP func(*http.ServeMux)
2022-11-20 10:26:20 -07:00
2022-10-30 09:18:08 -06:00
func (fn RegisterHTTP) RegisterHTTP(mux *http.ServeMux) {
fn(mux)
2022-11-20 10:26:20 -07:00
}