From 47aff6b106cdaa13672becccdb4c09f4661f7553 Mon Sep 17 00:00:00 2001 From: Jon Lundy Date: Wed, 7 Sep 2022 12:26:14 -0600 Subject: [PATCH] Fix join (#2) * fix: baseurl join * chore: update graphql gen --- app/salty/salty-user.go | 7 ++++--- app/salty/service.go | 6 ++++-- go.mod | 2 +- internal/graph/generated/generated.go | 2 +- main.go | 10 ++++++++-- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/app/salty/salty-user.go b/app/salty/salty-user.go index 10bc1bd..6d5d6c4 100644 --- a/app/salty/salty-user.go +++ b/app/salty/salty-user.go @@ -6,9 +6,10 @@ import ( "crypto/sha256" "fmt" "log" - "path" + "net/url" "strings" + "github.com/keys-pub/keys" "github.com/oklog/ulid/v2" "github.com/sour-is/ev/pkg/es/event" @@ -52,9 +53,9 @@ func (a *SaltyUser) OnUserRegister(name string, pubkey *keys.EdX25519PublicKey) func (a *SaltyUser) Nick() string { return a.name } func (a *SaltyUser) Inbox() string { return a.inbox.String() } func (a *SaltyUser) Pubkey() string { return a.pubkey.String() } -func (s *SaltyUser) Endpoint(ctx context.Context) string { +func (s *SaltyUser) Endpoint(ctx context.Context) (string, error) { svc := gql.FromContext[contextKey, *service](ctx, saltyKey) - return path.Join(svc.BaseURL(), s.inbox.String()) + return url.JoinPath(svc.BaseURL(), s.inbox.String()) } type UserRegistered struct { diff --git a/app/salty/service.go b/app/salty/service.go index c442ee9..3cf4922 100644 --- a/app/salty/service.go +++ b/app/salty/service.go @@ -8,7 +8,7 @@ import ( "fmt" "net" "net/http" - "path" + "net/url" "strings" "github.com/keys-pub/keys" @@ -124,12 +124,14 @@ func (s *service) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } + basePath, _ := url.JoinPath(s.baseURL, a.inbox.String()) + err = json.NewEncoder(w).Encode( struct { Endpoint string `json:"endpoint"` Key string `json:"key"` }{ - Endpoint: path.Join(s.baseURL, a.inbox.String()), + Endpoint: basePath, Key: a.pubkey.ID().String(), }) if err != nil { diff --git a/go.mod b/go.mod index 1b451d2..dc56ea1 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/sour-is/ev -go 1.18 +go 1.19 require ( github.com/99designs/gqlgen v0.17.13 diff --git a/internal/graph/generated/generated.go b/internal/graph/generated/generated.go index cad5da7..fc78276 100644 --- a/internal/graph/generated/generated.go +++ b/internal/graph/generated/generated.go @@ -2267,7 +2267,7 @@ func (ec *executionContext) _SaltyUser_endpoint(ctx context.Context, field graph }() resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.Endpoint(ctx), nil + return obj.Endpoint(ctx) }) if err != nil { ec.Error(ctx, err) diff --git a/main.go b/main.go index f837534..2a86fad 100644 --- a/main.go +++ b/main.go @@ -4,9 +4,9 @@ import ( "context" "log" "net/http" + "net/url" "os" "os/signal" - "path" "strings" "time" @@ -81,7 +81,13 @@ func run(ctx context.Context) error { if enable.Has("salty") { span.AddEvent("Enable Salty") - salty, err := salty.New(ctx, es, path.Join(env("EV_BASE_URL", "http://"+s.Addr), "inbox")) + base, err := url.JoinPath(env("EV_BASE_URL", "http://"+s.Addr), "inbox") + if err != nil { + span.RecordError(err) + return err + } + + salty, err := salty.New(ctx, es, base) if err != nil { span.RecordError(err) return err