server

package
v0.2.49 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: GPL-2.0, GPL-3.0 Imports: 35 Imported by: 0

Documentation

Overview

Package server — REST Idempotency-Key dedup middleware.

Wraps the mux so a retried mutation (POST/PUT/PATCH/DELETE) carrying a stable `Idempotency-Key` header replays the first response instead of re-executing the handler. The @cplieger/actions client sends the same key across an action's network retries, so a request that timed out client-side (but actually succeeded server-side) replays the cached outcome on retry rather than duplicating the mutation.

This is the HTTP/REST sibling of the POST /api/command request_id dedup in internal/command (which caches a JSON body keyed by request_id via internal/dedup.Cache). The REST path needs a richer entry — status code + Content-Type + body, plus an in-flight marker — so it carries its own focused cache rather than reusing dedup.Cache (which stores only []byte and has no in-flight concept). The TTL, mutex-guarded map, lazy-eviction-on-access, and periodic janitor goroutine all mirror dedup.Cache.

Package server — security middleware: CSP headers, the ALLOWED_HOSTS anti-DNS-rebinding gate, and stdlib CSRF protection.

Applied once at the top of the mux in ListenAndServe. CSP is a defense-in-depth layer: if the markdown renderer ever leaks an XSS vector, CSP contains it. CSRF protection uses Go 1.25's net/http.CrossOriginProtection, which checks Sec-Fetch-Site first (preferred per OWASP Fetch Metadata) and falls back to comparing the Origin header host against the Host header.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CLIRunner

type CLIRunner interface {
	// Run executes the CLI and returns combined stdout+stderr.
	Run(ctx context.Context, args ...string) ([]byte, error)
	// RunStdoutCapped executes the CLI capturing STDOUT only (stderr is
	// captured separately, bounded, and logged — never returned or merged
	// into the result), stopping the captured stdout at limit bytes. It
	// returns the possibly-truncated stdout, whether truncation occurred,
	// and any exec error.
	RunStdoutCapped(ctx context.Context, limit int, args ...string) (out []byte, truncated bool, err error)
}

CLIRunner abstracts subprocess execution for kiro-cli commands, enabling unit testing of handler logic without a real binary.

type Option

type Option func(*Server)

Option configures a Server at construction time.

func WithAccountUsage added in v0.1.165

func WithAccountUsage(p api.AccountUsageProvider) Option

WithAccountUsage sets the provider for account/subscription usage, served at GET /api/account/usage (sidebar footer).

func WithAuth

func WithAuth(a api.AuthHandler) Option

WithAuth sets the auth handler for login, logout, and whoami endpoints.

func WithCLIPath

func WithCLIPath(p string) Option

WithCLIPath sets the path to the kiro-cli binary used for CLI sub-operations.

func WithChats

func WithChats(c api.ChatStore) Option

WithChats sets the chat store used for reading and writing chat files.

func WithConfigDir

func WithConfigDir(d string) Option

WithConfigDir sets the configuration directory path used for chat files and settings.

func WithFiles

func WithFiles(f api.FileHandler) Option

WithFiles sets the file handler for workspace file read/write endpoints.

func WithForges

func WithForges(r api.RouteHandler) Option

WithForges sets the route handler for forge (GitHub/GitLab/Gitea) HTTP endpoints.

func WithGit

func WithGit(g api.GitHandler) Option

WithGit sets the git handler for non-AI git HTTP endpoints.

func WithGitAI

func WithGitAI(r api.RouteHandler) Option

WithGitAI sets the route handler for AI-assisted git operations.

func WithHostPolicy added in v0.2.37

func WithHostPolicy(p *webhttp.HostPolicy) Option

WithHostPolicy sets the exact-match Host allowlist (parsed from ALLOWED_HOSTS) that the security middleware applies before the CSRF check — the anti-DNS-rebinding gate. A nil or inactive policy is a pass-through (any Host accepted, the backward-compatible default).

func WithHub

func WithHub(h api.Hub) Option

WithHub sets the hub that manages bridge processes and SSE broadcasts.

func WithMCPConfig

func WithMCPConfig(r api.RouteHandler) Option

WithMCPConfig sets the route handler for MCP server configuration endpoints.

func WithMCPRegistry

func WithMCPRegistry(r api.RouteHandler) Option

WithMCPRegistry sets the route handler for the MCP registry proxy endpoint.

func WithMCPStatus

func WithMCPStatus(r api.RouteHandler) Option

WithMCPStatus sets the route handler for the MCP runtime status endpoint.

func WithPolicy added in v0.1.165

func WithPolicy(p api.PolicyProvider) Option

WithPolicy sets the native Cedar policy provider, backing the read-only policy view at GET /api/permissions and the pre-flight simulation at POST /api/permissions/explain. The rule WRITER at POST /api/permissions/rules needs no provider (it is a file write KAS hot-reloads).

func WithPush

func WithPush(p api.PushService) Option

WithPush sets the push service used for Web Push notification delivery.

func WithStaticFS

func WithStaticFS(staticFS fs.FS) Option

WithStaticFS sets the embedded filesystem serving the compiled web UI.

func WithSteering

func WithSteering(g api.SteeringGenerator) Option

WithSteering sets the steering generator used to produce environment.md for kiro-cli.

func WithTools added in v0.1.184

func WithTools(e *toolbelt.Engine) Option

WithTools sets the tools engine backing the /api/tools surface.

func WithTrustedProxies added in v0.1.141

func WithTrustedProxies(trusted []*net.IPNet) Option

WithTrustedProxies sets the reverse-proxy networks trusted when resolving the access-log client_ip via webhttp.WithClientIP. Empty/nil trusts nothing, so the unspoofable socket-peer host is logged (the spoof-safe default for a directly-exposed deployment).

func WithUtilityPrompt

func WithUtilityPrompt(p api.UtilityPrompter) Option

WithUtilityPrompt sets the utility prompter used for AI-assisted tasks (rename, commit message, etc.).

func WithWorkDir

func WithWorkDir(d string) Option

WithWorkDir sets the workspace directory served by the file handler and git endpoints.

type Server

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

Server holds shared state and registers all HTTP handlers.

func New

func New(opts ...Option) *Server

New constructs a Server with the given options applied.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

ListenAndServe registers all routes and starts the HTTP server. Blocks until SIGTERM/SIGINT, then shuts down gracefully.

Jump to

Keyboard shortcuts

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