feat(server): integrate UDP video service and normalize database schema

- Implement UDPVideoService to stream MJPEG video frames from ESP32s via UDP and send control commands.
- Normalize race database schema:
  - Remove redundant driver_ids array from races table (migration 011).
  - Move per-driver outcomes (total_time_ms, best_lap_ms, position) to race_drivers table (migration 012).
  - Add device_id column to lobby_drivers to link active physical devices (migration 013).
- Update WebSocket handler to pass lobbySvc, driversSvc, and videoSvc for driver identity tracking, syncing in-memory driver metadata, and forwarding WS controls to physical cars.
- Add CORS middleware to HTTP routes.
- Regenerate Swagger documentation.
This commit is contained in:
x0gp
2026-07-06 12:38:34 +04:00
parent 978d36c505
commit 49351b4928
19 changed files with 1406 additions and 132 deletions
+140
View File
@@ -948,6 +948,70 @@ const docTemplate = `{
}
}
},
"/api/drivers/{id}/device": {
"put": {
"description": "Dispatches on HTTP method. Path ` + "`" + `/api/drivers/by-nick/{nick}` + "`" + ` is a convenience that returns the driver by 3-letter nickname.",
"produces": [
"application/json"
],
"tags": [
"drivers"
],
"summary": "Get / update / delete a driver by id",
"parameters": [
{
"type": "string",
"description": "Driver id",
"name": "id",
"in": "path",
"required": true
},
{
"description": "Patch payload (only for PUT)",
"name": "driver",
"in": "body",
"schema": {
"$ref": "#/definitions/transport.DriverUpdateRequest"
}
}
],
"responses": {
"200": {
"description": "Driver payload or update result",
"schema": {
"$ref": "#/definitions/transport.Driver"
}
},
"204": {
"description": "Delete ack",
"schema": {
"$ref": "#/definitions/transport.Driver"
}
},
"400": {
"description": "Bad request",
"schema": {
"type": "object",
"additionalProperties": true
}
},
"404": {
"description": "Driver not found",
"schema": {
"type": "object",
"additionalProperties": true
}
},
"500": {
"description": "Internal server error",
"schema": {
"type": "object",
"additionalProperties": true
}
}
}
}
},
"/api/races": {
"get": {
"description": "Returns one keyset page of races. Live races (status lobby|racing) come from in-memory lobby.Service; finished races come from Postgres. The two sources are merged and ordered by (started_ms DESC, id ASC). The cursor is opaque: pass back the next_cursor verbatim from a previous page.\n## Filters\n- ` + "`" + `status` + "`" + ` (comma-separated, e.g. \"racing,finished\"). Recognised values: all, lobby, racing, finished.\n- ` + "`" + `track_id` + "`" + ` (exact match against a physical track id; custom lobbies are not supported in this build).\n- ` + "`" + `limit` + "`" + ` (1..200, default 50)",
@@ -1686,6 +1750,82 @@ const docTemplate = `{
}
}
},
"/api/video/control": {
"post": {
"description": "Sends a text command (e.g. start, stop, left, right) to the ESP32 command port (UDP 8888).",
"produces": [
"application/json"
],
"tags": [
"video"
],
"summary": "Send command to ESP32",
"parameters": [
{
"type": "integer",
"description": "Device ID (0..127)",
"name": "id",
"in": "query"
},
{
"type": "string",
"description": "Command text",
"name": "cmd",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "Command sent",
"schema": {
"type": "object",
"additionalProperties": true
}
},
"400": {
"description": "Missing cmd parameter",
"schema": {
"type": "string"
}
},
"500": {
"description": "Internal error",
"schema": {
"type": "string"
}
}
}
}
},
"/api/video/stream": {
"get": {
"description": "Streams video frames received via UDP as multipart/x-mixed-replace. Suitable for raw img tags in browser.",
"produces": [
"multipart/x-mixed-replace"
],
"tags": [
"video"
],
"summary": "Get MJPEG video stream from ESP32",
"parameters": [
{
"type": "integer",
"description": "Device ID (0..127)",
"name": "id",
"in": "query"
}
],
"responses": {
"200": {
"description": "MJPEG stream",
"schema": {
"type": "string"
}
}
}
}
},
"/health": {
"get": {
"description": "Returns 200 OK with current engine phase, WS connection count and total snapshot drops. Intended for load-balancer health checks.",
+140
View File
@@ -946,6 +946,70 @@
}
}
},
"/api/drivers/{id}/device": {
"put": {
"description": "Dispatches on HTTP method. Path `/api/drivers/by-nick/{nick}` is a convenience that returns the driver by 3-letter nickname.",
"produces": [
"application/json"
],
"tags": [
"drivers"
],
"summary": "Get / update / delete a driver by id",
"parameters": [
{
"type": "string",
"description": "Driver id",
"name": "id",
"in": "path",
"required": true
},
{
"description": "Patch payload (only for PUT)",
"name": "driver",
"in": "body",
"schema": {
"$ref": "#/definitions/transport.DriverUpdateRequest"
}
}
],
"responses": {
"200": {
"description": "Driver payload or update result",
"schema": {
"$ref": "#/definitions/transport.Driver"
}
},
"204": {
"description": "Delete ack",
"schema": {
"$ref": "#/definitions/transport.Driver"
}
},
"400": {
"description": "Bad request",
"schema": {
"type": "object",
"additionalProperties": true
}
},
"404": {
"description": "Driver not found",
"schema": {
"type": "object",
"additionalProperties": true
}
},
"500": {
"description": "Internal server error",
"schema": {
"type": "object",
"additionalProperties": true
}
}
}
}
},
"/api/races": {
"get": {
"description": "Returns one keyset page of races. Live races (status lobby|racing) come from in-memory lobby.Service; finished races come from Postgres. The two sources are merged and ordered by (started_ms DESC, id ASC). The cursor is opaque: pass back the next_cursor verbatim from a previous page.\n## Filters\n- `status` (comma-separated, e.g. \"racing,finished\"). Recognised values: all, lobby, racing, finished.\n- `track_id` (exact match against a physical track id; custom lobbies are not supported in this build).\n- `limit` (1..200, default 50)",
@@ -1684,6 +1748,82 @@
}
}
},
"/api/video/control": {
"post": {
"description": "Sends a text command (e.g. start, stop, left, right) to the ESP32 command port (UDP 8888).",
"produces": [
"application/json"
],
"tags": [
"video"
],
"summary": "Send command to ESP32",
"parameters": [
{
"type": "integer",
"description": "Device ID (0..127)",
"name": "id",
"in": "query"
},
{
"type": "string",
"description": "Command text",
"name": "cmd",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "Command sent",
"schema": {
"type": "object",
"additionalProperties": true
}
},
"400": {
"description": "Missing cmd parameter",
"schema": {
"type": "string"
}
},
"500": {
"description": "Internal error",
"schema": {
"type": "string"
}
}
}
}
},
"/api/video/stream": {
"get": {
"description": "Streams video frames received via UDP as multipart/x-mixed-replace. Suitable for raw img tags in browser.",
"produces": [
"multipart/x-mixed-replace"
],
"tags": [
"video"
],
"summary": "Get MJPEG video stream from ESP32",
"parameters": [
{
"type": "integer",
"description": "Device ID (0..127)",
"name": "id",
"in": "query"
}
],
"responses": {
"200": {
"description": "MJPEG stream",
"schema": {
"type": "string"
}
}
}
}
},
"/health": {
"get": {
"description": "Returns 200 OK with current engine phase, WS connection count and total snapshot drops. Intended for load-balancer health checks.",
+96
View File
@@ -1331,6 +1331,50 @@ paths:
summary: Get / update / delete a driver by id
tags:
- drivers
/api/drivers/{id}/device:
put:
description: Dispatches on HTTP method. Path `/api/drivers/by-nick/{nick}` is
a convenience that returns the driver by 3-letter nickname.
parameters:
- description: Driver id
in: path
name: id
required: true
type: string
- description: Patch payload (only for PUT)
in: body
name: driver
schema:
$ref: '#/definitions/transport.DriverUpdateRequest'
produces:
- application/json
responses:
"200":
description: Driver payload or update result
schema:
$ref: '#/definitions/transport.Driver'
"204":
description: Delete ack
schema:
$ref: '#/definitions/transport.Driver'
"400":
description: Bad request
schema:
additionalProperties: true
type: object
"404":
description: Driver not found
schema:
additionalProperties: true
type: object
"500":
description: Internal server error
schema:
additionalProperties: true
type: object
summary: Get / update / delete a driver by id
tags:
- drivers
/api/races:
get:
description: |-
@@ -1850,6 +1894,58 @@ paths:
summary: Get / update / delete a track by id
tags:
- tracks
/api/video/control:
post:
description: Sends a text command (e.g. start, stop, left, right) to the ESP32
command port (UDP 8888).
parameters:
- description: Device ID (0..127)
in: query
name: id
type: integer
- description: Command text
in: query
name: cmd
required: true
type: string
produces:
- application/json
responses:
"200":
description: Command sent
schema:
additionalProperties: true
type: object
"400":
description: Missing cmd parameter
schema:
type: string
"500":
description: Internal error
schema:
type: string
summary: Send command to ESP32
tags:
- video
/api/video/stream:
get:
description: Streams video frames received via UDP as multipart/x-mixed-replace.
Suitable for raw img tags in browser.
parameters:
- description: Device ID (0..127)
in: query
name: id
type: integer
produces:
- multipart/x-mixed-replace
responses:
"200":
description: MJPEG stream
schema:
type: string
summary: Get MJPEG video stream from ESP32
tags:
- video
/health:
get:
description: Returns 200 OK with current engine phase, WS connection count and