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
|
local.mk
|
||||||
logzio.yml
|
logzio.yml
|
||||||
tmp/
|
tmp/
|
||||||
|
/build
|
||||||
/ev
|
/ev
|
||||||
|
|
2
Makefile
2
Makefile
|
@ -12,7 +12,7 @@ endif
|
||||||
air ./cmd/ev
|
air ./cmd/ev
|
||||||
|
|
||||||
run:
|
run:
|
||||||
go run ./cmd/ev
|
go build ./cmd/ev && ./ev
|
||||||
|
|
||||||
test:
|
test:
|
||||||
go test -cover -race ./...
|
go test -cover -race ./...
|
||||||
|
|
|
@ -136,7 +136,7 @@ func (s *service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
defer s.m_req_time.Record(ctx, time.Since(start).Milliseconds())
|
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")
|
addr = strings.TrimSuffix(addr, ".json")
|
||||||
|
|
||||||
span.AddEvent(fmt.Sprint("find ", addr))
|
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)
|
span.RecordError(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
svc.Add(eventstore)
|
svc.Add(eventstore, &es.EventStore{EventStore: eventstore})
|
||||||
svc.Add(&es.EventStore{EventStore: eventstore})
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
|
@ -2,12 +2,10 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/sour-is/ev/app/gql"
|
"github.com/sour-is/ev/app/gql"
|
||||||
"github.com/sour-is/ev/internal/lg"
|
"github.com/sour-is/ev/internal/lg"
|
||||||
"github.com/sour-is/ev/pkg/gql/resolver"
|
"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/service"
|
||||||
"github.com/sour-is/ev/pkg/slice"
|
"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)
|
span.RecordError(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
svc.Add(gql, mux.RegisterHTTP(func(mux *http.ServeMux) {
|
svc.Add(gql)
|
||||||
mux.Handle("/", http.RedirectHandler("/playground", http.StatusTemporaryRedirect))
|
// svc.Add(mux.RegisterHTTP(func(mux *http.ServeMux) {
|
||||||
}))
|
// mux.Handle("/", http.RedirectHandler("/playground", http.StatusTemporaryRedirect))
|
||||||
|
// }))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
|
@ -21,6 +21,11 @@ var _ = apps.Register(20, func(ctx context.Context, svc *service.Harness) error
|
||||||
mux := mux.New()
|
mux := mux.New()
|
||||||
s.Handler = cors.AllowAll().Handler(mux)
|
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")
|
s.Addr = env.Default("EV_HTTP", ":8080")
|
||||||
if strings.HasPrefix(s.Addr, ":") {
|
if strings.HasPrefix(s.Addr, ":") {
|
||||||
s.Addr = "[::]" + s.Addr
|
s.Addr = "[::]" + s.Addr
|
||||||
|
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/matryer/is"
|
"github.com/matryer/is"
|
||||||
"github.com/sour-is/ev/pkg/es"
|
"github.com/sour-is/ev"
|
||||||
"github.com/sour-is/ev/pkg/math"
|
"github.com/sour-is/ev/pkg/math"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -74,8 +74,8 @@ func TestPagerBox(t *testing.T) {
|
||||||
{1, 10, -2, -10, 9, -9},
|
{1, 10, -2, -10, 9, -9},
|
||||||
{1, 10, 0, -10, 0, 0},
|
{1, 10, 0, -10, 0, 0},
|
||||||
{1, 10, 10, 10, 0, 0},
|
{1, 10, 10, 10, 0, 0},
|
||||||
{1, 10, 0, es.AllEvents, 1, 10},
|
{1, 10, 0, ev.AllEvents, 1, 10},
|
||||||
{1, 10, -1, -es.AllEvents, 10, -10},
|
{1, 10, -1, -ev.AllEvents, 10, -10},
|
||||||
|
|
||||||
{5, 10, 0, 1, 5, 1},
|
{5, 10, 0, 1, 5, 1},
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package mux
|
package mux
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -12,16 +13,16 @@ type mux struct {
|
||||||
|
|
||||||
func (mux *mux) Add(fns ...interface{ RegisterHTTP(*http.ServeMux) }) {
|
func (mux *mux) Add(fns ...interface{ RegisterHTTP(*http.ServeMux) }) {
|
||||||
for _, fn := range fns {
|
for _, fn := range fns {
|
||||||
// log.Printf("HTTP: %T", fn)
|
log.Printf("HTTP: %T", fn)
|
||||||
fn.RegisterHTTP(mux.ServeMux)
|
fn.RegisterHTTP(mux.ServeMux)
|
||||||
|
|
||||||
if fn, ok := fn.(interface{ RegisterAPIv1(*http.ServeMux) }); ok {
|
if fn, ok := fn.(interface{ RegisterAPIv1(*http.ServeMux) }); ok {
|
||||||
// log.Printf("APIv1: %T", fn)
|
log.Printf("APIv1: %T", fn)
|
||||||
fn.RegisterAPIv1(mux.api)
|
fn.RegisterAPIv1(mux.api)
|
||||||
}
|
}
|
||||||
|
|
||||||
if fn, ok := fn.(interface{ RegisterWellKnown(*http.ServeMux) }); ok {
|
if fn, ok := fn.(interface{ RegisterWellKnown(*http.ServeMux) }); ok {
|
||||||
// log.Printf("APIv1: %T", fn)
|
log.Printf("WellKnown: %T", fn)
|
||||||
fn.RegisterWellKnown(mux.wellknown)
|
fn.RegisterWellKnown(mux.wellknown)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,13 +34,11 @@ func New() *mux {
|
||||||
ServeMux: http.NewServeMux(),
|
ServeMux: http.NewServeMux(),
|
||||||
}
|
}
|
||||||
mux.Handle("/api/v1/", http.StripPrefix("/api/v1", mux.api))
|
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
|
return mux
|
||||||
}
|
}
|
||||||
|
|
||||||
type RegisterHTTP func(*http.ServeMux)
|
type RegisterHTTP func(*http.ServeMux)
|
||||||
|
|
||||||
func (fn RegisterHTTP) RegisterHTTP(mux *http.ServeMux) {
|
func (fn RegisterHTTP) RegisterHTTP(mux *http.ServeMux) { fn(mux) }
|
||||||
fn(mux)
|
|
||||||
}
|
|
||||||
|
|
|
@ -11,25 +11,61 @@ import (
|
||||||
|
|
||||||
type mockHTTP struct {
|
type mockHTTP struct {
|
||||||
onServeHTTP func()
|
onServeHTTP func()
|
||||||
|
onServeAPIv1 func()
|
||||||
|
onServeWellKnown func()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockHTTP) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (*mockHTTP) ServeFn(fn func()) func(w http.ResponseWriter, r *http.Request) {
|
||||||
m.onServeHTTP()
|
return func(w http.ResponseWriter, r *http.Request) { fn() }
|
||||||
}
|
}
|
||||||
func (h *mockHTTP) RegisterHTTP(mux *http.ServeMux) {
|
func (h *mockHTTP) RegisterHTTP(mux *http.ServeMux) {
|
||||||
mux.Handle("/", h)
|
mux.HandleFunc("/", h.ServeFn(h.onServeHTTP))
|
||||||
}
|
}
|
||||||
func (h *mockHTTP) RegisterAPIv1(mux *http.ServeMux) {
|
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)
|
is := is.New(t)
|
||||||
|
|
||||||
called := false
|
called := false
|
||||||
|
calledAPIv1 := false
|
||||||
|
calledWellKnown := false
|
||||||
|
|
||||||
mux := mux.New()
|
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)
|
is.True(mux != nil)
|
||||||
|
|
||||||
|
@ -37,5 +73,32 @@ func TestHttpMux(t *testing.T) {
|
||||||
r := httptest.NewRequest(http.MethodGet, "/api/v1/ping", nil)
|
r := httptest.NewRequest(http.MethodGet, "/api/v1/ping", nil)
|
||||||
mux.ServeHTTP(w, r)
|
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