|
| 1 | +import { env } from '$env/dynamic/public'; |
| 2 | +import { |
| 3 | + PUBLIC_DISCORD_INVITE_URL, |
| 4 | + PUBLIC_GITHUB_PROJECT_URL, |
| 5 | + PUBLIC_SITE_DESCRIPTION, |
| 6 | + PUBLIC_SITE_NAME, |
| 7 | +} from '$env/static/public'; |
| 8 | +import { isTruthy } from '$lib/utils/parse'; |
| 9 | +import { paths } from '$lib/utils/public-routes'; |
| 10 | +import { getBackendURL, getSiteURL } from '$lib/utils/url'; |
| 11 | +import { error } from '@sveltejs/kit'; |
| 12 | +import type { RequestHandler } from './$types'; |
| 13 | + |
| 14 | +// Internal-only top-level segments we don't want to disclose to crawlers. |
| 15 | +const HIDDEN_SEGMENTS: ReadonlySet<string> = new Set(['admin', 'hangfire']); |
| 16 | + |
| 17 | +const SWAGGER_VERSIONS = [1, 2] as const; |
| 18 | + |
| 19 | +export const GET: RequestHandler = ({ setHeaders }) => { |
| 20 | + if (isTruthy(env.PUBLIC_DISABLE_LLMS_TXT)) error(404); |
| 21 | + |
| 22 | + setHeaders({ |
| 23 | + 'content-type': 'text/plain; charset=utf-8', |
| 24 | + 'cache-control': 'public, max-age=3600', |
| 25 | + }); |
| 26 | + |
| 27 | + const name = PUBLIC_SITE_NAME.trim(); |
| 28 | + const description = PUBLIC_SITE_DESCRIPTION.trim(); |
| 29 | + const isOpenShock = name.toLowerCase() === 'openshock'; |
| 30 | + |
| 31 | + const summary = isOpenShock |
| 32 | + ? `OpenShock — ${description}` |
| 33 | + : `${name} — an independent instance of OpenShock — ${description}`; |
| 34 | + |
| 35 | + // Public, linkable routes grouped by role. |
| 36 | + const publicPaths = paths |
| 37 | + .filter((p) => !p.categories.includes('app') && p.parameters.length === 0) |
| 38 | + .toSorted((a, b) => a.path.localeCompare(b.path)); |
| 39 | + |
| 40 | + const generalPages = publicPaths.filter((p) => p.categories.length === 0); |
| 41 | + const authPages = publicPaths.filter((p) => p.categories.includes('auth')); |
| 42 | + |
| 43 | + const renderLinks = (list: typeof publicPaths) => |
| 44 | + list.map((p) => `- [${p.path}](${getSiteURL(p.path).href})`).join('\n'); |
| 45 | + |
| 46 | + const generalList = renderLinks(generalPages); |
| 47 | + const authList = renderLinks(authPages); |
| 48 | + |
| 49 | + // OpenAPI specs, one per backend API version. |
| 50 | + const apiURL = getBackendURL(); |
| 51 | + const swaggerList = SWAGGER_VERSIONS.map( |
| 52 | + (v) => `- [v${v} OpenAPI spec](${new URL(`/swagger/${v}/swagger.json`, apiURL).href})` |
| 53 | + ).join('\n'); |
| 54 | + |
| 55 | + // Authenticated app routes — listed for crawlers with a "not indexable" notice. |
| 56 | + const appPaths = paths |
| 57 | + .filter((p) => p.categories.includes('app')) |
| 58 | + .filter((p) => !HIDDEN_SEGMENTS.has(p.path.split('/')[1] ?? '')); |
| 59 | + |
| 60 | + const appSegments = [ |
| 61 | + ...new Set( |
| 62 | + appPaths.map((p) => p.path.split('/')[1]).filter((seg): seg is string => Boolean(seg)) |
| 63 | + ), |
| 64 | + ] |
| 65 | + .sort() |
| 66 | + .map((seg) => `\`/${seg}\``) |
| 67 | + .join(', '); |
| 68 | + |
| 69 | + const appList = appPaths |
| 70 | + .map((p) => p.path) |
| 71 | + .toSorted() |
| 72 | + .map((path) => `- ${path}`) |
| 73 | + .join('\n'); |
| 74 | + |
| 75 | + const body = `# ${name} |
| 76 | +
|
| 77 | +> ${summary} |
| 78 | +
|
| 79 | +${name} is the web frontend of the OpenShock platform. Authenticated users manage \ |
| 80 | +hubs (ESP32-based bridges), shockers, share permissions, API tokens, and live control \ |
| 81 | +sessions. Unauthenticated visitors can flash firmware to a device over WebSerial or sign \ |
| 82 | +up for an account. |
| 83 | +
|
| 84 | +## Public pages |
| 85 | +
|
| 86 | +${generalList} |
| 87 | +
|
| 88 | +## Authentication pages |
| 89 | +
|
| 90 | +${authList} |
| 91 | +
|
| 92 | +## External resources |
| 93 | +
|
| 94 | +- [openshock.org](https://openshock.org): project website |
| 95 | +- [wiki.openshock.org](https://wiki.openshock.org): user and developer documentation |
| 96 | +- [GitHub](${PUBLIC_GITHUB_PROJECT_URL}): source code and issues |
| 97 | +- [Discord](${PUBLIC_DISCORD_INVITE_URL}): community chat and support |
| 98 | +
|
| 99 | +## Backend API |
| 100 | +
|
| 101 | +${swaggerList} |
| 102 | +
|
| 103 | +## Notes for crawlers |
| 104 | +
|
| 105 | +Routes under ${appSegments} require authentication and contain user-specific \ |
| 106 | +data — not indexable: |
| 107 | +
|
| 108 | +${appList} |
| 109 | +`; |
| 110 | + |
| 111 | + return new Response(body); |
| 112 | +}; |
0 commit comments