feat(server): tenant-registry skeleton boots against dev stack
Minimal Go service so platform/portal has something to resolve in local
dev. Stdlib net/http with Go 1.22 enhanced ServeMux (method+path
patterns); no third-party deps yet.
Layout:
cmd/server/main.go entry point with graceful shutdown
internal/config/ env-driven config (APP_ENV, ADDR, KC issuer)
internal/server/ http handlers + request-logging middleware
internal/store/memory.go in-memory tenant store, seeded with acme
migrations/0001_init.up.sql schema for the M4.1 follow-up (unapplied)
Makefile dev/test/build/lint/docker targets
Dockerfile multi-stage distroless build
Endpoints (under :8080 in dev):
GET /healthz
GET /v1/tenants/by-slug/{slug} 200 acme | 404
GET /v1/tenants/{id} 200 by uuid | 404
JWT validation and the real Postgres-backed store land in the M4.1
follow-up PR — keeping this PR strictly to 'boots, replies, tests pass'.
Refs: M4.1 (skeleton)
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
// Package store is a stand-in for the real Postgres-backed tenant store.
|
||||
// The skeleton ships an in-memory implementation pre-seeded with one tenant
|
||||
// (acme) so portal middleware has something to resolve in local dev.
|
||||
// Replace with a pgx-backed implementation in the M4.1 follow-up PR.
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ErrNotFound = errors.New("tenant not found")
|
||||
|
||||
type Tenant struct {
|
||||
ID string `json:"id"`
|
||||
Slug string `json:"slug"`
|
||||
Name string `json:"name"`
|
||||
Status string `json:"status"` // active | trial | frozen | archived | demo
|
||||
Plan string `json:"plan"` // starter | professional | enterprise
|
||||
Products []string `json:"products"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type Memory struct {
|
||||
mu sync.RWMutex
|
||||
bySlug map[string]*Tenant
|
||||
byID map[string]*Tenant
|
||||
}
|
||||
|
||||
func NewMemory() *Memory {
|
||||
m := &Memory{
|
||||
bySlug: make(map[string]*Tenant),
|
||||
byID: make(map[string]*Tenant),
|
||||
}
|
||||
seed := &Tenant{
|
||||
ID: "00000000-0000-0000-0000-000000000001",
|
||||
Slug: "acme",
|
||||
Name: "Acme Inc.",
|
||||
Status: "active",
|
||||
Plan: "professional",
|
||||
Products: []string{"certifai", "compliance"},
|
||||
CreatedAt: time.Now().UTC(),
|
||||
}
|
||||
m.bySlug[seed.Slug] = seed
|
||||
m.byID[seed.ID] = seed
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *Memory) BySlug(_ context.Context, slug string) (*Tenant, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
t, ok := m.bySlug[slug]
|
||||
if !ok {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
cp := *t
|
||||
return &cp, nil
|
||||
}
|
||||
|
||||
func (m *Memory) ByID(_ context.Context, id string) (*Tenant, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
t, ok := m.byID[id]
|
||||
if !ok {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
cp := *t
|
||||
return &cp, nil
|
||||
}
|
||||
Reference in New Issue
Block a user