将内置UI封包

This commit is contained in:
undefined committed 2020-05-10 17:46:43 +08:00
1 parent 9156fa3a9b
commit 88fc0606ee
5 files changed
+74 -52

No files matched your search

+9 -9
View File
@@ -8,6 +8,15 @@ jobs:
name: Build
runs-on: ubuntu-latest
steps:
- run: |
cd ui
yarn
yarn build:production
cd ..
- uses: actions/upload-artifact@v1
with:
name: .uibuild
path: .uibuild
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
@@ -19,15 +28,6 @@ jobs:
with:
name: hydro
path: .build
- run: |
cd ui
yarn
yarn build:production
cd ..
- uses: actions/upload-artifact@v1
with:
name: .uibuild
path: .uibuild
- uses: elgohr/Publish-Docker-Github-Action@master
with:
name: hydrooj/hydro:alpine
+1 -26
View File
@@ -14,32 +14,7 @@ Hydro 的界面基于 Vijos 二次开发。
#### 直接部署:
安装mongodb (省略)
编辑 `config.yaml` :
```yaml
db:
host: 127.0.0.1
port: 27017
name: hydro
username: hydro
password: db.drop();
listen:
host: 127.0.0.1
port: 8888
session:
domain: '*'
```
运行:
```sh
yarn
cd ui
yarn
yarn build:production
cd ..
node hydro/development.js
```
下载Github Actions自动编译的 development.js 直接运行即可。
## 鸣谢
+27 -3
View File
@@ -1,10 +1,23 @@
const fs = require('fs');
const path = require('path');
const zlib = require('zlib');
const yaml = require('js-yaml');
const root = require('./root');
const template = require('./template');
const ignoreFailure = require('./ignoreFailure');
function getFiles(folder) {
const res = [];
const files = fs.readdirSync(root(folder));
for (const filename of files) {
res.push(filename);
if (fs.statSync(root(path.join(folder, filename))).isDirectory()) {
res.push(...(getFiles(path.join(folder, filename)).map((i) => path.join(filename, i))));
}
}
return res;
}
async function build(type) {
if (!['development', 'production'].includes(type)) throw new Error(`Unknown type: ${type}`);
ignoreFailure(fs.mkdirSync, root('.build'));
@@ -20,10 +33,21 @@ async function build(type) {
'partials/problem_default.md',
'bsod.html',
];
const builtin = {};
builtin.locale = lang;
builtin.template = template('templates', exclude);
const builtin = {
locale: lang,
template: template('templates', exclude),
file: {},
};
const files = getFiles('.uibuild');
for (const f of files) {
if (fs.statSync(root(`.uibuild/${f}`)).isDirectory()) {
builtin.file[f] = null;
} else {
builtin.file[f] = fs.readFileSync(root(`.uibuild/${f}`)).toString('base64');
}
}
const data = zlib.gzipSync(Buffer.from(yaml.safeDump(builtin)), { level: 3 });
fs.writeFileSync(root('.build/builtin.json'), JSON.stringify({ data: data.toString('base64') }));
fs.writeFileSync(root('.build/module/builtin.hydro'), data);
await require('./buildModule')(type);
await require('./webpack')(type);
+30 -12
View File
@@ -20,6 +20,13 @@ function ensureDir(dir) {
}
const active = [];
try {
const f = require('../.build/builtin.json');
const m = { ...yaml.safeLoad(zlib.gunzipSync(f.data)), id: 'builtin' };
active.push(m);
} catch (e) {
/* Ignore */
}
async function preload() {
const files = fs.readdirSync(root('.build/module'));
@@ -28,24 +35,35 @@ async function preload() {
try {
const f = fs.readFileSync(root(`.build/module/${file}`));
const m = { ...yaml.safeLoad(zlib.gunzipSync(f)), id: file.split('.')[0] };
if (m.os) {
if (!m.os.includes(os.platform())) throw new Error('Unsupported OS');
}
if (m.file) {
m.files = {};
ensureDir(path.resolve(os.tmpdir(), 'hydro', file));
for (const n in m.file) {
const e = path.resolve(os.tmpdir(), 'hydro', file, n);
fs.writeFileSync(e, Buffer.from(m.file[n], 'base64'), { mode: 755 });
m.files[n] = e;
}
}
active.push(m);
} catch (e) {
console.error(`Module Load Fail: ${file}`);
}
}
}
for (const i of active) {
try {
if (i.os) {
if (!i.os.includes(os.platform())) throw new Error('Unsupported OS');
}
if (i.file) {
i.files = {};
ensureDir(path.resolve(os.tmpdir(), 'hydro', i.id));
for (const n in i.file) {
if (i.file[n] === null) {
ensureDir(path.resolve(os.tmpdir(), 'hydro', i.id, n));
} else {
const e = path.resolve(os.tmpdir(), 'hydro', i.id, n);
fs.writeFileSync(e, Buffer.from(i.file[n], 'base64'), { mode: 755 });
i.files[n] = e;
}
}
}
} catch (e) {
console.error(`Module Load Fail: ${i.id}`);
console.error(e);
}
}
}
async function handler() {
+7 -2
View File
@@ -1,4 +1,5 @@
const path = require('path');
const os = require('os');
const { ObjectID } = require('bson');
const Koa = require('koa');
const morgan = require('koa-morgan');
@@ -28,7 +29,7 @@ async function prepare() {
const useHttps = await system.get('listen.https');
server = (useHttps ? https : http).createServer(app.callback());
app.keys = await system.get('session.keys');
app.use(cache(path.join(process.cwd(), '.uibuild'), {
app.use(cache(path.join(os.tmpdir(), 'hydro', 'builtin'), {
maxAge: 365 * 24 * 60 * 60,
}));
app.use(Body({
@@ -395,6 +396,10 @@ function Connection(prefix, RouteConnHandler) {
sock.installHandlers(server);
}
function Middleware(middleware) {
app.use(middleware);
}
async function start() {
app.use(morgan(':method :url :status :res[content-length] - :response-time ms'));
app.use(router.routes()).use(router.allowedMethods());
@@ -404,5 +409,5 @@ async function start() {
}
global.Hydro.service.server = module.exports = {
Handler, ConnectionHandler, Route, Connection, prepare, start,
Handler, ConnectionHandler, Route, Connection, Middleware, prepare, start,
};