From 0810ec73a046fcdead0f7d11081f68379c451464 Mon Sep 17 00:00:00 2001 From: Jon Lundy Date: Mon, 9 Jan 2023 12:32:45 -0700 Subject: [PATCH] chore: fixes to http mux --- .gitignore | 1 + Makefile | 2 +- app/salty/service.go | 2 +- cmd/ev/svc.es.go | 3 +- cmd/ev/svc.gql.go | 9 +++-- cmd/ev/svc.http.go | 5 +++ pkg/math/math_test.go | 6 ++-- pkg/mux/httpmux.go | 13 ++++--- pkg/mux/httpmux_test.go | 79 ++++++++++++++++++++++++++++++++++++----- 9 files changed, 93 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index d1bd010..c0a9cb6 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ data/ local.mk logzio.yml tmp/ +/build /ev diff --git a/Makefile b/Makefile index ce50a8c..63536d8 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ endif air ./cmd/ev run: - go run ./cmd/ev + go build ./cmd/ev && ./ev test: go test -cover -race ./... diff --git a/app/salty/service.go b/app/salty/service.go index da35979..da9dd87 100644 --- a/app/salty/service.go +++ b/app/salty/service.go @@ -136,7 +136,7 @@ func (s *service) ServeHTTP(w http.ResponseWriter, r *http.Request) { start := time.Now() defer s.m_req_time.Record(ctx, time.Since(start).Milliseconds()) - addr := "saltyuser-" + strings.TrimPrefix(r.URL.Path, "/.well-known/salty/") + addr := "saltyuser-" + strings.TrimPrefix(r.URL.Path, "/salty/") addr = strings.TrimSuffix(addr, ".json") span.AddEvent(fmt.Sprint("find ", addr)) diff --git a/cmd/ev/svc.es.go b/cmd/ev/svc.es.go index 7afe868..00cec22 100644 --- a/cmd/ev/svc.es.go +++ b/cmd/ev/svc.es.go @@ -47,8 +47,7 @@ var _ = apps.Register(10, func(ctx context.Context, svc *service.Harness) error span.RecordError(err) return err } - svc.Add(eventstore) - svc.Add(&es.EventStore{EventStore: eventstore}) + svc.Add(eventstore, &es.EventStore{EventStore: eventstore}) return nil }) diff --git a/cmd/ev/svc.gql.go b/cmd/ev/svc.gql.go index 51f11b2..ad3f025 100644 --- a/cmd/ev/svc.gql.go +++ b/cmd/ev/svc.gql.go @@ -2,12 +2,10 @@ package main import ( "context" - "net/http" "github.com/sour-is/ev/app/gql" "github.com/sour-is/ev/internal/lg" "github.com/sour-is/ev/pkg/gql/resolver" - "github.com/sour-is/ev/pkg/mux" "github.com/sour-is/ev/pkg/service" "github.com/sour-is/ev/pkg/slice" ) @@ -22,9 +20,10 @@ var _ = apps.Register(90, func(ctx context.Context, svc *service.Harness) error span.RecordError(err) return err } - svc.Add(gql, mux.RegisterHTTP(func(mux *http.ServeMux) { - mux.Handle("/", http.RedirectHandler("/playground", http.StatusTemporaryRedirect)) - })) + svc.Add(gql) + // svc.Add(mux.RegisterHTTP(func(mux *http.ServeMux) { + // mux.Handle("/", http.RedirectHandler("/playground", http.StatusTemporaryRedirect)) + // })) return nil }) diff --git a/cmd/ev/svc.http.go b/cmd/ev/svc.http.go index 72af2c9..b350f99 100644 --- a/cmd/ev/svc.http.go +++ b/cmd/ev/svc.http.go @@ -21,6 +21,11 @@ var _ = apps.Register(20, func(ctx context.Context, svc *service.Harness) error mux := mux.New() s.Handler = cors.AllowAll().Handler(mux) + // s.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // log.Println(r.URL.Path) + // mux.ServeHTTP(w, r) + // }) + s.Addr = env.Default("EV_HTTP", ":8080") if strings.HasPrefix(s.Addr, ":") { s.Addr = "[::]" + s.Addr diff --git a/pkg/math/math_test.go b/pkg/math/math_test.go index 106951c..04b179a 100644 --- a/pkg/math/math_test.go +++ b/pkg/math/math_test.go @@ -4,7 +4,7 @@ import ( "testing" "github.com/matryer/is" - "github.com/sour-is/ev/pkg/es" + "github.com/sour-is/ev" "github.com/sour-is/ev/pkg/math" ) @@ -74,8 +74,8 @@ func TestPagerBox(t *testing.T) { {1, 10, -2, -10, 9, -9}, {1, 10, 0, -10, 0, 0}, {1, 10, 10, 10, 0, 0}, - {1, 10, 0, es.AllEvents, 1, 10}, - {1, 10, -1, -es.AllEvents, 10, -10}, + {1, 10, 0, ev.AllEvents, 1, 10}, + {1, 10, -1, -ev.AllEvents, 10, -10}, {5, 10, 0, 1, 5, 1}, } diff --git a/pkg/mux/httpmux.go b/pkg/mux/httpmux.go index c2a2dea..4c28934 100644 --- a/pkg/mux/httpmux.go +++ b/pkg/mux/httpmux.go @@ -1,6 +1,7 @@ package mux import ( + "log" "net/http" ) @@ -12,16 +13,16 @@ type mux struct { func (mux *mux) Add(fns ...interface{ RegisterHTTP(*http.ServeMux) }) { for _, fn := range fns { - // log.Printf("HTTP: %T", fn) + log.Printf("HTTP: %T", fn) fn.RegisterHTTP(mux.ServeMux) if fn, ok := fn.(interface{ RegisterAPIv1(*http.ServeMux) }); ok { - // log.Printf("APIv1: %T", fn) + log.Printf("APIv1: %T", fn) fn.RegisterAPIv1(mux.api) } if fn, ok := fn.(interface{ RegisterWellKnown(*http.ServeMux) }); ok { - // log.Printf("APIv1: %T", fn) + log.Printf("WellKnown: %T", fn) fn.RegisterWellKnown(mux.wellknown) } } @@ -33,13 +34,11 @@ func New() *mux { ServeMux: http.NewServeMux(), } mux.Handle("/api/v1/", http.StripPrefix("/api/v1", mux.api)) - mux.Handle("/.well-known/", http.StripPrefix("/.well-known/", 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) -} +func (fn RegisterHTTP) RegisterHTTP(mux *http.ServeMux) { fn(mux) } diff --git a/pkg/mux/httpmux_test.go b/pkg/mux/httpmux_test.go index 0ffa87b..c9b4dc5 100644 --- a/pkg/mux/httpmux_test.go +++ b/pkg/mux/httpmux_test.go @@ -10,26 +10,62 @@ import ( ) type mockHTTP struct { - onServeHTTP func() + onServeHTTP func() + onServeAPIv1 func() + onServeWellKnown func() } -func (m *mockHTTP) ServeHTTP(w http.ResponseWriter, r *http.Request) { - m.onServeHTTP() +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.Handle("/", h) + mux.HandleFunc("/", h.ServeFn(h.onServeHTTP)) } func (h *mockHTTP) RegisterAPIv1(mux *http.ServeMux) { - mux.Handle("/ping", h) + mux.HandleFunc("/ping", h.ServeFn(h.onServeAPIv1)) +} +func (h *mockHTTP) RegisterWellKnown(mux *http.ServeMux) { + mux.HandleFunc("/echo", h.ServeFn(h.onServeWellKnown)) } -func TestHttpMux(t *testing.T) { +func TestHttp(t *testing.T) { is := is.New(t) called := false + calledAPIv1 := false + calledWellKnown := false mux := mux.New() - mux.Add(&mockHTTP{func() { called = true }}) + 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) @@ -37,5 +73,32 @@ func TestHttpMux(t *testing.T) { r := httptest.NewRequest(http.MethodGet, "/api/v1/ping", nil) mux.ServeHTTP(w, r) - is.True(called) + 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) }