chore: fixes to http mux
This commit is contained in:
parent
4fc9c78dae
commit
0810ec73a0
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -5,4 +5,5 @@ data/
|
|||
local.mk
|
||||
logzio.yml
|
||||
tmp/
|
||||
/build
|
||||
/ev
|
||||
|
|
2
Makefile
2
Makefile
|
@ -12,7 +12,7 @@ endif
|
|||
air ./cmd/ev
|
||||
|
||||
run:
|
||||
go run ./cmd/ev
|
||||
go build ./cmd/ev && ./ev
|
||||
|
||||
test:
|
||||
go test -cover -race ./...
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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
|
||||
})
|
||||
|
|
|
@ -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
|
||||
})
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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},
|
||||
}
|
||||
|
|
|
@ -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) }
|
||||
|
|
|
@ -11,25 +11,61 @@ import (
|
|||
|
||||
type mockHTTP struct {
|
||||
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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user