mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: serve static files without copy
This commit is contained in:
1 parent
eef64ff2e0
commit
b7348e4be3
5 files changed
+7
-20
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "hydrooj",
|
"name": "hydrooj",
|
||||||
"version": "2.34.8",
|
"version": "2.34.9",
|
||||||
"bin": "bin/hydrooj.js",
|
"bin": "bin/hydrooj.js",
|
||||||
"main": "src/loader",
|
"main": "src/loader",
|
||||||
"module": "src/loader",
|
"module": "src/loader",
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import cac from 'cac';
|
|||||||
import fs from 'fs-extra';
|
import fs from 'fs-extra';
|
||||||
import Koa, { Context } from 'koa';
|
import Koa, { Context } from 'koa';
|
||||||
import Body from 'koa-body';
|
import Body from 'koa-body';
|
||||||
import cache from 'koa-static-cache';
|
|
||||||
import mongodb from 'mongodb';
|
import mongodb from 'mongodb';
|
||||||
import { Logger } from '../logger';
|
import { Logger } from '../logger';
|
||||||
|
|
||||||
@@ -117,9 +116,6 @@ export function load() {
|
|||||||
const server = http.createServer(app.callback());
|
const server = http.createServer(app.callback());
|
||||||
app.keys = ['Hydro'];
|
app.keys = ['Hydro'];
|
||||||
app
|
app
|
||||||
.use(cache(path.join(os.tmpdir(), 'hydro', 'public'), {
|
|
||||||
maxAge: 365 * 24 * 60 * 60,
|
|
||||||
}))
|
|
||||||
.use(Body())
|
.use(Body())
|
||||||
.use((ctx) => {
|
.use((ctx) => {
|
||||||
if (ctx.request.method.toLowerCase() === 'post') return post(ctx);
|
if (ctx.request.method.toLowerCase() === 'post') return post(ctx);
|
||||||
|
|||||||
@@ -34,4 +34,5 @@ if (!global.Hydro) {
|
|||||||
locales: {},
|
locales: {},
|
||||||
};
|
};
|
||||||
global.addons = [];
|
global.addons = [];
|
||||||
|
global.publicDirs = [];
|
||||||
}
|
}
|
||||||
@@ -4,7 +4,6 @@
|
|||||||
import 'reflect-metadata';
|
import 'reflect-metadata';
|
||||||
import './init';
|
import './init';
|
||||||
import './interface';
|
import './interface';
|
||||||
import os from 'os';
|
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import fs from 'fs-extra';
|
import fs from 'fs-extra';
|
||||||
import './utils';
|
import './utils';
|
||||||
@@ -24,8 +23,6 @@ logger.debug('%o', argv);
|
|||||||
process.on('unhandledRejection', logger.error);
|
process.on('unhandledRejection', logger.error);
|
||||||
process.on('uncaughtException', logger.error);
|
process.on('uncaughtException', logger.error);
|
||||||
|
|
||||||
const publicTemp = path.resolve(os.tmpdir(), 'hydro', 'public');
|
|
||||||
|
|
||||||
export function addon(addonPath: string, prepend = false) {
|
export function addon(addonPath: string, prepend = false) {
|
||||||
if (!(fs.existsSync(addonPath) && fs.statSync(addonPath).isFile())) {
|
if (!(fs.existsSync(addonPath) && fs.statSync(addonPath).isFile())) {
|
||||||
try {
|
try {
|
||||||
@@ -33,9 +30,8 @@ export function addon(addonPath: string, prepend = false) {
|
|||||||
const packagejson = require.resolve(`${addonPath}/package.json`);
|
const packagejson = require.resolve(`${addonPath}/package.json`);
|
||||||
const modulePath = path.dirname(packagejson);
|
const modulePath = path.dirname(packagejson);
|
||||||
const publicPath = path.resolve(modulePath, 'public');
|
const publicPath = path.resolve(modulePath, 'public');
|
||||||
if (fs.existsSync(publicPath)) fs.copySync(publicPath, publicTemp);
|
if (fs.existsSync(publicPath)) global.publicDirs[prepend ? 'push' : 'unshift'](publicPath);
|
||||||
if (prepend) global.addons.unshift(modulePath);
|
global.addons[prepend ? 'unshift' : 'push'](modulePath);
|
||||||
else global.addons.push(modulePath);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.error(`Addon not found: ${addonPath}`);
|
logger.error(`Addon not found: ${addonPath}`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
import assert from 'assert';
|
import assert from 'assert';
|
||||||
import { createHash } from 'crypto';
|
import { createHash } from 'crypto';
|
||||||
import http from 'http';
|
import http from 'http';
|
||||||
import os from 'os';
|
|
||||||
import { resolve } from 'path';
|
|
||||||
import { PassThrough } from 'stream';
|
import { PassThrough } from 'stream';
|
||||||
import cac from 'cac';
|
import cac from 'cac';
|
||||||
import Cookies from 'cookies';
|
import Cookies from 'cookies';
|
||||||
@@ -252,13 +250,9 @@ export async function prepare() {
|
|||||||
rewrite: (p) => p.replace('/fs', ''),
|
rewrite: (p) => p.replace('/fs', ''),
|
||||||
}));
|
}));
|
||||||
app.use(Compress());
|
app.use(Compress());
|
||||||
if (argv.options.public) {
|
for (const dir of global.publicDirs) {
|
||||||
app.use(cache(argv.options.public, {
|
app.use(cache(dir, {
|
||||||
maxAge: 0,
|
maxAge: argv.options.public ? 0 : 24 * 3600 * 1000,
|
||||||
}));
|
|
||||||
} else {
|
|
||||||
app.use(cache(resolve(os.tmpdir(), 'hydro', 'public'), {
|
|
||||||
maxAge: 30 * 24 * 60 * 60,
|
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
if (process.env.DEV) {
|
if (process.env.DEV) {
|
||||||
|
|||||||
Reference in new issue
Block a user