monkeeShark/src/server/web/index.ts

247 lines
5.2 KiB
TypeScript
Raw Normal View History

2016-12-28 22:49:51 +00:00
/**
2018-03-29 11:32:18 +00:00
* Web Client Server
2016-12-28 22:49:51 +00:00
*/
2019-01-12 02:27:23 +00:00
import * as os from 'os';
2017-01-02 21:03:19 +00:00
import ms = require('ms');
2018-04-12 21:06:18 +00:00
import * as Koa from 'koa';
import * as Router from 'koa-router';
import * as send from 'koa-send';
import * as favicon from 'koa-favicon';
import * as views from 'koa-views';
2018-12-26 09:32:16 +00:00
import { ObjectID } from 'mongodb';
2016-12-28 22:49:51 +00:00
2018-04-13 03:05:24 +00:00
import docs from './docs';
import packFeed from './feed';
import User from '../../models/user';
2018-07-07 10:19:00 +00:00
import parseAcct from '../../misc/acct/parse';
import config from '../../config';
import Note, { pack as packNote } from '../../models/note';
2018-07-07 10:19:00 +00:00
import getNoteSummary from '../../misc/get-note-summary';
import fetchMeta from '../../misc/fetch-meta';
2019-01-12 02:27:23 +00:00
import Emoji from '../../models/emoji';
import * as pkg from '../../../package.json';
2018-04-13 03:05:24 +00:00
2018-04-12 22:34:27 +00:00
const client = `${__dirname}/../../client/`;
2018-03-29 11:32:18 +00:00
2018-04-12 21:06:18 +00:00
// Init app
const app = new Koa();
2016-12-28 22:49:51 +00:00
// Init renderer
app.use(views(__dirname + '/views', {
extension: 'pug',
options: {
config
}
}));
2018-04-12 21:06:18 +00:00
// Serve favicon
app.use(favicon(`${client}/assets/favicon.ico`));
2016-12-28 22:49:51 +00:00
2018-04-12 21:06:18 +00:00
// Common request handler
2018-04-12 22:34:27 +00:00
app.use(async (ctx, next) => {
2018-04-12 21:06:18 +00:00
// IFrameの中に入れられないようにする
ctx.set('X-Frame-Options', 'DENY');
2018-04-12 22:34:27 +00:00
await next();
2016-12-28 22:49:51 +00:00
});
2018-04-12 21:06:18 +00:00
// Init router
const router = new Router();
2018-03-29 11:32:18 +00:00
//#region static assets
2018-04-12 22:34:27 +00:00
router.get('/assets/*', async ctx => {
2019-01-22 12:42:05 +00:00
await send(ctx as any, ctx.path, {
2018-04-12 22:34:27 +00:00
root: client,
2018-04-12 21:06:18 +00:00
maxage: ms('7 days'),
immutable: true
});
});
// Apple touch icon
router.get('/apple-touch-icon.png', async ctx => {
2019-01-22 12:42:05 +00:00
await send(ctx as any, '/assets/apple-touch-icon.png', {
2018-05-04 08:59:51 +00:00
root: client
});
2017-11-28 05:07:00 +00:00
});
2016-12-28 22:49:51 +00:00
2018-09-03 09:58:26 +00:00
// ServiceWorker
2018-06-17 07:47:37 +00:00
router.get(/^\/sw\.(.+?)\.js$/, async ctx => {
2019-01-22 12:42:05 +00:00
await send(ctx as any, `/assets/sw.${ctx.params[0]}.js`, {
2018-06-17 07:47:37 +00:00
root: client
});
});
2017-11-20 18:40:09 +00:00
2018-03-29 11:32:18 +00:00
// Manifest
2018-04-12 21:06:18 +00:00
router.get('/manifest.json', async ctx => {
2019-01-22 12:42:05 +00:00
await send(ctx as any, '/assets/manifest.json', {
2018-05-04 08:59:51 +00:00
root: client
});
2018-04-12 21:06:18 +00:00
});
2017-02-21 19:19:53 +00:00
2018-03-29 11:32:18 +00:00
//#endregion
2017-11-20 18:40:09 +00:00
2018-04-12 21:06:18 +00:00
// Docs
2018-04-13 03:05:24 +00:00
router.use('/docs', docs.routes());
2018-04-12 21:06:18 +00:00
// URL preview endpoint
2018-04-21 10:02:12 +00:00
router.get('/url', require('./url-preview'));
2018-03-29 11:32:18 +00:00
const getFeed = async (acct: string) => {
const { username, host } = parseAcct(acct);
const user = await User.findOne({
usernameLower: username.toLowerCase(),
host
});
return user && await packFeed(user);
};
// Atom
router.get('/@:user.atom', async ctx => {
const feed = await getFeed(ctx.params.user);
if (feed) {
ctx.set('Content-Type', 'application/atom+xml; charset=utf-8');
ctx.body = feed.atom1();
} else {
ctx.status = 404;
}
});
// RSS
router.get('/@:user.rss', async ctx => {
const feed = await getFeed(ctx.params.user);
if (feed) {
ctx.set('Content-Type', 'application/rss+xml; charset=utf-8');
ctx.body = feed.rss2();
} else {
ctx.status = 404;
}
});
// JSON
router.get('/@:user.json', async ctx => {
const feed = await getFeed(ctx.params.user);
if (feed) {
ctx.set('Content-Type', 'application/json; charset=utf-8');
ctx.body = feed.json1();
} else {
ctx.status = 404;
}
});
//#region for crawlers
// User
2018-05-13 08:00:34 +00:00
router.get('/@:user', async (ctx, next) => {
const { username, host } = parseAcct(ctx.params.user);
const user = await User.findOne({
usernameLower: username.toLowerCase(),
host
});
if (user != null) {
2019-02-06 13:27:23 +00:00
const meta = await fetchMeta();
await ctx.render('user', {
user,
instanceName: meta.name
});
2018-11-19 20:29:51 +00:00
ctx.set('Cache-Control', 'public, max-age=180');
} else {
2018-05-13 08:00:34 +00:00
// リモートユーザーなので
await next();
}
});
router.get('/users/:user', async ctx => {
if (!ObjectID.isValid(ctx.params.user)) {
ctx.status = 404;
return;
}
const userId = new ObjectID(ctx.params.user);
const user = await User.findOne({
_id: userId,
host: null
});
if (user === null) {
ctx.status = 404;
return;
}
ctx.redirect(`/@${user.username}${ user.host == null ? '' : '@' + user.host}`);
});
// Note
router.get('/notes/:note', async ctx => {
2018-12-26 09:32:16 +00:00
if (ObjectID.isValid(ctx.params.note)) {
const note = await Note.findOne({ _id: ctx.params.note });
if (note) {
const _note = await packNote(note);
2019-02-06 13:27:23 +00:00
const meta = await fetchMeta();
2018-12-26 09:32:16 +00:00
await ctx.render('note', {
note: _note,
2019-02-06 13:27:23 +00:00
summary: getNoteSummary(_note),
instanceName: meta.name
2018-12-26 09:32:16 +00:00
});
if (['public', 'home'].includes(note.visibility)) {
ctx.set('Cache-Control', 'public, max-age=180');
} else {
ctx.set('Cache-Control', 'private, max-age=0, must-revalidate');
}
2018-12-26 10:58:04 +00:00
2018-12-26 09:32:16 +00:00
return;
}
}
2018-12-26 09:32:16 +00:00
ctx.status = 404;
});
//#endregion
2019-01-12 02:27:23 +00:00
router.get('/info', async ctx => {
const meta = await fetchMeta();
const emojis = await Emoji.find({ host: null }, {
fields: {
_id: false
}
});
await ctx.render('info', {
version: pkg.version,
machine: os.hostname(),
os: os.platform(),
node: process.version,
cpu: {
model: os.cpus()[0].model,
cores: os.cpus().length
},
emojis: emojis,
meta: meta
});
});
2019-01-20 10:10:19 +00:00
const override = (source: string, target: string, depth: number = 0) =>
[, ...target.split('/').filter(x => x), ...source.split('/').filter(x => x).splice(depth)].join('/');
router.get('/othello', async ctx => ctx.redirect(override(ctx.URL.pathname, 'games/reversi', 1)));
router.get('/reversi', async ctx => ctx.redirect(override(ctx.URL.pathname, 'games')));
2018-03-29 11:32:18 +00:00
// Render base html for all requests
2018-04-12 21:06:18 +00:00
router.get('*', async ctx => {
const meta = await fetchMeta();
await ctx.render('base', {
img: meta.bannerUrl
2017-05-17 20:06:55 +00:00
});
ctx.set('Cache-Control', 'public, max-age=300');
2017-05-17 20:06:55 +00:00
});
2016-12-28 22:49:51 +00:00
2018-04-12 21:06:18 +00:00
// Register router
app.use(router.routes());
2016-12-28 22:49:51 +00:00
module.exports = app;