function serveAsset($request, $assets, String $assetPath) { set $served = http_serve_static_as( $request, $assets, $assetPath, 200, "public, max-age=300" ); if ($served = false) { http_respond($request, 404, "text/plain; charset=utf-8", "Not Found"); } return 0; } function serveStatus($request) { http_respond( $request, 200, "application/json; charset=utf-8", json_encode([ "runtime_stats": runtime_stats(), "profile_snapshot": profile_snapshot() ]) ); return 0; } function serveHealth($request) { http_respond( $request, 200, "text/plain; charset=utf-8", "ok" ); return 0; } function serveRobots($request) { set String $body = "User-agent: *\n"; $body = $body + "Allow: /\n\n"; $body = $body + "Sitemap: https://graylanguage.com/sitemap.xml\n"; http_respond_headers( $request, 200, "text/plain; charset=utf-8", $body, ["Cache-Control": "public, max-age=3600"] ); return 0; } function serveSitemap($request) { set String $body = "\n"; $body = $body + "\n"; $body = $body + " https://graylanguage.com/\n"; $body = $body + "\n"; http_respond_headers( $request, 200, "application/xml; charset=utf-8", $body, ["Cache-Control": "public, max-age=300"] ); return 0; } function String docsRoot() { set String $root = env_get("GRAY_DOCS_ROOT"); if ($root = "") { return "developer-docs"; } return $root; } set String $root = docsRoot(); set $assets = static_open($root, 33554432); set $examples = static_open($root + "/../examples", 8388608); set String $host = env_get("GRAY_DOCS_HOST"); if ($host = "") { $host = "127.0.0.1"; } set $server = tcp_listen($host, env_int("GRAY_DOCS_PORT", 8790)); http_configure($server, 1024, 30000, 2097152); http_handler($server, "GET", "/", "serveAsset", [$assets, "/public/index.html"]); http_handler($server, "HEAD", "/", "serveAsset", [$assets, "/public/index.html"]); http_handler($server, "GET", "/styles.css", "serveAsset", [$assets, "/public/styles.css"]); http_handler($server, "HEAD", "/styles.css", "serveAsset", [$assets, "/public/styles.css"]); http_handler($server, "GET", "/app.js", "serveAsset", [$assets, "/public/app.js"]); http_handler($server, "HEAD", "/app.js", "serveAsset", [$assets, "/public/app.js"]); http_handler($server, "GET", "/favicon.svg", "serveAsset", [$assets, "/public/favicon.svg"]); http_handler($server, "HEAD", "/favicon.svg", "serveAsset", [$assets, "/public/favicon.svg"]); http_handler( $server, "GET", "/grayworth-symbol.svg", "serveAsset", [$assets, "/public/grayworth-symbol.svg"] ); http_handler( $server, "HEAD", "/grayworth-symbol.svg", "serveAsset", [$assets, "/public/grayworth-symbol.svg"] ); http_handler( $server, "GET", "/instrument-sans.ttf", "serveAsset", [$assets, "/public/instrument-sans.ttf"] ); http_handler( $server, "HEAD", "/instrument-sans.ttf", "serveAsset", [$assets, "/public/instrument-sans.ttf"] ); http_handler($server, "GET", "/robots.txt", "serveRobots", []); http_handler($server, "HEAD", "/robots.txt", "serveRobots", []); http_handler($server, "GET", "/sitemap.xml", "serveSitemap", []); http_handler($server, "HEAD", "/sitemap.xml", "serveSitemap", []); http_handler($server, "GET", "/api/status", "serveStatus", []); http_handler($server, "GET", "/api/health", "serveHealth", []); http_handler( $server, "GET", "/api/reference", "serveAsset", [$assets, "/generated/api.json"] ); http_handler( $server, "GET", "/tutorials/01-template.gy", "serveAsset", [$assets, "/tutorials/01-template.gy"] ); http_handler( $server, "GET", "/tutorials/02-functions-and-collections.gy", "serveAsset", [$assets, "/tutorials/02-functions-and-collections.gy"] ); http_handler( $server, "GET", "/tutorials/03-classes.gy", "serveAsset", [$assets, "/tutorials/03-classes.gy"] ); http_handler( $server, "GET", "/tutorials/04-tasks-and-errors.gy", "serveAsset", [$assets, "/tutorials/04-tasks-and-errors.gy"] ); http_handler( $server, "GET", "/tutorials/05-schemas.gy", "serveAsset", [$assets, "/tutorials/05-schemas.gy"] ); http_handler( $server, "GET", "/tutorials/06-sqlite.gy", "serveAsset", [$assets, "/tutorials/06-sqlite.gy"] ); http_handler( $server, "GET", "/tutorials/07-includes.gy", "serveAsset", [$assets, "/tutorials/07-includes.gy"] ); http_handler( $server, "GET", "/tutorials/08-cache.gy", "serveAsset", [$assets, "/tutorials/08-cache.gy"] ); http_handler( $server, "GET", "/tutorials/09-observability.gy", "serveAsset", [$assets, "/tutorials/09-observability.gy"] ); http_handler( $server, "GET", "/tutorials/10-typed-sessions.gy", "serveAsset", [$assets, "/tutorials/10-typed-sessions.gy"] ); http_handler( $server, "GET", "/tutorials/11-session-fabric.gy", "serveAsset", [$assets, "/tutorials/11-session-fabric.gy"] ); http_handler( $server, "GET", "/guides/sessions.md", "serveAsset", [$assets, "/guides/sessions.md"] ); http_handler( $server, "GET", "/guides/session-storage.md", "serveAsset", [$assets, "/guides/session-storage.md"] ); http_handler( $server, "GET", "/guides/session-key-rotation.md", "serveAsset", [$assets, "/guides/session-key-rotation.md"] ); http_handler( $server, "GET", "/guides/session-privacy.md", "serveAsset", [$assets, "/guides/session-privacy.md"] ); http_handler( $server, "GET", "/session-demo/source.gy", "serveAsset", [$examples, "/session-fabric/app.gy"] ); http_handler($server, "GET", "/source/server.gy", "serveAsset", [$assets, "/src/main.gy"]); while (true) { http_run($server,-1); }