From 61f21594a9edbb3102d6a0f45c1e95ed82c3f606 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 13 Apr 2018 07:34:27 +0900 Subject: [PATCH] wip --- src/server/api/index.ts | 2 ++ src/server/file/pour.ts | 5 +++-- src/server/index.ts | 16 +++++++--------- src/server/web/index.ts | 17 ++++++++--------- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/server/api/index.ts b/src/server/api/index.ts index d2427d30a..c383e1cf8 100644 --- a/src/server/api/index.ts +++ b/src/server/api/index.ts @@ -5,6 +5,7 @@ import * as Koa from 'koa'; import * as Router from 'koa-router'; import * as multer from 'koa-multer'; +import * as bodyParser from 'koa-bodyparser'; import endpoints from './endpoints'; @@ -12,6 +13,7 @@ const handler = require('./api-handler').default; // Init app const app = new Koa(); +app.use(bodyParser); // Init multer instance const upload = multer({ diff --git a/src/server/file/pour.ts b/src/server/file/pour.ts index 2a31cb589..b38b969c2 100644 --- a/src/server/file/pour.ts +++ b/src/server/file/pour.ts @@ -80,10 +80,11 @@ export default function(readable: stream.Readable, type: string, ctx: Koa.Contex } if (ctx.query.download !== undefined) { - ctx.header('Content-Disposition', 'attachment'); + ctx.set('Content-Disposition', 'attachment'); } - ctx.header('Content-Type', data.contentType); + ctx.set('Cache-Control', 'max-age=31536000, immutable'); + ctx.set('Content-Type', data.contentType); data.stream.pipe(ctx.res); diff --git a/src/server/index.ts b/src/server/index.ts index 02362037e..a637e8598 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -4,10 +4,9 @@ import * as fs from 'fs'; import * as http from 'http'; -import * as https from 'https'; +import * as http2 from 'http2'; import * as Koa from 'koa'; import * as Router from 'koa-router'; -import * as bodyParser from 'koa-bodyparser'; import * as mount from 'koa-mount'; import activityPub from './activitypub'; @@ -17,14 +16,13 @@ import config from '../config'; // Init app const app = new Koa(); app.proxy = true; -app.use(bodyParser); // HSTS // 6months (15552000sec) if (config.url.startsWith('https')) { - app.use((ctx, next) => { + app.use(async (ctx, next) => { ctx.set('strict-transport-security', 'max-age=15552000; preload'); - next(); + await next(); }); } @@ -38,20 +36,20 @@ const router = new Router(); router.use(activityPub.routes()); router.use(webFinger.routes()); -app.use(mount(require('./web'))); - // Register router app.use(router.routes()); +app.use(mount(require('./web'))); + function createServer() { if (config.https) { const certs = {}; Object.keys(config.https).forEach(k => { certs[k] = fs.readFileSync(config.https[k]); }); - return https.createServer(certs, app.callback); + return http2.createSecureServer(certs, app.callback()); } else { - return http.createServer(app.callback); + return http.createServer(app.callback()); } } diff --git a/src/server/web/index.ts b/src/server/web/index.ts index b28ad5592..dd296f875 100644 --- a/src/server/web/index.ts +++ b/src/server/web/index.ts @@ -2,15 +2,13 @@ * Web Client Server */ -import * as path from 'path'; 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'; -const client = path.resolve(`${__dirname}/../../client/`); +const client = `${__dirname}/../../client/`; // Init app const app = new Koa(); @@ -19,10 +17,10 @@ const app = new Koa(); app.use(favicon(`${client}/assets/favicon.ico`)); // Common request handler -app.use((ctx, next) => { +app.use(async (ctx, next) => { // IFrameの中に入れられないようにする ctx.set('X-Frame-Options', 'DENY'); - next(); + await next(); }); // Init router @@ -30,9 +28,9 @@ const router = new Router(); //#region static assets -router.get('/assets', async ctx => { +router.get('/assets/*', async ctx => { await send(ctx, ctx.path, { - root: `${client}/assets`, + root: client, maxage: ms('7 days'), immutable: true }); @@ -63,8 +61,9 @@ router.get('url', require('./url-preview')); // Render base html for all requests router.get('*', async ctx => { - await send(ctx, `${client}/app/base.html`, { - maxage: ms('7 days'), + await send(ctx, `app/base.html`, { + root: client, + maxage: ms('3 days'), immutable: true }); });