Skip to content
Merged
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ the same route. See the [organisation profile](https://github.com/OpenBikeNet) f
The protocol is designed in the [protocol repository](https://github.com/OpenBikeNet/protocol).
Ideas and feedback are welcome in its Discussions.

## Public server

A public instance runs at [openbikenet.cloud](https://openbikenet.cloud). Apps connect to
`wss://openbikenet.cloud/v0`, and `https://openbikenet.cloud/v0/stats` shows which apps are on.

## Running it

With Docker (nothing else needed):
Expand Down
66 changes: 66 additions & 0 deletions crates/server/src/landing.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>OpenBikeNet server</title>
<style>
:root {
color-scheme: light dark;
--bg: #f6f6f3; --fg: #1d1d1b; --dim: #6b6b66; --line: #d9d9d3; --accent: #1f7a4d;
}
@media (prefers-color-scheme: dark) {
:root { --bg: #121312; --fg: #e4e4df; --dim: #8d8d86; --line: #2c2d2b; --accent: #5fd08f; }
}
* { box-sizing: border-box; }
body {
margin: 0; background: var(--bg); color: var(--fg);
font: 15px/1.6 ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}
main { max-width: 44rem; margin: 0 auto; padding: 4rem 1.5rem 3rem; }
h1 { font-size: 1.1rem; margin: 0 0 1.5rem; font-weight: 600; }
h1 .dot { color: var(--accent); }
h2 { font-size: 0.8rem; font-weight: 600; color: var(--dim); text-transform: uppercase;
letter-spacing: 0.08em; margin: 2.5rem 0 0.75rem; }
p { margin: 0 0 1rem; }
.dim { color: var(--dim); }
table { border-collapse: collapse; width: 100%; }
td { padding: 0.45rem 0; border-top: 1px solid var(--line); vertical-align: top; }
td:first-child { white-space: nowrap; padding-right: 1.5rem; }
.method { color: var(--dim); display: inline-block; width: 3.5em; }
code { font: inherit; color: var(--fg); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
ul { list-style: none; padding: 0; margin: 0; }
li { padding: 0.2rem 0; }
li::before { content: "→ "; color: var(--dim); }
footer { margin-top: 3rem; padding-top: 1rem; border-top: 1px solid var(--line);
color: var(--dim); font-size: 0.85rem; }
</style>
</head>
<body>
<main>
<h1><span class="dot">●</span> OpenBikeNet server</h1>
<p>A neutral relay that lets riders of different indoor cycling apps meet on the same route.</p>
<p class="dim">This is a backend for apps. There is nothing to use here in a browser.</p>

<h2>Endpoints</h2>
<table>
<tr><td><span class="method">WS</span>/v0</td><td>Protocol v0: presence on shared routes</td></tr>
<tr><td><span class="method">GET</span>/v0/stats</td><td>Connected apps, riders and usage per app</td></tr>
<tr><td><span class="method">GET</span>/healthz</td><td>Liveness check</td></tr>
</table>

<h2>Links</h2>
<ul>
<li><a href="https://openbikenet.cloud">openbikenet.cloud</a> <span class="dim">· the public server</span></li>
<li><a href="https://github.com/OpenBikeNet">github.com/OpenBikeNet</a></li>
<li><a href="https://github.com/OpenBikeNet/protocol/blob/main/spec/v0.md">Protocol specification</a></li>
<li><a href="https://github.com/OpenBikeNet/server">Server source</a> <span class="dim">· run your own with <code>make up</code></span></li>
<li><a href="https://github.com/OpenBikeNet/protocol/discussions">Discussions</a> <span class="dim">· building an app? say hello</span></li>
</ul>

<footer>obn-server {{VERSION}} · AGPL-3.0</footer>
</main>
</body>
</html>
15 changes: 12 additions & 3 deletions crates/server/src/ws.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! WebSocket endpoint `/v0` and the stats endpoint `/v0/stats`.
//! WebSocket endpoint `/v0`, the stats endpoint `/v0/stats` and the landing page at `/`.
//!
//! `presence` is pure and returns addressed deliveries; the [`Hub`] holds one bounded queue per
//! connection and sends them. A connection whose queue is full is dropped, so a slow reader can
Expand All @@ -7,13 +7,13 @@
use std::collections::{HashMap, VecDeque};
use std::net::{IpAddr, SocketAddr};
use std::sync::atomic::{AtomicU16, AtomicU64, Ordering};
use std::sync::{Arc, Mutex, MutexGuard};
use std::sync::{Arc, LazyLock, Mutex, MutexGuard};
use std::time::Duration;

use axum::extract::ws::{CloseFrame, Message, WebSocket, WebSocketUpgrade};
use axum::extract::{ConnectInfo, State};
use axum::http::{header, HeaderMap, StatusCode};
use axum::response::{IntoResponse, Response};
use axum::response::{Html, IntoResponse, Response};
use axum::routing::get;
use axum::{Json, Router};
use futures_util::stream::SplitSink;
Expand Down Expand Up @@ -168,13 +168,22 @@ pub fn spawn_tickers(state: Arc<AppState>) {

pub fn router(state: Arc<AppState>) -> Router {
Router::new()
.route("/", get(landing))
.route("/v0", get(ws_handler))
.route("/v0/stats", get(stats_handler))
.route("/healthz", get(healthz))
.route("/internal/metrics", get(metrics_handler))
.with_state(state)
}

/// A static page for people who open the server's address in a browser.
static LANDING: LazyLock<String> =
LazyLock::new(|| include_str!("landing.html").replace("{{VERSION}}", env!("CARGO_PKG_VERSION")));

async fn landing() -> Html<&'static str> {
Html(LANDING.as_str())
}

async fn healthz() -> &'static str {
"ok"
}
Expand Down
11 changes: 11 additions & 0 deletions crates/server/tests/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,3 +388,14 @@ async fn ticker_expires_riders() {
assert_eq!(l["reason"], "timeout");
assert_eq!(l["id"], "r1");
}

#[tokio::test]
async fn landing_page() {
let addr = start(test_config()).await;
let (head, body) = http_get(addr, "/").await;
assert!(head.starts_with("HTTP/1.1 200"), "{head}");
assert!(head.to_lowercase().contains("content-type: text/html"), "{head}");
assert!(body.contains("/v0/stats"), "{body}");
assert!(body.contains("https://github.com/OpenBikeNet"), "{body}");
assert!(body.contains(env!("CARGO_PKG_VERSION")), "{body}");
}
Loading