core: serve static files without copy

This commit is contained in:
undefined committed 2021-09-15 12:29:18 +08:00
1 parent eef64ff2e0
commit b7348e4be3
5 files changed
+7 -20

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "hydrooj",
"version": "2.34.8",
"version": "2.34.9",
"bin": "bin/hydrooj.js",
"main": "src/loader",
"module": "src/loader",
-4
View File
@@ -5,7 +5,6 @@ import cac from 'cac';
import fs from 'fs-extra';
import Koa, { Context } from 'koa';
import Body from 'koa-body';
import cache from 'koa-static-cache';
import mongodb from 'mongodb';
import { Logger } from '../logger';
@@ -117,9 +116,6 @@ export function load() {
const server = http.createServer(app.callback());
app.keys = ['Hydro'];
app
.use(cache(path.join(os.tmpdir(), 'hydro', 'public'), {
maxAge: 365 * 24 * 60 * 60,
}))
.use(Body())
.use((ctx) => {
if (ctx.request.method.toLowerCase() === 'post') return post(ctx);
+1
View File
@@ -34,4 +34,5 @@ if (!global.Hydro) {
locales: {},
};
global.addons = [];
global.publicDirs = [];
}
+2 -6
View File
@@ -4,7 +4,6 @@
import 'reflect-metadata';
import './init';
import './interface';
import os from 'os';
import path from 'path';
import fs from 'fs-extra';
import './utils';
@@ -24,8 +23,6 @@ logger.debug('%o', argv);
process.on('unhandledRejection', logger.error);
process.on('uncaughtException', logger.error);
const publicTemp = path.resolve(os.tmpdir(), 'hydro', 'public');
export function addon(addonPath: string, prepend = false) {
if (!(fs.existsSync(addonPath) && fs.statSync(addonPath).isFile())) {
try {
@@ -33,9 +30,8 @@ export function addon(addonPath: string, prepend = false) {
const packagejson = require.resolve(`${addonPath}/package.json`);
const modulePath = path.dirname(packagejson);
const publicPath = path.resolve(modulePath, 'public');
if (fs.existsSync(publicPath)) fs.copySync(publicPath, publicTemp);
if (prepend) global.addons.unshift(modulePath);
else global.addons.push(modulePath);
if (fs.existsSync(publicPath)) global.publicDirs[prepend ? 'push' : 'unshift'](publicPath);
global.addons[prepend ? 'unshift' : 'push'](modulePath);
} catch (e) {
logger.error(`Addon not found: ${addonPath}`);
}
+3 -9
View File
@@ -1,8 +1,6 @@
import assert from 'assert';
import { createHash } from 'crypto';
import http from 'http';
import os from 'os';
import { resolve } from 'path';
import { PassThrough } from 'stream';
import cac from 'cac';
import Cookies from 'cookies';
@@ -252,13 +250,9 @@ export async function prepare() {
rewrite: (p) => p.replace('/fs', ''),
}));
app.use(Compress());
if (argv.options.public) {
app.use(cache(argv.options.public, {
maxAge: 0,
}));
} else {
app.use(cache(resolve(os.tmpdir(), 'hydro', 'public'), {
maxAge: 30 * 24 * 60 * 60,
for (const dir of global.publicDirs) {
app.use(cache(dir, {
maxAge: argv.options.public ? 0 : 24 * 3600 * 1000,
}));
}
if (process.env.DEV) {