web

package
v0.0.0-...-751db77 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: BSD-3-Clause Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package web holds the HTTP helpers shared by the servers: JSON in and out, CORS, bearer tokens.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BearerToken

func BearerToken(r *http.Request) (string, bool)

BearerToken returns the token from an "Authorization: Bearer" header.

func CORS

func CORS(h http.Handler, methods string) http.Handler

CORS wraps h with permissive CORS headers and answers preflight requests. This is safe because authentication is always explicit (bearer tokens or HTTP Basic), never cookies, and Access-Control-Allow-Credentials is never sent.

func DecodeJSON

func DecodeJSON(w http.ResponseWriter, r *http.Request, v any) bool

DecodeJSON decodes a JSON request body of at most 1 MiB, rejecting unknown fields. On failure it writes a 400 and returns false.

func HashToken

func HashToken(token string) string

func NewToken

func NewToken(prefix string) (token, hash string)

NewToken returns a new random token with the given prefix (which tells tokens of different services apart) and the hash to store.

func RandomString

func RandomString(n int) string

RandomString returns n random bytes, base64url-encoded.

func ServerError

func ServerError(w http.ResponseWriter, err error)

ServerError logs err and writes a generic 500.

func WriteError

func WriteError(w http.ResponseWriter, status int, msg string)

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, v any)

Types

This section is empty.

Jump to

Keyboard shortcuts

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