Help us shape the future of Go! Take the 2026 Go Developer Survey

api

package
v0.30.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 27 Imported by: 0

Documentation

Overview

Package api provides the HTTP server for the stackit-web application.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RequestIDFromContext added in v0.19.0

func RequestIDFromContext(ctx context.Context) string

RequestIDFromContext is kept here as a re-export for callers in the api package; handlers in other packages import internal/api/reqid directly.

Types

type AuthConfig added in v0.19.0

type AuthConfig struct {
	Handler      *auth.Handler
	SessionStore auth.Store

	// Cipher decrypts the GitHub access token stored on a session. Handlers
	// that act as the requesting user (e.g. runtime repo onboarding verifying
	// the user's access to a repo) need it to recover the token from
	// SessionFromContext. It is the same cipher the OAuth handler sealed with.
	Cipher *auth.Cipher
}

AuthConfig is the runtime auth setup. SessionStore must outlive the Server's lifetime (close it after Shutdown returns).

type RateLimitConfig added in v0.20.0

type RateLimitConfig struct {
	RequestsPerSecond float64
	Burst             int
}

RateLimitConfig configures the per-IP token-bucket limiter. Zero RPS/Burst take the defaults; a negative RPS disables limiting entirely.

type Server

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

Server is the stackit-web HTTP server. Per-repo state (engine, watcher, broadcaster) lives on each registry entry — the server only owns transport-level concerns.

func NewServer

func NewServer(cfg ServerConfig) *Server

NewServer creates a new API server backed by the given registry.

func (*Server) Shutdown

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

Shutdown gracefully stops the server. The registry's watchers and broadcasters are torn down separately by main.go's defer reg.Close().

func (*Server) Start

func (s *Server) Start() error

Start begins serving HTTP requests. It blocks until the server is stopped.

type ServerConfig

type ServerConfig struct {
	// BindAddr is the host/IP to listen on. Empty means "all interfaces".
	// apps/server picks a safe default (127.0.0.1) and binds 0.0.0.0 when
	// STACKIT_ENV=production.
	BindAddr    string
	Port        int
	CORSOrigins []string

	// AllowLoopbackOrigins accepts any loopback browser origin (localhost /
	// 127.0.0.1 / [::1] on any port) in addition to CORSOrigins. apps/server
	// sets it only on a loopback bind so local dev web servers — whose port is
	// environment-assigned — work without a fixed allowlist entry. An exposed
	// server leaves it false and keeps the strict explicit allowlist.
	AllowLoopbackOrigins bool

	APIPrefixes []string
	StaticFS    fs.FS
	Registry    *registry.Registry

	// SSELimits bounds concurrent Server-Sent Events connections. Zero
	// fields take the package defaults, which are generous enough for normal
	// use but cap the blast radius of a public deployment.
	SSELimits handlers.SSELimits

	// RateLimit configures the per-IP request rate limiter applied to the
	// API. Zero fields take the defaults; a negative RequestsPerSecond
	// disables limiting.
	RateLimit RateLimitConfig

	// BranchDiffConcurrency caps how many branch-diff requests run their git
	// subprocesses at once. Zero takes the handler default.
	BranchDiffConcurrency int

	// SingleRepo marks single-repo mode (the local -cwd/discovery shortcut, not
	// the DB-backed multi-tenant model). It is surfaced via /api/v1/config so
	// the web client skips the repo picker and opens the sole repo directly.
	SingleRepo bool

	// ReadOnly puts the server into a public read-only posture: the
	// mutating submit route is replaced with a handler that refuses with
	// 405 so writes are impossible by construction, not just by policy.
	// This is the safety floor for exposing a repo publicly — a read-only
	// server cannot push branches or touch GitHub no matter who calls it.
	ReadOnly bool

	// Auth bundles the OAuth handler, session store, and surrounding state
	// needed to gate /api/* routes behind GitHub login. When nil the server
	// runs without authentication; that mode is only safe on a private
	// network (localhost dev, tunneled access). apps/server refuses to
	// start unauthenticated when it binds a non-loopback interface unless
	// -read-only is set.
	Auth *AuthConfig

	// RepoStore is the persistence backend for repo configuration. It is set
	// only when the server runs in DB-backed mode (-database-url). Runtime
	// repo onboarding persists new repos here so they survive a restart; when
	// nil, onboarding is unavailable and the registry is fixed at boot.
	RepoStore *store.Store

	// ReposRoot is the absolute base directory under which onboarded repos are
	// checked out (<ReposRoot>/<owner>/<name>). Required for runtime
	// onboarding; when empty, onboarding is refused.
	ReposRoot string

	// RepoSyncTokens mints GitHub App installation tokens for git operations on
	// managed repos. It authenticates onboarding clones and background syncs.
	// Nil when no GitHub App is configured, in which case onboarding is refused
	// and the sync loop can only fetch public repos.
	RepoSyncTokens *githubpkg.AppTokenProvider

	// SyncInterval, when > 0, enables the background sync loop: every interval
	// each managed repo is mirror-fetched from its remote so the served state
	// stays current without a local actor. Zero disables it. Even with the loop
	// off, the syncer still backs on-demand refreshes (webhook receiver).
	SyncInterval time.Duration

	// GitHubWebhookSecret is the shared secret GitHub signs webhook deliveries
	// with. When set, POST /api/v1/webhooks/github accepts verified push events
	// and refreshes the matching managed repo immediately (the interval loop
	// stays as a backstop). Empty disables the endpoint (it 404s), which is the
	// correct posture for local/dev servers GitHub can't reach.
	GitHubWebhookSecret string

	// WebhookDebounce is the quiet window a repo's webhook triggers wait out
	// before a fetch runs, so a stack submit's burst of pushes collapses into a
	// single refresh. Negative uses the package default; zero dispatches
	// immediately.
	WebhookDebounce time.Duration
}

ServerConfig holds configuration for the API server.

Directories

Path Synopsis
Package auth implements GitHub OAuth + session cookies + an allowlist gate so stackit-server can be safely exposed on the public web.
Package auth implements GitHub OAuth + session cookies + an allowlist gate so stackit-server can be safely exposed on the public web.
Package githubwebhook contains the pure, transport-free pieces of the GitHub webhook receiver: verifying a delivery's HMAC signature and extracting the repository coordinates from a push payload.
Package githubwebhook contains the pure, transport-free pieces of the GitHub webhook receiver: verifying a delivery's HMAC signature and extracting the repository coordinates from a push payload.
Package provision builds registry entries from repo coordinates, resolving the per-repo engine and GitHub client through the app bootstrap layer.
Package provision builds registry entries from repo coordinates, resolving the per-repo engine and GitHub client through the app bootstrap layer.
Package registry holds the set of repositories served by the stackit server, keyed by a stable repoID.
Package registry holds the set of repositories served by the stackit server, keyed by a stable repoID.
Package reposync keeps server-managed repo checkouts in step with their remotes.
Package reposync keeps server-managed repo checkouts in step with their remotes.
Package reqid holds the request-ID context plumbing shared between the middleware that mints the ID (internal/api) and the handlers that want to include it in audit log lines (internal/api/handlers).
Package reqid holds the request-ID context plumbing shared between the middleware that mints the ID (internal/api) and the handlers that want to include it in audit log lines (internal/api/handlers).
Package store is the API server's PostgreSQL-backed repo configuration store.
Package store is the API server's PostgreSQL-backed repo configuration store.
testpostgres
Package testpostgres provisions a Postgres backend for store tests.
Package testpostgres provisions a Postgres backend for store tests.

Jump to

Keyboard shortcuts

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