webapp

package
v0.54.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package webapp contains deliberately small building blocks shared by the runnable web applications in cmd/. It keeps the examples dependency-free while making their database and HTTP behaviour consistent.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(ctx context.Context, db *sql.DB, statements ...string) error

Apply executes schema statements one by one so error messages name the failing statement. The schemas in these examples are intentionally simple and use only TinySQL's public SQL surface.

func ContextWithTimeout

func ContextWithTimeout(parent context.Context) (context.Context, context.CancelFunc)

ContextWithTimeout returns a modest request timeout used by command apps.

func Error

func Error(w http.ResponseWriter, r *http.Request, status int, message string)

Error writes a JSON error when a request targets /api/, otherwise a plain text HTTP error. It avoids leaking implementation details from handlers.

func JSON

func JSON(w http.ResponseWriter, status int, value any)

JSON writes a JSON response. It is suitable for the compact browser APIs in the examples and intentionally does not expose internal errors itself.

func MountShared added in v0.50.0

func MountShared(mux *http.ServeMux)

MountShared registers the assets that every demo shares under /static/. Call it before the app's own "GET /static/" file server: ServeMux prefers the more specific pattern, so the app keeps full control of every other path below /static/.

func NextID

func NextID(ctx context.Context, q interface {
	QueryRowContext(context.Context, string, ...any) *sql.Row
}, table string) (int64, error)

NextID returns the next positive integral key for a fixed, trusted table name. Table names are supplied by application code, never HTTP input.

func Open

func Open(ctx context.Context, dsn string) (*sql.DB, error)

Open opens a TinySQL database and verifies it before returning. Callers can pass mem:// for ephemeral data or file:path.db?autosave=1 for persistence.

func SecurityHeaders

func SecurityHeaders(next http.Handler) http.Handler

SecurityHeaders adds a compact baseline appropriate for local web tools. No third-party scripts, frames or plugin content are required by these apps.

Types

type CheckPageOptions added in v0.50.0

type CheckPageOptions struct {
	// AllowExternalAssets lists hosts the page is permitted to load from, for
	// example "cdn.jsdelivr.net". Any other absolute URL is reported. A page
	// that loads nothing remotely — the goal for a local-first application —
	// leaves this empty.
	AllowExternalAssets []string
}

CheckPageOptions tunes CheckPage for a specific application.

type PageProblem added in v0.50.0

type PageProblem struct {
	Kind   string // "missing-asset", "blocked-inline", "external-asset"
	Detail string
}

PageProblem describes one defect found by CheckPage.

func CheckPage added in v0.50.0

func CheckPage(handler http.Handler, path string, opts CheckPageOptions) ([]PageProblem, error)

CheckPage fetches path from handler and reports the defects that a plain status-code test cannot see.

It exists because two applications in this repository shipped a Content-Security-Policy without 'unsafe-inline' while keeping their entire stylesheet — and, in one case, every interactive handler — in inline <style> and <script> blocks. The browser dropped all of it, so the page rendered unstyled and its buttons did nothing, while every Go test still saw a healthy 200. The checks below are exactly the ones that would have caught that on the first day:

  • every same-origin stylesheet and script the page references is actually served,
  • no inline style/script content or on*= handler survives a CSP that forbids it, and
  • no asset is loaded from an unexpected host.

func (PageProblem) String added in v0.50.0

func (p PageProblem) String() string

Jump to

Keyboard shortcuts

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