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 ¶
- func Apply(ctx context.Context, db *sql.DB, statements ...string) error
- func ContextWithTimeout(parent context.Context) (context.Context, context.CancelFunc)
- func Error(w http.ResponseWriter, r *http.Request, status int, message string)
- func JSON(w http.ResponseWriter, status int, value any)
- func NextID(ctx context.Context, q interface{ ... }, table string) (int64, error)
- func Open(ctx context.Context, dsn string) (*sql.DB, error)
- func SecurityHeaders(next http.Handler) http.Handler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Apply ¶
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 ¶
ContextWithTimeout returns a modest request timeout used by command apps.
func Error ¶
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 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.
Types ¶
This section is empty.