💾 Archived View for iich.space › src › app › admin › index.ts captured on 2021-12-03 at 14:04:38.

View Raw

More Information

-=-=-=-=-=-=-

import { Status } from '@/gemini';
import { createLogger } from '@/log';
import { Handler, Router } from '@/mission-control';

import createSessionHandler from '~/middleware/createSessionHandler';
import withCertificate from '~/middleware/withCertificate';
import AdminLandingPage from '~/views/admin/AdminLandingPage';

import bans from './bans';
import boards from './boards';
import posts from './posts';

const FINGERPRINT_WHITELIST = process.env.ADMIN_FINGERPRINT!.split(',');

const router = new Router<Handler>();

const withState = createSessionHandler({
  isAdmin: false,
});

const log = createLogger();

router.use(
  '*',
  withCertificate,
  withState((req, res, state) => {
    if (state.isAdmin === false) {
      if (FINGERPRINT_WHITELIST.includes(req.fingerprint!)) {
        state.isAdmin = true;
      } else {
        log.error('failed admin request', req.remote, req.fingerprint);
        res.sendStatus(Status.NOT_FOUND);
        return res.end();
      }
    }
  }),
);

router.use('/', (_, res, { tb }) => {
  res.send(tb.include(AdminLandingPage));
  res.end();
});

router.use('/boards', boards);
router.use('/posts', posts);
router.use('/bans', bans);

export default router;