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 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({

View file

@ -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);

View file

@ -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());
}
}

View file

@ -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
});
});