import { Elysia, ValidationError } from 'elysia'; import { html, Html } from '@elysiajs/html'; import { staticPlugin } from '@elysiajs/static'; import { AlreadyExistsError, BadRequestError, CouldNotResolveDidError, KnownInternalServerError, NotAuthenticatedError, NotAuthorizedError, NotImplementedError, ResourceNotFoundError, UpstreamError } from './error'; import { env } from './lib/env'; import { startScheduler } from './lib/backup/scheduler'; import RootLayout from './view/layout/root'; import DashboardPage from './view/page/DashboardPage'; import PdsPage from './view/page/PdsPage'; import TrackedPage from './view/page/TrackedPage'; import AccountsPage from './view/page/AccountsPage'; import WipePage from './view/page/WipePage'; import AuthController from './controller/AuthController'; import BackupController from './controller/BackupController'; import { isAuthed } from './plugin/auth'; const app = new Elysia() .use(html()) .use(staticPlugin({ assets: 'public', prefix: '/' })) .error({ [new NotImplementedError().message]: NotImplementedError, [new NotAuthenticatedError().message]: NotAuthenticatedError, [new NotAuthorizedError().message]: NotAuthorizedError, [new BadRequestError('EXAMPLE').message]: BadRequestError, [new ResourceNotFoundError().message]: ResourceNotFoundError, [new AlreadyExistsError('EXAMPLE').message]: AlreadyExistsError, [new KnownInternalServerError({}).message]: KnownInternalServerError, [new CouldNotResolveDidError().message]: CouldNotResolveDidError, [new UpstreamError('EXAMPLE').message]: UpstreamError }) .onError(({ code, error, path, request }) => { if ( typeof code !== 'number' && [ 'NOT_IMPLEMENTED', 'NOT_AUTHENTICATED', 'NOT_AUTHORIZED', 'BAD_REQUEST', 'RESOURCE_NOT_FOUND', 'ALREADY_EXISTS', 'NOT_FOUND', 'COULD_NOT_RESOLVE_DID', 'UPSTREAM_ERROR' ].includes(code) ) console.debug(error); else if (code === 'KNOWN_INTERNAL_SERVER_ERROR') console.error(error, JSON.stringify((error as KnownInternalServerError).internal)); else console.error(error); if (path.startsWith('/api')) { switch (code) { case 'ALREADY_EXISTS': case 'BAD_REQUEST': case 'UPSTREAM_ERROR': return { code, details: (error as BadRequestError | UpstreamError).details }; case 'VALIDATION': return { code, details: JSON.parse((error as ValidationError).message) }; default: return { code: typeof code === 'string' ? code : code.toString() }; } } return (

an error occurred

looks like there was a problem loading this page.

					{String(code)}
					{'\n'}
					{(error as Error).message ?? ''}
				
); }) .use(DashboardPage) .use(PdsPage) .use(TrackedPage) .use(AccountsPage) .use(WipePage) .use(AuthController) .use(BackupController) .listen(env.PORT); // Start the periodic backup scheduler. This kicks off a backup on boot and // then re-triggers on BACKUP_INTERVAL_MS. startScheduler(); console.log( `safekeeper listening on http://${app.server?.hostname}:${app.server?.port}` );