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.
This commit is contained in:
1 parent
f3a91ad915
commit
a279405cae
41 files changed
+3876
-78
No files matched your search
@@ -1,7 +1,10 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
|
||||
"git.avsdev.uk/Go/job-search/internal/util"
|
||||
)
|
||||
|
||||
// JSCKServer is the server section's configuration key.
|
||||
@@ -9,13 +12,22 @@ const JSCKServer string = "server"
|
||||
|
||||
// JSCServer configures the outward server. Host is the address to listen
|
||||
// on (empty means every interface) and Port the port (0 means any free
|
||||
// port).
|
||||
// port). MaxHeaderBytes is the largest request header block; 0 means the
|
||||
// default (8KB), and -1 (no limit) is rejected (see validate). MaxBodyBytes is
|
||||
// the largest request body; 0 means the default (1MB) and -1 no limit.
|
||||
type JSCServer struct {
|
||||
Host string `cfg:"host" default:"127.0.0.1"`
|
||||
Port int `cfg:"port" default:"9393"`
|
||||
Timeouts JSCServerTimeouts `cfg:"timeouts"`
|
||||
RateLimits JSCServerRateLimits `cfg:"rate_limits"`
|
||||
WebSocket JSCServerWebSocket `cfg:"websocket"`
|
||||
Host string `cfg:"host" default:"127.0.0.1"`
|
||||
Port int `cfg:"port" default:"9393"`
|
||||
MaxHeaderBytes util.ByteSize `cfg:"max_header_bytes" default:"8KB"`
|
||||
MaxBodyBytes util.ByteSize `cfg:"max_body_bytes" default:"1MB"`
|
||||
Timeouts JSCServerTimeouts `cfg:"timeouts"`
|
||||
RateLimits JSCServerRateLimits `cfg:"rate_limits"`
|
||||
Proxy JSCServerProxy `cfg:"proxy"`
|
||||
CSRF JSCServerCSRF `cfg:"csrf"`
|
||||
TLS JSCServerTLS `cfg:"tls"`
|
||||
Headers JSCServerHeaders `cfg:"headers"`
|
||||
Frontend JSCServerFrontend `cfg:"frontend"`
|
||||
WebSocket JSCServerWebSocket `cfg:"websocket"`
|
||||
}
|
||||
|
||||
// ---------- Section methods (see Section) ----------
|
||||
@@ -42,3 +54,18 @@ func (c JSCServer) ValueOf(key string) (any, error) {
|
||||
func (c *JSCServer) SetValue(key string, value any) error {
|
||||
return setValue(c, key, value)
|
||||
}
|
||||
|
||||
// ---------- Section specific methods ----------
|
||||
|
||||
// validate checks values before they are stored (see validator).
|
||||
// max_header_bytes can't be -1 (no limit): the HTTP server always limits the
|
||||
// header block, so -1 would quietly mean its own default instead.
|
||||
func (c *JSCServer) validate(key string, value any) error {
|
||||
if key == "max_header_bytes" &&
|
||||
value.(util.ByteSize) == util.ByteSizeNoLimit {
|
||||
return errors.New(
|
||||
"config: max_header_bytes can't be -1 (no limit): set an explicit size, or 0 for the default",
|
||||
)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user