diff options
Diffstat (limited to 'server/lib/net/app.dart')
| -rw-r--r-- | server/lib/net/app.dart | 50 |
1 files changed, 47 insertions, 3 deletions
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', |
