Skip to content

Commit

Permalink
add tenant id into contest
Browse files Browse the repository at this point in the history
  • Loading branch information
gernest committed Feb 20, 2024
1 parent a25eb3b commit 1f4e9e4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
3 changes: 3 additions & 0 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func New(ctx context.Context, o *v1.Config, tenants *tenant.Tenants) (*API, erro
tenants: tenants,
}
base := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/api/v1/") {
r.WithContext(tenants.Load(ctx, r.URL.Query()))
}
w.Header().Add(vary, acceptEncoding)
code := &statsWriter{ResponseWriter: w, compress: acceptsGzip(r)}
defer func() {
Expand Down
31 changes: 30 additions & 1 deletion internal/tenant/tenants.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package tenant

import v1 "github.com/vinceanalytics/vince/gen/go/staples/v1"
import (
"context"
"net/url"

v1 "github.com/vinceanalytics/vince/gen/go/staples/v1"
)

const Default = "staples"

Expand Down Expand Up @@ -60,6 +65,30 @@ func (t *Tenants) AllDomains() (o []*v1.Domain) {
return
}

func (t *Tenants) Load(ctx context.Context, q url.Values) context.Context {
v := q.Get("tenant_id")
if v == "" {
site := q.Get("site_id")
if site != "" {
s := t.Get(site)
if s != nil {
v = s.Id
}
}
}
return With(ctx, v)
}

func (t *Tenants) All() []*v1.Tenant {
return t.all
}

type tenantId struct{}

func With(ctx context.Context, id string) context.Context {
return context.WithValue(ctx, tenantId{}, id)
}

func Get(ctx context.Context) string {
return ctx.Value(tenantId{}).(string)
}

0 comments on commit 1f4e9e4

Please sign in to comment.