模块管理器

This commit is contained in:
undefined committed 2020-05-05 12:07:34 +08:00
1 parent 04941936bc
commit 3e9b074e77
22 files changed
+191 -59

No files matched your search

+1 -1
View File
@@ -9,7 +9,7 @@ process.stdin.on('data', async (input) => {
}
});
require('./loader')().catch((e) => {
require('./loader').load().catch((e) => {
console.error(e);
process.exit(1);
});
+23 -5
View File
@@ -1,6 +1,8 @@
const user = require('../model/user');
const { Route, Handler } = require('../service/server');
const { PERM_MANAGE } = require('../permission');
const hpm = require('../lib/hpm');
const loader = require('../loader');
const { RoleAlreadyExistError, ValidationError } = require('../error');
class ManageHandler extends Handler {
@@ -18,14 +20,14 @@ class ManageMainHandler extends ManageHandler {
class ManageDashboardHandler extends ManageHandler {
async get() {
this.response.template = 'domain_manage_dashboard.html';
this.response.template = 'manage_dashboard.html';
this.response.body = { system: this.system };
}
}
class ManageEditHandler extends ManageHandler {
async get() {
this.response.template = 'domain_manage_edit.html';
this.response.template = 'manage_edit.html';
this.response.body = { system: this.system };
}
@@ -54,7 +56,7 @@ class ManageUserHandler extends ManageHandler {
}
const rolesSelect = roles.map((role) => [role._id, role._id]);
const udict = await user.getList(uids);
this.response.template = 'domain_manage_user.html';
this.response.template = 'manage_user.html';
this.response.body = {
roles, rolesSelect, rudocs, udict, system,
};
@@ -72,7 +74,7 @@ class ManagePermissionHandler extends ManageHandler {
user.getRoles(),
user.getById(0),
]);
this.response.template = 'domain_manage_permission.html';
this.response.template = 'manage_permission.html';
this.response.body = { roles, system };
}
@@ -95,7 +97,7 @@ class ManageRoleHandler extends ManageHandler {
user.getRoles(),
user.getById(0),
]);
this.response.template = 'domain_manage_role.html';
this.response.template = 'manage_role.html';
this.response.body = { roles, system };
}
@@ -120,6 +122,20 @@ class ManageRoleHandler extends ManageHandler {
}
}
class ManageModuleHandler extends ManageHandler {
async get() {
const installed = await hpm.getInstalled();
const active = loader.active.map((module) => module.id);
this.response.body = { installed, active };
this.response.template = 'manage_module.html';
}
async postDelete({ id }) {
await hpm.del(id);
this.back();
}
}
async function apply() {
Route('/manage', module.exports.ManageMainHandler);
Route('/manage/dashboard', module.exports.ManageDashboardHandler);
@@ -127,6 +143,7 @@ async function apply() {
Route('/manage/user', module.exports.ManageUserHandler);
Route('/manage/permission', module.exports.ManagePermissionHandler);
Route('/manage/role', module.exports.ManageRoleHandler);
Route('/manage/module', module.exports.ManageModuleHandler);
}
global.Hydro.handler.manage = module.exports = {
@@ -136,5 +153,6 @@ global.Hydro.handler.manage = module.exports = {
ManageUserHandler,
ManagePermissionHandler,
ManageRoleHandler,
ManageModuleHandler,
apply,
};
+39
View File
@@ -0,0 +1,39 @@
/* eslint-disable import/no-dynamic-require */
/* eslint-disable no-await-in-loop */
/* eslint-disable no-eval */
const fs = require('fs');
const zlib = require('zlib');
const path = require('path');
const yaml = require('js-yaml');
const download = require('./download');
function root(name) {
return path.resolve(process.cwd(), name);
}
async function getInstalled() {
const files = fs.readdirSync(root('.build/module'));
const installed = [];
for (const file of files) {
if (file.endsWith('.hydro-module')) {
const f = fs.readFileSync(root(`.build/module/${file}`));
const s = fs.statSync(root(`.build/module/${file}`));
installed.push({
...yaml.safeLoad(zlib.gunzipSync(f)),
id: file.split('.')[0],
size: s.size,
});
}
}
return installed;
}
async function del(id) {
fs.unlinkSync(root(`.build/module/${id}.hydro-module`));
}
async function install(id, url) {
await download(url, root(`module/${id}.hydro-module`));
}
module.exports = { getInstalled, del, install };
+1 -1
View File
@@ -36,7 +36,7 @@ exports.paginate = function* paginate(page, numPages) {
yield ['last', numPages];
};
exports.format_size = function formatSize(size, base = 1) {
exports.size = function formatSize(size, base = 1) {
size *= base;
const unit = 1024;
const unitNames = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
+1 -1
View File
@@ -13,6 +13,6 @@ Item('/p', 'problem_main', 'problem', permission.PERM_VIEW_PROBLEM);
Item('/t', 'training_main', 'training', permission.PERM_VIEW_TRAINING);
Item('/discuss', 'discussion_main', 'discussion', permission.PERM_VIEW_DISCUSSION);
Item('/c', 'contest_main', 'contest', permission.PERM_VIEW_CONTEST);
Item('/manage', 'domain_manage', 'domain_manage', permission.PERM_MANAGE);
Item('/manage', 'manage', 'manage', permission.PERM_MANAGE);
global.Hydro.lib.nav = module.exports = Item;
+7 -7
View File
@@ -10,20 +10,20 @@ function root(name) {
return path.resolve(process.cwd(), name);
}
const installed = [];
const active = [];
async function preload() {
const files = fs.readdirSync(root('.build/module'));
for (const file of files) {
if (file.endsWith('.hydro-module')) {
const f = fs.readFileSync(root(`.build/module/${file}`));
installed.push({ ...yaml.safeLoad(zlib.gunzipSync(f)), id: file.split('.')[0] });
active.push({ ...yaml.safeLoad(zlib.gunzipSync(f)), id: file.split('.')[0] });
}
}
}
async function handler() {
for (const i of installed) {
for (const i of active) {
if (i.handler) {
console.time(`Handler init: ${i.id}`);
const module = {}; // eslint-disable-line no-unused-vars
@@ -35,7 +35,7 @@ async function handler() {
}
async function locale() {
for (const i of installed) {
for (const i of active) {
if (i.locale) {
global.Hydro.lib.i18n(i.locale);
console.log(`Locale init: ${i.id}`);
@@ -44,7 +44,7 @@ async function locale() {
}
async function template() {
for (const i of installed) {
for (const i of active) {
if (i.template) {
Object.assign(global.Hydro.template, i.template);
console.log(`Template init: ${i.id}`);
@@ -53,7 +53,7 @@ async function template() {
}
async function model() {
for (const i of installed) {
for (const i of active) {
if (i.model) {
console.time(`Model init: ${i.id}`);
const module = {};
@@ -125,4 +125,4 @@ async function load() {
server.start();
}
module.exports = load;
module.exports = { load, active };