summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/bin/main.dart3
-rw-r--r--server/lib/net/app.dart50
-rw-r--r--server/test/net/app_test.dart9
-rw-r--r--server/test/net/ws_test_helpers.dart1
4 files changed, 60 insertions, 3 deletions
diff --git a/server/bin/main.dart b/server/bin/main.dart
index 7571960..f864828 100644
--- a/server/bin/main.dart
+++ b/server/bin/main.dart
@@ -40,6 +40,9 @@ Future<void> main(List<String> args) async {
port: port,
staticDir: staticDir,
gameServer: gameServer,
+ // The client's menus and artwork read the same assets the engine loads.
+ dataDir: 'assets/data/',
+ imageDir: 'assets/images/',
);
stdout
..writeln('X-Haven server listening on http://localhost:${server.port}')
diff --git a/server/lib/net/app.dart b/server/lib/net/app.dart
index 21a1b81..a38d5c0 100644
--- a/server/lib/net/app.dart
+++ b/server/lib/net/app.dart
@@ -19,13 +19,30 @@ import 'ws_upgrade.dart';
final _log = Logger('server');
/// Builds the HTTP handler for the game server.
-Handler createHandler({required String staticDir, required GameServer gameServer}) {
+///
+/// [dataDir] (e.g. `assets/data/`) is served read-only at `/data/` when
+/// non-null: the client's menus read the campaign/scenario/character/section
+/// JSON from it (the same files the engine loads). [imageDir] (e.g.
+/// `assets/images/`) is served at `/images/` when non-null — character and
+/// monster artwork for the main list.
+Handler createHandler({
+ required String staticDir,
+ required GameServer gameServer,
+ String? dataDir,
+ String? imageDir,
+}) {
final router = Router();
router.get('/ws', createWebSocketUpgradeHandler(gameServer));
router.get('/healthz', (Request request) => Response.ok(
jsonEncode({'status': 'ok'}),
headers: {'content-type': 'application/json; charset=utf-8'},
));
+ if (dataDir != null) {
+ router.mount('/data/', _createStaticHandler(dataDir, plain: true));
+ }
+ if (imageDir != null) {
+ router.mount('/images/', _createStaticHandler(imageDir, plain: true));
+ }
// Session endpoint (PLAN.md section 3.3): returns the caller's persistent
// session id, setting the `x-haven-session` cookie when the caller has
// none yet. Browsers then carry the cookie on every same-origin request,
@@ -53,8 +70,19 @@ Future<HttpServer> startServer({
required int port,
required String staticDir,
required GameServer gameServer,
+ String? dataDir,
+ String? imageDir,
}) {
- return shelf_io.serve(createHandler(staticDir: staticDir, gameServer: gameServer), '0.0.0.0', port);
+ return shelf_io.serve(
+ createHandler(
+ staticDir: staticDir,
+ gameServer: gameServer,
+ dataDir: dataDir,
+ imageDir: imageDir,
+ ),
+ '0.0.0.0',
+ port,
+ );
}
/// The session id from [request]'s cookie, when well-formed.
@@ -71,9 +99,17 @@ String? _validSessionIdFrom(Request request) {
return null;
}
-Handler _createStaticHandler(String staticDir) {
+/// Serves [staticDir] as static files. When [plain] is true (data/image
+/// directories) it is plain static serving with a cache header and a plain
+/// 404; otherwise (the SPA build) a 404 on an extensionless path falls back
+/// to `index.html` so client-side routes resolve.
+Handler _createStaticHandler(String staticDir, {bool plain = false}) {
final directory = Directory(staticDir);
if (!directory.existsSync()) {
+ if (plain) {
+ _log.info('data dir $staticDir not found; serving 404');
+ return (Request request) => Response.notFound('not found');
+ }
_log.info('static dir $staticDir not found; serving stub page');
return (Request request) => Response.ok(
'<!doctype html><meta charset="utf-8">'
@@ -85,6 +121,14 @@ Handler _createStaticHandler(String staticDir) {
'with <code>--static-dir ../web/dist</code>.</p>',
);
}
+ if (plain) {
+ // Plain static serving (no SPA fallback) with long-lived caching —
+ // the JSON and images are immutable build inputs.
+ return createStaticHandler(
+ directory.path,
+ maxAge: const Duration(days: 1),
+ );
+ }
final staticHandler = createStaticHandler(
directory.path,
defaultDocument: 'index.html',
diff --git a/server/test/net/app_test.dart b/server/test/net/app_test.dart
index 4a02c15..a37a8e5 100644
--- a/server/test/net/app_test.dart
+++ b/server/test/net/app_test.dart
@@ -42,6 +42,15 @@ void main() {
expect(jsonDecode(body), {'status': 'ok'});
});
+ test('serves the game data JSON at /data/', () async {
+ final body = await httpGet(
+ server.httpUrl.replace(path: '/data/editions/editions.json'),
+ );
+ final decoded = jsonDecode(body) as Map<String, dynamic>;
+ expect(decoded['editions'], isA<List>());
+ expect(decoded['editions'], contains('Jaws of the Lion'));
+ });
+
test('session endpoint mints a session and sets the cookie', () async {
final client = HttpClient();
addTearDown(client.close);
diff --git a/server/test/net/ws_test_helpers.dart b/server/test/net/ws_test_helpers.dart
index cb8af67..1903567 100644
--- a/server/test/net/ws_test_helpers.dart
+++ b/server/test/net/ws_test_helpers.dart
@@ -63,6 +63,7 @@ class TestGameServer {
port: 0,
staticDir: 'nonexistent-static-dir',
gameServer: instance.gameServer,
+ dataDir: dataDir,
);
return instance;
}