2023-01-09 11:30:02 -07:00
|
|
|
package mux
|
|
|
|
|
|
|
|
import (
|
2023-01-09 12:32:45 -07:00
|
|
|
"log"
|
2023-01-09 11:30:02 -07:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mux struct {
|
|
|
|
*http.ServeMux
|
|
|
|
api *http.ServeMux
|
|
|
|
wellknown *http.ServeMux
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mux *mux) Add(fns ...interface{ RegisterHTTP(*http.ServeMux) }) {
|
|
|
|
for _, fn := range fns {
|
2023-01-09 12:32:45 -07:00
|
|
|
log.Printf("HTTP: %T", fn)
|
2023-01-09 11:30:02 -07:00
|
|
|
fn.RegisterHTTP(mux.ServeMux)
|
|
|
|
|
|
|
|
if fn, ok := fn.(interface{ RegisterAPIv1(*http.ServeMux) }); ok {
|
2023-01-09 12:32:45 -07:00
|
|
|
log.Printf("APIv1: %T", fn)
|
2023-01-09 11:30:02 -07:00
|
|
|
fn.RegisterAPIv1(mux.api)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fn, ok := fn.(interface{ RegisterWellKnown(*http.ServeMux) }); ok {
|
2023-01-09 12:32:45 -07:00
|
|
|
log.Printf("WellKnown: %T", fn)
|
2023-01-09 11:30:02 -07:00
|
|
|
fn.RegisterWellKnown(mux.wellknown)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func New() *mux {
|
|
|
|
mux := &mux{
|
|
|
|
api: http.NewServeMux(),
|
|
|
|
wellknown: http.NewServeMux(),
|
|
|
|
ServeMux: http.NewServeMux(),
|
|
|
|
}
|
|
|
|
mux.Handle("/api/v1/", http.StripPrefix("/api/v1", mux.api))
|
2023-01-09 12:32:45 -07:00
|
|
|
mux.Handle("/.well-known/", http.StripPrefix("/.well-known", mux.wellknown))
|
2023-01-09 11:30:02 -07:00
|
|
|
|
|
|
|
return mux
|
|
|
|
}
|
|
|
|
|
|
|
|
type RegisterHTTP func(*http.ServeMux)
|
|
|
|
|
2023-01-09 12:32:45 -07:00
|
|
|
func (fn RegisterHTTP) RegisterHTTP(mux *http.ServeMux) { fn(mux) }
|