context.go

 1package backend
 2
 3import (
 4	"context"
 5
 6	"github.com/charmbracelet/soft-serve/server/access"
 7	"github.com/charmbracelet/soft-serve/server/auth"
 8	"github.com/charmbracelet/soft-serve/server/settings"
 9	"github.com/charmbracelet/soft-serve/server/store"
10)
11
12var contextKey = &struct{ string }{"backend"}
13
14// FromContext returns the backend from a context.
15func FromContext(ctx context.Context) *Backend {
16	if b, ok := ctx.Value(contextKey).(*Backend); ok {
17		return b
18	}
19
20	return nil
21}
22
23// WithContext returns a new context with the backend attached.
24func WithContext(ctx context.Context, b *Backend) context.Context {
25	ctx = settings.WithContext(ctx, b.Settings)
26	ctx = store.WithContext(ctx, b.Store)
27	ctx = access.WithContext(ctx, b.Access)
28	ctx = auth.WithContext(ctx, b.Auth)
29	ctx = context.WithValue(ctx, contextKey, b)
30	return ctx
31}