Added websocket hub for handling websocket clients, with a wsserve command
which runs a plain TCP server for receiving websocket hub clients, with server config, CSRF and rate limit middleware. - internal/websocket: Hub pushing messages to WebSocket clients (ServeHTTP, example.com/ws upgrade with optional CSRF check) and plain TCP clients (ServeTCP, newline-delimited JSON envelopes); ping/pong keepalive, per-client message rate limit, slow consumer handling, graceful Shutdown, debug logging of messages sent and received - internal/websocket/tcpserver: standalone TCP server for a hub, with accept back-off, a per-IP connection limit (server.websocket.connection_limit) and graceful shutdown - internal/cli/wsserve: `wsserve` command running the TCP server, with --listen/--host/--port overrides - internal/middleware: keyed token bucket rate limiter, real client IP from trusted proxies - internal/httpx: RFC 9457 problem responses, request IDs in context - internal/config: server section (host, port, shutdown timeout, rate limiter idle TTL, websocket); util.ByteSize settings (e.g. "32KiB"); lists of sections as single keys; empty lists stored as nil; isExplicitlySet for printLoadedConfig; JobSearchConfigSource renamed to JSCSourcesSource; TestSections checks every section's methods - internal/util: ByteSize, parsed and formatted with go-humanize
This commit is contained in:
1 parent
803a8929e6
commit
c9f42b62ad
42 files changed
+3619
-57
No files matched your search
@@ -0,0 +1,84 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestWriteProblem(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/api/items/1", nil)
|
||||
req = req.WithContext(WithRequestID(req.Context(), "req-123"))
|
||||
rec := httptest.NewRecorder()
|
||||
WriteProblem(rec, req, http.StatusNotFound, "no such item")
|
||||
|
||||
if rec.Code != http.StatusNotFound {
|
||||
t.Errorf("status = %d; want 404", rec.Code)
|
||||
}
|
||||
if ct := rec.Header().Get("Content-Type"); ct != "application/problem+json" {
|
||||
t.Errorf("Content-Type = %q", ct)
|
||||
}
|
||||
if nosniff := rec.Header().Get("X-Content-Type-Options"); nosniff != "nosniff" {
|
||||
t.Errorf("X-Content-Type-Options = %q", nosniff)
|
||||
}
|
||||
var p Problem
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &p); err != nil {
|
||||
t.Fatalf("body %q: %v", rec.Body, err)
|
||||
}
|
||||
want := Problem{
|
||||
Type: "about:blank",
|
||||
Status: http.StatusNotFound,
|
||||
Title: "Not Found",
|
||||
Detail: "no such item",
|
||||
RequestID: "req-123",
|
||||
}
|
||||
if p != want {
|
||||
t.Errorf("problem = %+v; want %+v", p, want)
|
||||
}
|
||||
}
|
||||
|
||||
// Without a request (or a request ID) the problem has no requestId, and an
|
||||
// empty detail is left out.
|
||||
func TestWriteProblemMinimal(t *testing.T) {
|
||||
for _, req := range []*http.Request{nil, httptest.NewRequest("GET", "/", nil)} {
|
||||
rec := httptest.NewRecorder()
|
||||
WriteProblem(rec, req, http.StatusTooManyRequests, "")
|
||||
var body map[string]any
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
||||
t.Fatalf("body %q: %v", rec.Body, err)
|
||||
}
|
||||
want := map[string]any{
|
||||
"type": "about:blank",
|
||||
"status": float64(http.StatusTooManyRequests),
|
||||
"title": "Too Many Requests",
|
||||
}
|
||||
if len(body) != len(want) {
|
||||
t.Errorf("request %v: body = %v; want %v", req != nil, body, want)
|
||||
}
|
||||
for k, v := range want {
|
||||
if body[k] != v {
|
||||
t.Errorf("request %v: %s = %v; want %v", req != nil, k, body[k], v)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequestID(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
if id := RequestID(ctx); id != "" {
|
||||
t.Errorf("RequestID(empty) = %q; want empty", id)
|
||||
}
|
||||
ctx = WithRequestID(ctx, "abc")
|
||||
if id := RequestID(ctx); id != "abc" {
|
||||
t.Errorf("RequestID = %q; want abc", id)
|
||||
}
|
||||
// The latest ID wins; a value of another type under the key is ignored.
|
||||
if id := RequestID(WithRequestID(ctx, "def")); id != "def" {
|
||||
t.Errorf("RequestID after replacing = %q; want def", id)
|
||||
}
|
||||
if id := RequestID(context.WithValue(ctx, requestIDKey, 42)); id != "" {
|
||||
t.Errorf("RequestID(int) = %q; want empty", id)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// Package httpx holds small HTTP helpers shared by middleware and handlers.
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Problem is an RFC 9457 problem details body.
|
||||
type Problem struct {
|
||||
Type string `json:"type,omitempty"`
|
||||
Status int `json:"status"`
|
||||
Title string `json:"title"`
|
||||
Detail string `json:"detail,omitempty"`
|
||||
Instance string `json:"instance,omitempty"`
|
||||
RequestID string `json:"requestId,omitempty"`
|
||||
}
|
||||
|
||||
// WriteProblem writes a problem+json error response.
|
||||
func WriteProblem(w http.ResponseWriter, r *http.Request, status int, detail string) {
|
||||
p := Problem{
|
||||
Type: "about:blank",
|
||||
Title: http.StatusText(status),
|
||||
Status: status,
|
||||
Detail: detail,
|
||||
}
|
||||
if r != nil {
|
||||
p.RequestID = RequestID(r.Context())
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/problem+json")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.WriteHeader(status)
|
||||
json.NewEncoder(w).Encode(p) //nolint:errcheck,gosec // the client may have gone; nothing to do
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type ctxKey int
|
||||
|
||||
const requestIDKey ctxKey = iota
|
||||
|
||||
// WithRequestID stores a request ID in the context.
|
||||
func WithRequestID(ctx context.Context, id string) context.Context {
|
||||
return context.WithValue(ctx, requestIDKey, id)
|
||||
}
|
||||
|
||||
// RequestID returns the request ID stored in ctx, or "".
|
||||
func RequestID(ctx context.Context) string {
|
||||
id, _ := ctx.Value(requestIDKey).(string)
|
||||
return id
|
||||
}
|
||||
Reference in new issue
Block a user