server

package
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 73 Imported by: 0

Documentation

Overview

Package server assembles the moth HTTP handler: connect (gRPC / gRPC-Web) services and the plain-HTTP surfaces, multiplexed on one port.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Protocols

func Protocols() *http.Protocols

Protocols returns the protocol set for the http.Server: HTTP/1.1 plus unencrypted HTTP/2.

func RequestIDFromContext

func RequestIDFromContext(ctx context.Context) string

RequestIDFromContext returns the request ID assigned by the interceptor.

func SelfCheck

func SelfCheck(dataDir string, master keys.MasterKey, now time.Time) error

SelfCheck validates the preconditions for serving and returns an actionable error when one fails: the data directory must be writable, the system clock must be sane, and the master key must decrypt the persisted canary. The canary is written on first run; on later runs a decrypt failure means the supplied MOTH_MASTER_KEY (or master.key) does not match this data directory, which would otherwise surface only when a project's private key is touched. Run it before ListenAndServe.

Types

type Options

type Options struct {
	Config config.Config
	Store  Store
	Master keys.MasterKey
	Logger *slog.Logger
	// Mailer delivers auth emails; nil falls back to the console
	// transport (dev default).
	Mailer mail.Mailer
	// RateLimit overrides the credential-facing rate-limit tiers. Nil means
	// the tiers resolved from Config (production) or, for the zero-value test
	// config, disabled. Tests point it at tiny limits to exercise throttling.
	RateLimit *ratelimit.Config
	// SetupToken guards the first-run admin setup screen. Empty when an
	// admin account already exists.
	SetupToken string
	// AuthEndpoints override the Google/Apple OAuth endpoint locations;
	// zero value means the real providers (tests point them at doubles).
	AuthEndpoints authrpc.ProviderEndpoints
	// StripeBaseURL overrides the Stripe API host for the billing engine;
	// empty means the real https://api.stripe.com. A testing hook like
	// AuthEndpoints (the SDK e2e harnesses point it at a local double via
	// the env-only MOTH_STRIPE_API_URL — see cmd/moth/serve.go).
	StripeBaseURL string
	// Now is injectable for tests; defaults to time.Now.
	Now func() time.Time
	// Metrics is the instrumentation registry; nil creates a fresh one.
	Metrics *metrics.Registry
	// Reflection enables gRPC server reflection in release builds (dev
	// builds always enable it).
	Reflection bool
}

Options configures a Server.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is the assembled moth server.

func New

func New(o Options) (*Server, error)

New assembles the full handler.

func (*Server) Billing

func (s *Server) Billing() *billingrpc.Handler

Billing exposes the billing handler so the serve loop can schedule the subscription reconciliation sweep.

func (*Server) Close

func (s *Server) Close(ctx context.Context) error

Close stops the async analytics writer, draining its buffered events until ctx expires. Call it after the HTTP server has stopped accepting requests so no event is emitted into a closed writer.

func (*Server) Handler

func (s *Server) Handler() http.Handler

Handler returns the root HTTP handler. Serve it with Protocols() so native gRPC clients can speak HTTP/2 without TLS (h2c) on the same port as plain HTTP/1.1 (browsers, curl, pub client).

func (*Server) Metrics

func (s *Server) Metrics() *metrics.Registry

Metrics exposes the instrumentation registry so the serve loop can feed it the counters it owns (e.g. rollup runs).

func (*Server) Rollup

func (s *Server) Rollup() *analytics.Rollup

Rollup exposes the analytics aggregate-and-prune job so the serve loop can schedule it next to its other maintenance goroutines.

func (*Server) RollupObserver

func (s *Server) RollupObserver() func(err error)

RollupObserver returns a callback for analytics.Rollup.RunPeriodically that records each scheduled run's outcome in moth_rollup_runs_total.

type Store

type Store interface {
	adminrpc.Store
	store.EmailTokenStore
	store.OAuthTokenStore
	store.RateLimitStore
	// StoreNotificationStore backs the billing webhook idempotency; the other
	// billing surfaces come in through adminrpc.Store.
	store.StoreNotificationStore
	// StripeCustomerStore backs the Stripe checkout/webhook user↔customer
	// mapping (milestone 17).
	store.StripeCustomerStore
}

Store is everything the assembled server needs from persistence. adminrpc.Store already covers the event and daily-stats surfaces the analytics pipeline needs.

Directories

Path Synopsis
rpc
admin
Package adminrpc implements the moth.admin.v1 connect services.
Package adminrpc implements the moth.admin.v1 connect services.
auth
Package authrpc implements moth.auth.v1.AuthService — the public end-user authentication API.
Package authrpc implements moth.auth.v1.AuthService — the public end-user authentication API.
billing
Package billingrpc implements moth.billing.v1.BillingService — the publishable-key + Bearer client API for subscriptions and entitlements — and hosts the shared store-validation plumbing the webhook and reconciliation sweep reuse.
Package billingrpc implements moth.billing.v1.BillingService — the publishable-key + Bearer client API for subscriptions and entitlements — and hosts the shared store-validation plumbing the webhook and reconciliation sweep reuse.
push
Package pushrpc implements moth.push.v1.PushService — the publishable-key + Bearer client API for the push-device registry (milestone 20).
Package pushrpc implements moth.push.v1.PushService — the publishable-key + Bearer client API for the push-device registry (milestone 20).
serverapi
Package serverapi implements moth.server.v1 — the services the developer's own backend calls with the project secret key (`x-moth-key: sk_...`): online token introspection and programmatic user management.
Package serverapi implements moth.server.v1 — the services the developer's own backend calls with the project secret key (`x-moth-key: sk_...`): online token introspection and programmatic user management.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL