core: 压缩响应内容

This commit is contained in:
undefined committed 2021-03-27 09:10:42 +08:00
1 parent 33bc45c5ab
commit 1ec793d039
2 files changed
+15 -12

No files matched your search

+2 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "hydrooj", "name": "hydrooj",
"version": "2.20.36", "version": "2.20.37",
"bin": "bin/hydrooj.js", "bin": "bin/hydrooj.js",
"main": "dist/loader.js", "main": "dist/loader.js",
"typings": "dist/loader.d.ts", "typings": "dist/loader.d.ts",
@@ -20,6 +20,7 @@
"js-yaml": "^4.0.0", "js-yaml": "^4.0.0",
"koa": "^2.13.1", "koa": "^2.13.1",
"koa-body": "^4.2.0", "koa-body": "^4.2.0",
"koa-compress": "^5.0.1",
"koa-proxies": "^0.11.0", "koa-proxies": "^0.11.0",
"koa-router": "^10.0.0", "koa-router": "^10.0.0",
"koa-static-cache": "^5.1.3", "koa-static-cache": "^5.1.3",
+13 -11
View File
@@ -10,6 +10,7 @@ import {
import { ObjectID } from 'mongodb'; import { ObjectID } from 'mongodb';
import Koa, { Context } from 'koa'; import Koa, { Context } from 'koa';
import Body from 'koa-body'; import Body from 'koa-body';
import Compress from 'koa-compress';
import Router from 'koa-router'; import Router from 'koa-router';
import proxy from 'koa-proxies'; import proxy from 'koa-proxies';
import cache from 'koa-static-cache'; import cache from 'koa-static-cache';
@@ -230,15 +231,26 @@ export async function prepare() {
changeOrigin: true, changeOrigin: true,
rewrite: (p) => p.replace('/fs', ''), rewrite: (p) => p.replace('/fs', ''),
})); }));
app.use(Compress());
if (argv.public) { if (argv.public) {
app.use(cache(argv.public, { app.use(cache(argv.public, {
maxAge: 0, maxAge: 0,
})); }));
} else { } else {
app.use(cache(resolve(os.tmpdir(), 'hydro', 'public'), { app.use(cache(resolve(os.tmpdir(), 'hydro', 'public'), {
maxAge: 365 * 24 * 60 * 60, maxAge: 30 * 24 * 60 * 60,
})); }));
} }
if (argv.debug) {
app.use(async (ctx: Context, next: Function) => {
const startTime = new Date().getTime();
await next();
const endTime = new Date().getTime();
if (ctx.nolog || ctx.response.headers.nolog) return;
ctx._remoteAddress = ctx.request.ip;
logger.debug(`${ctx.request.method} ${ctx.request.path} ${ctx.response.status} ${endTime - startTime}ms ${ctx.response.length}`);
});
}
app.use(Body({ app.use(Body({
multipart: true, multipart: true,
formidable: { formidable: {
@@ -786,16 +798,6 @@ export function Connection(
// TODO use postInit? // TODO use postInit?
export function start() { export function start() {
const port = system.get('server.port'); const port = system.get('server.port');
if (argv.debug) {
app.use(async (ctx: Context, next: Function) => {
const startTime = new Date().getTime();
await next();
const endTime = new Date().getTime();
if (ctx.response.headers.nolog) return;
ctx._remoteAddress = ctx.request.ip;
logger.debug(`${ctx.request.method} ${ctx.request.path} ${ctx.response.status} ${endTime - startTime}ms ${ctx.response.length}`);
});
}
app.use(router.routes()).use(router.allowedMethods()); app.use(router.routes()).use(router.allowedMethods());
server.listen(argv.port || port); server.listen(argv.port || port);
logger.success('Server listening at: %d', argv.port || port); logger.success('Server listening at: %d', argv.port || port);