khatru: WithServiceURL() subhandlers.
This commit is contained in:
+2
-2
@@ -43,8 +43,8 @@ func (rl *Relay) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
relayPathMatches := true
|
relayPathMatches := true
|
||||||
if rl.ServiceURL != "" {
|
if serviceURL := rl.getServiceURL(r); serviceURL != "" {
|
||||||
p, err := url.Parse(rl.ServiceURL)
|
p, err := url.Parse(serviceURL)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
relayPathMatches = strings.TrimSuffix(r.URL.Path, "/") == strings.TrimSuffix(p.Path, "/")
|
relayPathMatches = strings.TrimSuffix(r.URL.Path, "/") == strings.TrimSuffix(p.Path, "/")
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-2
@@ -166,8 +166,8 @@ func (rl *Relay) UseEventstore(store eventstore.Store, maxQueryLimit int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rl *Relay) getBaseURL(r *http.Request) string {
|
func (rl *Relay) getBaseURL(r *http.Request) string {
|
||||||
if rl.ServiceURL != "" {
|
if serviceURL := rl.getServiceURL(r); serviceURL != "" {
|
||||||
return rl.ServiceURL
|
return serviceURL
|
||||||
}
|
}
|
||||||
|
|
||||||
host := r.Header.Get("X-Forwarded-Host")
|
host := r.Header.Get("X-Forwarded-Host")
|
||||||
@@ -192,6 +192,14 @@ func (rl *Relay) getBaseURL(r *http.Request) string {
|
|||||||
return proto + "://" + host + r.URL.Path
|
return proto + "://" + host + r.URL.Path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (rl *Relay) getServiceURL(r *http.Request) string {
|
||||||
|
if serviceURL, ok := r.Context().Value(serviceURLOverrideKey).(string); ok {
|
||||||
|
return serviceURL
|
||||||
|
}
|
||||||
|
|
||||||
|
return rl.ServiceURL
|
||||||
|
}
|
||||||
|
|
||||||
// Stats returns the current number of connected clients and open listeners.
|
// Stats returns the current number of connected clients and open listeners.
|
||||||
func (rl *Relay) Stats() (clients, listeners int) {
|
func (rl *Relay) Stats() (clients, listeners int) {
|
||||||
rl.clientsMutex.Lock()
|
rl.clientsMutex.Lock()
|
||||||
@@ -211,3 +219,10 @@ func (rl *Relay) Router() *http.ServeMux {
|
|||||||
func (rl *Relay) SetRouter(mux *http.ServeMux) {
|
func (rl *Relay) SetRouter(mux *http.ServeMux) {
|
||||||
rl.serveMux = mux
|
rl.serveMux = mux
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (rl *Relay) WithServiceURL(serviceURL string) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
ctx := context.WithValue(r.Context(), serviceURLOverrideKey, serviceURL)
|
||||||
|
rl.ServeHTTP(w, r.WithContext(ctx))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,9 @@ package khatru
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -9,9 +12,60 @@ import (
|
|||||||
|
|
||||||
"fiatjaf.com/nostr"
|
"fiatjaf.com/nostr"
|
||||||
"fiatjaf.com/nostr/eventstore/slicestore"
|
"fiatjaf.com/nostr/eventstore/slicestore"
|
||||||
|
"fiatjaf.com/nostr/nip11"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestWithServiceURL(t *testing.T) {
|
||||||
|
relay := NewRelay()
|
||||||
|
relay.Info.Icon = "icon.png"
|
||||||
|
relay.Info.Banner = "banner.png"
|
||||||
|
relay.Router().HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusAccepted)
|
||||||
|
io.WriteString(w, "fallback")
|
||||||
|
})
|
||||||
|
|
||||||
|
handlerA := relay.WithServiceURL("https://a.example/relay")
|
||||||
|
handlerB := relay.WithServiceURL("https://b.example/relay")
|
||||||
|
|
||||||
|
t.Run("uses override for nip11 base url", func(t *testing.T) {
|
||||||
|
for _, tc := range []struct {
|
||||||
|
name string
|
||||||
|
handler http.Handler
|
||||||
|
expectedBase string
|
||||||
|
}{
|
||||||
|
{name: "first interface", handler: handlerA, expectedBase: "https://a.example/relay"},
|
||||||
|
{name: "second interface", handler: handlerB, expectedBase: "https://b.example/relay"},
|
||||||
|
} {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
req := httptest.NewRequest(http.MethodGet, "http://internal/relay", nil)
|
||||||
|
req.Header.Set("Accept", "application/nostr+json")
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
|
tc.handler.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
require.Equal(t, http.StatusOK, rr.Code)
|
||||||
|
|
||||||
|
var info nip11.RelayInformationDocument
|
||||||
|
require.NoError(t, json.NewDecoder(rr.Body).Decode(&info))
|
||||||
|
require.Equal(t, tc.expectedBase+"/icon.png", info.Icon)
|
||||||
|
require.Equal(t, tc.expectedBase+"/banner.png", info.Banner)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("uses override for relay path matching", func(t *testing.T) {
|
||||||
|
req := httptest.NewRequest(http.MethodGet, "http://internal/not-relay", nil)
|
||||||
|
req.Header.Set("Accept", "application/nostr+json")
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
|
handlerA.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
require.Equal(t, http.StatusAccepted, rr.Code)
|
||||||
|
require.Equal(t, "fallback", rr.Body.String())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestBasicRelayFunctionality(t *testing.T) {
|
func TestBasicRelayFunctionality(t *testing.T) {
|
||||||
// setup relay with in-memory store
|
// setup relay with in-memory store
|
||||||
relay := NewRelay()
|
relay := NewRelay()
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ const (
|
|||||||
subscriptionIdKey
|
subscriptionIdKey
|
||||||
nip86HeaderAuthKey
|
nip86HeaderAuthKey
|
||||||
internalCallKey
|
internalCallKey
|
||||||
|
serviceURLOverrideKey
|
||||||
)
|
)
|
||||||
|
|
||||||
func RequestAuth(ctx context.Context) {
|
func RequestAuth(ctx context.Context) {
|
||||||
|
|||||||
Reference in New Issue
Block a user