Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions backend/crates/station/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ toml.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
serde_json.workspace = true
futures-util = { version = "0.3", default-features = false, features = ["std", "sink"] }
tower = "0.5.3"
axum = { version = "0.8.9", features = ["ws", "http1", "http2"] }
tower-http = { version = "0.6.11", features = ["cors", "set-header"] }

[dev-dependencies]
tokio = { workspace = true, features = ["test-util"] }

[lints]
workspace = true
12 changes: 7 additions & 5 deletions backend/crates/station/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ El backend de la Control Station de Hyperloop UPV: escucha a las placas del
pod, decodifica sus paquetes, los reenvía a los frontends y guarda un
registro en disco. Construido sobre [`software-core`](../core/README.md).

Ver [`../../ARCHITECTURE.md`](../../ARCHITECTURE.md) para el diseño completo.
La librería ([`lib.rs`](src/lib.rs)) expone los módulos [HTTP](src/http/README.md)
y [WebSocket](src/websocket/README.md) como `station::http` y
`station::websocket`.

## Arrancar
## Ejecutar la demo

```sh
cargo run -p station -- --config ./dev-config.toml
cargo run -p station -- "Hyperloop UPV"
```

*(Pendiente: crear `dev-config.toml` y `config.toml`, portados de
`backend_old/cmd/`.)*
El binario ([`main.rs`](src/main.rs)) ejecuta la [demo de saludo](../../readme-demo.md).
El arranque del backend y la carga de la configuración quedan pendientes.

122 changes: 23 additions & 99 deletions backend/crates/station/src/http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ from the frontend. It does not load the ADJ or handle WebSocket messages.

| File | Responsibility |
| --- | --- |
| [`../http.rs`](../http.rs) | Module entry points, with or without a WebSocket consumer. |
| [`configure.rs`](configure.rs) | Provisional HTTP configuration, validation, server preparation, and configuration tests. |
| [`server.rs`](server.rs) | Router construction, server execution, and route/WebSocket tests. |
| [`../http.rs`](../http.rs) | Server entry point, with an optional WebSocket consumer. |
| [`server.rs`](server.rs) | Router construction and HTTP and WebSocket tests. |
| [`handlers.rs`](handlers.rs) | Shared state, pre-serialized catalog responses, and WebSocket handoff. |

The response models and transformations from `Adj` belong to
[`software_core::adj`](../../../core/src/adj.rs). Their JSON examples are in
[`core/src/adj/http/example`](../../../core/src/adj/http/example).
The server builds catalog responses from the given `Adj`. The
[HTTP models README](../../../core/src/model/http/README.md) contains the JSON examples.

## Routes

Expand All @@ -23,7 +21,7 @@ The response models and transformations from `Adj` belong to
| GET | `/backend/adj/DataPackets` | Boards and data packet definitions, without measurement protections. |
| GET | `/backend/adj/OrderPackets` | Boards and order definitions, including parameter protections. |
| GET | `/backend/adj/ProtectionPackets` | Flat protection catalog and protection-type display metadata. |
| GET with WebSocket upgrade headers | `/backend` | Upgrades the connection and hands the socket to the configured channel. |
| GET with WebSocket upgrade headers | `/backend/ws` | Upgrades the connection and hands the socket to the configured channel. |

Catalog responses return `200 OK` with `Content-Type: application/json`.

Expand All @@ -39,33 +37,29 @@ Each request clones the relevant `Bytes` handle and sends the existing JSON
buffer. It does not rebuild the catalog, copy the whole buffer, or serialize
the response again. No application-level mutex is needed for these reads.

The catalogs are a snapshot of the ADJ at startup; they are not refreshed
automatically. Serialization errors are propagated before requests are served.
This in-memory reuse is separate from browser caching. `configure_server`
applies the configured `Cache-Control` policy to the catalog routes. The older
listener-based entry points do not set an explicit cache policy.
The catalogs represent the ADJ loaded at startup. Serialization errors are
returned before requests are served. The router sets `Cache-Control: no-cache`
and uses permissive CORS.

## Starting the server

The caller supplies a bound `tokio::net::TcpListener` and an already-loaded
`&Adj`. The listener determines the address and port. Both entry points are
async and run until the server stops.
The caller passes a bound `tokio::net::TcpListener` and an already-loaded
`&Adj` to `station::http::serve`. The listener determines the address and port.
The function runs until the server stops.

Without a WebSocket consumer:
Pass `None` to serve the catalogs by themselves.

```rust
http::serve(listener, &adj).await?;
station::http::serve(listener, &adj, None).await?;
```

