summaryrefslogtreecommitdiff
path: root/src/server/web/index.ts
blob: 376aadda730432f33cfff2c2ca582ef50faf2a91 (plain)
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
/**
 * Web Client Server
 */

import ms = require('ms');
import * as Koa from 'koa';
import * as Router from 'koa-router';
import * as send from 'koa-send';
import * as favicon from 'koa-favicon';

import docs from './docs';

const client = `${__dirname}/../../client/`;

// Init app
const app = new Koa();

// Serve favicon
app.use(favicon(`${client}/assets/favicon.ico`));

// Common request handler
app.use(async (ctx, next) => {
	// IFrameの中に入れられないようにする
	ctx.set('X-Frame-Options', 'DENY');
	await next();
});

// Init router
const router = new Router();

//#region static assets

router.get('/assets/*', async ctx => {
	await send(ctx, ctx.path, {
		root: client,
		maxage: ms('7 days'),
		immutable: true
	});
});

// Apple touch icon
router.get('/apple-touch-icon.png', async ctx => {
	await send(ctx, `${client}/assets/apple-touch-icon.png`);
});

// ServiceWroker
router.get(/^\/sw\.(.+?)\.js$/, async ctx => {
	await send(ctx, `${client}/assets/sw.${ctx.params[0]}.js`);
});

// Manifest
router.get('/manifest.json', async ctx => {
	await send(ctx, `${client}/assets/manifest.json`);
});

//#endregion

// Docs
router.use('/docs', docs.routes());

// URL preview endpoint
router.get('url', require('./url-preview'));

// Render base html for all requests
router.get('*', async ctx => {
	await send(ctx, `app/base.html`, {
		root: client,
		maxage: ms('3 days'),
		immutable: true
	});
});

// Register router
app.use(router.routes());

module.exports = app;