Files
job-search/internal/config/conf_server_websocket.go
T
bwilliams a279405cae Added the HTTP server and serve command, with proxy, CSRF, TLS and security header config; wsserve is now ws serve, alongside a new ws dial
The serve command runs internal/server: the API under /api (health, and
the CSRF token endpoint) and the WebSocket hub under /ws, over HTTP or TLS,
with an optional HTTP to HTTPS redirect, the PROXY protocol from trusted
sources and a graceful shutdown that closes WebSockets first. Its middleware
covers the client IP behind trusted proxies, request IDs, request logging,
panic recovery, global and API rate limits, Host and body size checks,
security response headers, compression, CSRF protection (cross-origin
checks and signed double-submit tokens) and JSON-only API bodies. Middleware
that logs takes a WithLogger option.

New server config sections: proxy, csrf, tls, headers and frontend (not
used yet), plus max_header_bytes, max_body_bytes, global and API rate limits
and HTTP read, write and idle timeouts. Every setting documents what 0 or
empty means, and internal/config/README.md is reorganised by section.
server.timeouts.read_header must be greater than 0, and a header or idle
timeout of 0 means no limit rather than the read timeout.

WebSocket upgrades are protected by the Origin check: the server's own
origin, or one of csrf.cross_origin_protection.trusted_origins, which the
hub now takes with WithTrustedOrigins.

wsserve becomes "ws serve". "ws dial" opens a WebSocket on /ws to test it:
--ping sends a ping and waits for the pong, --message sends messages and
prints the replies, --origin tests the Origin check, and with neither
--ping nor --message each line of stdin is sent until Ctrl+C.
2026-10-15 17:47:40 +01:00

69 lines
2.4 KiB
Go

package config
import (
"errors"
"reflect"
"time"
"git.avsdev.uk/Go/job-search/internal/util"
)
// JSCKServerWebSocket is the websocket section's configuration key.
const JSCKServerWebSocket string = "websocket"
// JSCServerWebSocket configures the server's websocket connections. A
// browser's upgrade must come from the server's own origin or one of
// csrf.cross_origin_protection.trusted_origins. MaxMessageSize is the
// largest message read from a client; 0 means the default (32KiB), and -1
// (no limit) is rejected (see validate). A WriteTimeout or PongTimeout of 0
// or less means no limit, and a PingInterval of 0 or less turns pings off.
type JSCServerWebSocket struct {
EnableCompression bool `cfg:"enable_compression" default:"false"`
MaxMessageSize util.ByteSize `cfg:"max_message_size" default:"32KiB"`
ConnectionLimit JSCRateLimitRule `cfg:"connection_limit"`
MessageRateLimit JSCRateLimitRule `cfg:"message_rate_limit"`
WriteTimeout time.Duration `cfg:"write_timeout" default:"10s"`
PingInterval time.Duration `cfg:"ping_interval" default:"30s"`
PongTimeout time.Duration `cfg:"pong_timeout" default:"10s"`
}
// ---------- Section methods (see Section) ----------
// Map implements Section.
func (c JSCServerWebSocket) Map() map[string]any { return mapOf(c) }
// Flatten implements Section.
func (c JSCServerWebSocket) Flatten() map[string]any {
return flattenOf(JSCKServerWebSocket, c)
}
// TypeOf implements Section.
func (c JSCServerWebSocket) TypeOf(key string) (reflect.Kind, error) {
return typeOf(c, key)
}
// ValueOf implements Section.
func (c JSCServerWebSocket) ValueOf(key string) (any, error) {
return valueOf(c, key)
}
// SetValue implements Section.
func (c *JSCServerWebSocket) SetValue(key string, value any) error {
return setValue(c, key, value)
}
// ---------- Section specific methods ----------
// validate checks values before they are stored (see validator).
// max_message_size can't be -1 (no limit): a client could then send a
// message of any size, and the server holds each message in memory.
func (c *JSCServerWebSocket) validate(key string, value any) error {
if key == "max_message_size" &&
value.(util.ByteSize) == util.ByteSizeNoLimit {
return errors.New(
"config: max_message_size can't be -1 (no limit): set an explicit size, or 0 for the default",
)
}
return nil
}