With a bounded `tokio::sync::mpsc::Sender<axum::extract::ws::WebSocket>` whose
receiver belongs to the future WebSocket module:
To enable WebSocket upgrades, pass the sender of a bounded `tokio::sync::mpsc`
channel and give its receiver to [`station::websocket::run`](../websocket/README.md).

```rust
http::serve_with_websocket(listener, &adj, Some(sender)).await?;
station::http::serve(listener, &adj, Some(sender)).await?;
```

Passing `None` to `serve_with_websocket` has the same behavior as `serve`.

## WebSocket handoff

The HTTP handler only performs the handshake and transfers ownership of the
Expand All @@ -80,87 +74,17 @@ Before approving an upgrade, it reserves a slot in the bounded channel:
capacity, it returns `503 Service Unavailable` instead of upgrading.
- Invalid upgrade requests are rejected by Axum's `WebSocketUpgrade` extractor.

The future WebSocket module must consume the receiver and manage each socket's
lifetime, messages, and closure. Keeping a receiver alive without reading it
does not provide a functioning WebSocket service; the queue will eventually
fill. If the receiver disappears during the handoff, the socket is dropped.

## Configuration

`HttpConfig` is defined provisionally in `configure.rs` and reexported by `http`.
It does not depend on the future application configuration module or read files
and environment variables.

| Field | Purpose |
| --- | --- |
| `bind_addr` | Explicit IP address and port to bind; port zero requests an available port. |
| `allowed_origins` | Exact HTTP(S) origins allowed by CORS; an empty list allows none. |
| `cache_policy` | Catalog policy: `NoStore`, `NoCache`, or `Unspecified`. |

`HttpConfig::new(bind_addr)` uses no allowed CORS origins and defaults to
`CachePolicy::NoStore` (`Cache-Control: no-store`). `NoCache` allows storage
but requires revalidation before reuse; `Unspecified` sets no Cache-Control
header. These policies do not affect the server's reusable JSON buffers and
are not applied to WebSocket upgrades.

The configuration path prepares the server without starting it:

```rust
let mut config = http::HttpConfig::new("127.0.0.1:8080".parse()?);
config.allowed_origins = vec!["http://localhost:9000".into()];

let (listener, router) = http::configure_server(&config, &adj, None).await?;
axum::serve(listener, router).await?;
```

`configure_server` validates origins and serializes the catalogs before binding
the listener. Invalid settings and binding errors are returned to the caller;
there is no fallback to another address or port. To enable socket handoff,
replace `None` with `Some(sender)` as described above.

CORS permits GET and HEAD from explicitly listed origins, handles preflight
requests, and does not enable credentials. Wildcards and origin URLs with
embedded credentials, paths (including a trailing slash), queries, fragments,
or invalid ports are rejected. The opaque origin `null` can be explicitly
listed for local-file clients, but it is not exclusive to Electron: other
opaque origins share it too.

CORS controls browser access to responses; it is not HTTP authentication.
WebSocket Origin validation is still not implemented and is independent of
CORS. The existing `serve` and `serve_with_websocket` entry points remain
available for the current test startup and do not apply `HttpConfig` policies.

## Manual checks

From the backend workspace root, the current `main.rs` test startup can be run
with:

```sh
cargo run -p station --locked
```

It currently loads the ADJ fixture and listens on `127.0.0.1:8080`. These are
temporary test settings, not configuration defaults. It uses `http::serve`, so
valid WebSocket upgrade requests receive `503` until a consumer is integrated.

In another terminal:

```sh
curl -i http://127.0.0.1:8080/backend/adj/DataPackets
curl -i http://127.0.0.1:8080/backend/adj/OrderPackets
curl -i http://127.0.0.1:8080/backend/adj/ProtectionPackets
```
The WebSocket module consumes the receiver and manages each session.
If the receiver disappears during the handoff, the socket is dropped.

## Tests

From the backend workspace root:
Run the HTTP tests from the backend workspace root.

```sh
cargo test -p station http:: --locked
cargo test -p station --lib --locked http::
```

The tests cover catalog contents and repeated responses, empty catalogs,
configuration validation, binding errors, cache headers, CORS and preflight
handling, WebSocket upgrade and socket delivery over a real TCP connection,
unavailable channels (missing, closed, or full), and requests without upgrade
headers.
The tests cover catalog responses, cache headers, CORS, and WebSocket upgrades
over TCP. The end-to-end test checks that a protection event reaches the client
and a validated order reaches the vehicle queue.
Loading
Loading