initial commit

This commit is contained in:
xuu
2023-07-12 12:43:25 -06:00
commit 80dbf39736
34 changed files with 3664 additions and 0 deletions

43
mux/httpmux.go Normal file
View File

@@ -0,0 +1,43 @@
package mux
import (
"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 {
// log.Printf("HTTP: %T", fn)
fn.RegisterHTTP(mux.ServeMux)
if fn, ok := fn.(interface{ RegisterAPIv1(*http.ServeMux) }); ok {
// log.Printf("APIv1: %T", fn)
fn.RegisterAPIv1(mux.api)
}
if fn, ok := fn.(interface{ RegisterWellKnown(*http.ServeMux) }); ok {
// log.Printf("WellKnown: %T", fn)
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))
mux.Handle("/.well-known/", http.StripPrefix("/.well-known", mux.wellknown))
return mux
}
type RegisterHTTP func(*http.ServeMux)
func (fn RegisterHTTP) RegisterHTTP(mux *http.ServeMux) { fn(mux) }

104
mux/httpmux_test.go Normal file
View File

@@ -0,0 +1,104 @@
package mux_test
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/matryer/is"
"go.sour.is/pkg/mux"
)
type mockHTTP struct {
onServeHTTP func()
onServeAPIv1 func()
onServeWellKnown func()
}
func (*mockHTTP) ServeFn(fn func()) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) { fn() }
}
func (h *mockHTTP) RegisterHTTP(mux *http.ServeMux) {
mux.HandleFunc("/", h.ServeFn(h.onServeHTTP))
}
func (h *mockHTTP) RegisterAPIv1(mux *http.ServeMux) {
mux.HandleFunc("/ping", h.ServeFn(h.onServeAPIv1))
}
func (h *mockHTTP) RegisterWellKnown(mux *http.ServeMux) {
mux.HandleFunc("/echo", h.ServeFn(h.onServeWellKnown))
}
func TestHttp(t *testing.T) {
is := is.New(t)
called := false
calledAPIv1 := false
calledWellKnown := false
mux := mux.New()
mux.Add(&mockHTTP{
func() { called = true },
func() { calledAPIv1 = true },
func() { calledWellKnown = true },
})
is.True(mux != nil)
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/", nil)
mux.ServeHTTP(w, r)
is.True(called)
is.True(!calledAPIv1)
is.True(!calledWellKnown)
}
func TestHttpAPIv1(t *testing.T) {
is := is.New(t)
called := false
calledAPIv1 := false
calledWellKnown := false
mux := mux.New()
mux.Add(&mockHTTP{
func() { called = true },
func() { calledAPIv1 = true },
func() { calledWellKnown = true },
})
is.True(mux != nil)
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/api/v1/ping", nil)
mux.ServeHTTP(w, r)
is.True(!called)
is.True(calledAPIv1)
is.True(!calledWellKnown)
}
func TestHttpWellKnown(t *testing.T) {
is := is.New(t)
called := false
calledAPIv1 := false
calledWellKnown := false
mux := mux.New()
mux.Add(&mockHTTP{
func() { called = true },
func() { calledAPIv1 = true },
func() { calledWellKnown = true },
})
is.True(mux != nil)
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/.well-known/echo", nil)
mux.ServeHTTP(w, r)
is.True(!called)
is.True(!calledAPIv1)
is.True(calledWellKnown)
}