server

package
v0.0.0-...-cc4816d Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package server is fenster's OpenAI-compatible HTTP layer. It is pure: it takes a Backend interface and exposes apfel's wire format byte-for-byte.

DRY: all error responses go through writeError; all chat-success responses go through writeChatResponse; all SSE chunks go through writeSSE.

The Mux uses Go 1.22's stdlib pattern routing (METHOD /path) so we don't pull in a third-party router.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultOriginAllowlist

func DefaultOriginAllowlist() []string

DefaultOriginAllowlist returns the origins fenster (and apfel) allow by default: localhost variants only. Apfel matches on the host portion of the Origin header, accepting common loopback names regardless of port or scheme.

func NewMux

func NewMux(cfg Config) http.Handler

NewMux returns the configured http.Handler.

func OriginAllowed

func OriginAllowed(origin string, allowlist []string) bool

OriginAllowed reports whether the given Origin header value is in the allowlist. Empty origin → allowed (curl, SDK use, no CORS context).

"*" in the allowlist matches any origin (footgun mode).

For non-wildcard entries, we compare scheme + host (port-agnostic) so http://localhost:3000 matches an allowlist entry of http://localhost.

Types

type Config

type Config struct {
	Backend        backend.Backend
	EnableCORS     bool     // when false, OPTIONS still resolves but no ACAO is emitted
	BearerToken    string   // optional; when non-empty, /v1/* requires Authorization: Bearer <token>
	AllowedOrigins []string // origin allowlist; nil → DefaultOriginAllowlist
	NoOriginCheck  bool     // disable origin check (preserves bearer auth)
	PublicHealth   bool     // when true, /health is reachable without token
	Footgun        bool     // disables origin + bearer checks; CORS preflight wide-open
	BindHost       string   // bind host; loopback bypasses /health auth automatically
	Debug          bool
}

Config configures NewMux.

type LogEntry

type LogEntry struct {
	Time         string `json:"time"`
	Method       string `json:"method"`
	Path         string `json:"path"`
	Status       int    `json:"status"`
	DurationMs   int64  `json:"duration_ms"`
	RequestBody  string `json:"request_body"`
	ResponseBody string `json:"response_body"`
}

LogEntry is one captured request/response pair.

type LogStore

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

LogStore is a small ring buffer of LogEntry.

func NewLogStore

func NewLogStore(max int) *LogStore

NewLogStore creates a store with capacity max.

func (*LogStore) Add

func (s *LogStore) Add(e LogEntry)

Add records one entry.

func (*LogStore) List

func (s *LogStore) List(limit int) []LogEntry

List returns the most recent up to limit entries.

func (*LogStore) Stats

func (s *LogStore) Stats() map[string]any

Stats returns aggregate counts.

type State

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

State is the shared, request-spanning state (active request gauge, etc.).

func (*State) ActiveRequests

func (s *State) ActiveRequests() int64

ActiveRequests returns the current count.

func (*State) IncRequest

func (s *State) IncRequest() func()

IncRequest atomically increments the active-request counter; returns a decrement function for defer.

Jump to

Keyboard shortcuts

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