This commit is contained in:
syuilo 2018-04-13 07:34:27 +09:00
parent c54ff5b2bc
commit 61f21594a9
4 changed files with 20 additions and 20 deletions

View file

@ -5,6 +5,7 @@
import * as Koa from 'koa'; import * as Koa from 'koa';
import * as Router from 'koa-router'; import * as Router from 'koa-router';
import * as multer from 'koa-multer'; import * as multer from 'koa-multer';
import * as bodyParser from 'koa-bodyparser';
import endpoints from './endpoints'; import endpoints from './endpoints';
@ -12,6 +13,7 @@ const handler = require('./api-handler').default;
// Init app // Init app
const app = new Koa(); const app = new Koa();
app.use(bodyParser);
// Init multer instance // Init multer instance
const upload = multer({ const upload = multer({

View file

@ -80,10 +80,11 @@ export default function(readable: stream.Readable, type: string, ctx: Koa.Contex
} }
if (ctx.query.download !== undefined) { 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); data.stream.pipe(ctx.res);

View file

@ -4,10 +4,9 @@
import * as fs from 'fs'; import * as fs from 'fs';
import * as http from 'http'; import * as http from 'http';
import * as https from 'https'; import * as http2 from 'http2';
import * as Koa from 'koa'; import * as Koa from 'koa';
import * as Router from 'koa-router'; import * as Router from 'koa-router';
import * as bodyParser from 'koa-bodyparser';
import * as mount from 'koa-mount'; import * as mount from 'koa-mount';
import activityPub from './activitypub'; import activityPub from './activitypub';
@ -17,14 +16,13 @@ import config from '../config';
// Init app // Init app
const app = new Koa(); const app = new Koa();
app.proxy = true; app.proxy = true;
app.use(bodyParser);
// HSTS // HSTS
// 6months (15552000sec) // 6months (15552000sec)
if (config.url.startsWith('https')) { if (config.url.startsWith('https')) {
app.use((ctx, next) => { app.use(async (ctx, next) => {
ctx.set('strict-transport-security', 'max-age=15552000; preload'); 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(activityPub.routes());
router.use(webFinger.routes()); router.use(webFinger.routes());
app.use(mount(require('./web')));
// Register router // Register router
app.use(router.routes()); app.use(router.routes());
app.use(mount(require('./web')));
function createServer() { function createServer() {
if (config.https) { if (config.https) {
const certs = {}; const certs = {};
Object.keys(config.https).forEach(k => { Object.keys(config.https).forEach(k => {
certs[k] = fs.readFileSync(config.https[k]); certs[k] = fs.readFileSync(config.https[k]);
}); });
return https.createServer(certs, app.callback); return http2.createSecureServer(certs, app.callback());
} else { } else {
return http.createServer(app.callback); return http.createServer(app.callback());
} }
} }

View file

@ -2,15 +2,13 @@
* Web Client Server * Web Client Server
*/ */
import * as path from 'path';
import ms = require('ms'); import ms = require('ms');
import * as Koa from 'koa'; import * as Koa from 'koa';
import * as Router from 'koa-router'; import * as Router from 'koa-router';
import * as send from 'koa-send'; import * as send from 'koa-send';
import * as favicon from 'koa-favicon'; import * as favicon from 'koa-favicon';
const client = path.resolve(`${__dirname}/../../client/`); const client = `${__dirname}/../../client/`;
// Init app // Init app
const app = new Koa(); const app = new Koa();
@ -19,10 +17,10 @@ const app = new Koa();
app.use(favicon(`${client}/assets/favicon.ico`)); app.use(favicon(`${client}/assets/favicon.ico`));
// Common request handler // Common request handler
app.use((ctx, next) => { app.use(async (ctx, next) => {
// IFrameの中に入れられないようにする // IFrameの中に入れられないようにする
ctx.set('X-Frame-Options', 'DENY'); ctx.set('X-Frame-Options', 'DENY');
next(); await next();
}); });
// Init router // Init router
@ -30,9 +28,9 @@ const router = new Router();
//#region static assets //#region static assets
router.get('/assets', async ctx => { router.get('/assets/*', async ctx => {
await send(ctx, ctx.path, { await send(ctx, ctx.path, {
root: `${client}/assets`, root: client,
maxage: ms('7 days'), maxage: ms('7 days'),
immutable: true immutable: true
}); });
@ -63,8 +61,9 @@ router.get('url', require('./url-preview'));
// Render base html for all requests // Render base html for all requests
router.get('*', async ctx => { router.get('*', async ctx => {
await send(ctx, `${client}/app/base.html`, { await send(ctx, `app/base.html`, {
maxage: ms('7 days'), root: client,
maxage: ms('3 days'),
immutable: true immutable: true
}); });
}); });