All files / app-leaderboard/src/pages routes.ts

96.87% Statements 31/32
100% Branches 4/4
80% Functions 4/5
96.77% Lines 30/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103                                                          11x 11x   11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x       11x 2x 2x 1x             2x       11x 2x 2x 1x             2x         11x                       11x 11x     11x             11x    
import type { Hono } from 'hono';
import { setCookie } from 'hono/cookie';
import { Scalar } from '@scalar/hono-api-reference';
import type { AppEnv } from '../app.ts';
import { isRuleset } from '../rulesets.ts';
import { isLocale, LOCALE_COOKIE } from './i18n/index.ts';
import { aboutPage } from './about.ts';
import { avatarImage } from './avatar.ts';
import { helpPage } from './help.ts';
import { courseScoreCardImage, scoreCardImage } from './card.ts';
import { mountClientScript } from './client-script.ts';
import { chartPage } from './chart.ts';
import { chartsetPage } from './chartset.ts';
import { coursePage } from './course.ts';
import { courseRunPage } from './course-run.ts';
import { mountIcons } from './icons.ts';
import { STYLESHEET_PATH } from './layout.ts';
import { notFound, RULESET_COOKIE_MAX_AGE } from './page.ts';
import { playerPage } from './player.ts';
import { RULESET_COOKIE, safeReturnPath } from './preferences.ts';
import { scorePage } from './score.ts';
import { settingsPage } from './settings.ts';
import { APP_CSS } from './styles.generated.ts';
import { topPage } from './top.ts';
import { loadViewer } from './viewer.ts';
 
/** Server-rendered Leaderboard pages, at the root of the Leaderboard's own host (docs/leaderboard.md section 10). */
export function mountPages(app: Hono<AppEnv>): void {
  // Every page knows who is looking (the header shows the account); assets skip the lookup.
  app.use('/', loadViewer);
  app.use('/*', loadViewer);
 
  app.get('/', topPage);
  app.get('/scores/:id', scorePage);
  app.get('/scores/:id/card', scoreCardImage);
  app.get('/charts/:chartFingerprint', chartPage);
  app.get('/courses/:courseFingerprint', coursePage);
  app.get('/course-scores/:id', courseRunPage);
  app.get('/course-scores/:id/card', courseScoreCardImage);
  app.get('/players/:handle', playerPage);
  app.get('/players/:handle/avatar', avatarImage);
  app.get('/chartsets/:id', chartsetPage);
  app.get('/settings', settingsPage);
  app.get('/about', aboutPage);
  app.get('/help', helpPage);
 
  // Switches the display ruleset and goes back to the page it was switched on. A dedicated URL keeps the setting out of
  // shareable page URLs, so opening someone's link never changes the viewer's setting.
  app.get('/ruleset/:ruleset', (c) => {
    const ruleset = c.req.param('ruleset');
    if (isRuleset(ruleset)) {
      setCookie(c, RULESET_COOKIE, ruleset, {
        path: '/',
        maxAge: RULESET_COOKIE_MAX_AGE,
        sameSite: 'Lax',
        secure: new URL(c.req.url).protocol === 'https:',
      });
    }
    return c.redirect(safeReturnPath(c.req.query('next')), 303);
  });
 
  // Switches the page language, like the ruleset switch: kept in a cookie, then back to the page it was switched on.
  app.get('/lang/:locale', (c) => {
    const locale = c.req.param('locale');
    if (isLocale(locale)) {
      setCookie(c, LOCALE_COOKIE, locale, {
        path: '/',
        maxAge: RULESET_COOKIE_MAX_AGE,
        sameSite: 'Lax',
        secure: new URL(c.req.url).protocol === 'https:',
      });
    }
    return c.redirect(safeReturnPath(c.req.query('next')), 303);
  });
 
  // The API reference, rendered by Scalar from the OpenAPI document. Its script is pinned so the page does not change
  // under us.
  app.get(
    '/api-reference',
    Scalar({
      url: '/api/v0/openapi.json',
      pageTitle: 'Be-Music Leaderboard API',
      cdn: 'https://cdn.jsdelivr.net/npm/@scalar/api-reference@1.72.4',
      // A plain reference: no hosted AI agent and no developer toolbar.
      agent: { disabled: true },
      showDeveloperTools: 'never',
    }),
  );
 
  mountIcons(app);
  mountClientScript(app);
 
  // The compiled stylesheet. Its name carries a content hash, so browsers and caches may keep it forever.
  app.get(STYLESHEET_PATH, (c) =>
    c.body(APP_CSS, 200, {
      'content-type': 'text/css; charset=utf-8',
      'cache-control': 'public, max-age=31536000, immutable',
    }),
  );
 
  app.get('/*', (c) => notFound(c, 'page'));
}