服务状态(/status)

This commit is contained in:
masnn committed 2020-05-31 20:07:52 +08:00
1 parent b2989cb9ea
commit 216a4a91e4
19 files changed
+322 -136

No files matched your search

+15 -13
View File
@@ -21,6 +21,8 @@ const {
PERM_LOGGEDIN,
} = require('../permission');
const { geoip, useragent } = global.Hydro.lib;
class HomeHandler extends Handler {
async contest(domainId) {
if (this.user.hasPerm(PERM_VIEW_CONTEST)) {
@@ -81,13 +83,13 @@ class HomeSecurityHandler extends Handler {
// TODO(iceboy): pagination? or limit session count for uid?
// TODO(masnn): UA & IP
const sessions = await token.getSessionListByUid(this.user._id);
const parsed = sessions.map((session) => ({
...session,
// updateUa: useragent.parse(session.updateUa || session.createUa || ''),
// updateGeoip: geoip.ip2geo(session.updateIp || session.createIp, this.user.viewLang),
_id: md5(session._id),
isCurrent: session._id === this.session._id,
}));
const parsed = [];
for (const session of sessions) {
session.isCurrent = session._id === this.session._id;
session._id = md5(session._id);
if (useragent) session.updateUa = useragent.parse(session.updateUa || session.createUa || '');
if (geoip) session.updateGeoip = geoip.lookup(session.updateIp || session.createIp);
}
this.response.template = 'home_security.html';
this.response.body = { sessions: parsed };
}
@@ -153,7 +155,7 @@ class HomeSettingsHandler extends Handler {
async post(args) {
// FIXME validation
await user.setById(domainId, this.user._id, args);
await user.setById(args.domainId, this.user._id, args);
this.back();
}
}
@@ -166,9 +168,10 @@ class UserChangemailWithCodeHandler extends Handler {
}
const udoc = await user.getByEmail(tdoc.mail);
if (udoc) throw new UserAlreadyExistError(tdoc.mail);
// TODO(twd2): Ensure mail is unique.
await user.setEmail(this.user._id, tdoc.mail);
await token.delete(code, token.TYPE_CHANGEMAIL);
await Promise.all([
user.setEmail(this.user._id, tdoc.mail),
token.delete(code, token.TYPE_CHANGEMAIL),
]);
this.response.redirect = '/home/security';
}
}
@@ -211,8 +214,7 @@ class HomeMessagesHandler extends Handler {
if (type === 'single') {
mdoc = mdoc.reply[mdoc.reply.length - 1];
}
// TODO(twd2): improve here:
// projection
// TODO(twd2): improve here: projection
mdoc.from_udoc = this.user;
mdoc.to_udoc = udoc;
this.udoc(mdoc, 'from');
+3 -1
View File
@@ -1,6 +1,7 @@
const user = require('../model/user');
const domain = require('../model/domain');
const setting = require('../model/setting');
const system = require('../model/system');
const { Route, Handler } = require('../service/server');
const { PERM_MANAGE } = require('../permission');
const hpm = require('../lib/hpm');
@@ -53,6 +54,7 @@ class ManageEditHandler extends ManageHandler {
class ManageUserHandler extends ManageHandler {
async get({ domainId }) {
// FIXME(masnn) UPDATE CODE
const uids = [];
const rudocs = {};
const [udocs, roles] = await Promise.all([
@@ -73,7 +75,7 @@ class ManageUserHandler extends ManageHandler {
];
this.response.template = 'manage_user.html';
this.response.body = {
roles, rolesSelect, rudocs, udict, system: sys, path,
roles, rolesSelect, rudocs, udict, path,
};
}
+34
View File
@@ -0,0 +1,34 @@
const { PERM_JUDGE } = require('../permission');
const { Route, Handler } = require('../service/server');
const db = require('../service/db');
const coll = db.collection('status');
class StatusHandler extends Handler {
async get() {
const stats = await coll.find().sort({ type: 1, updateAt: -1 }).toArray();
for (const i in stats) {
let desc = '';
const online = new Date(stats[i].updateAt).getTime() > new Date().getTime() - 300000;
if (!online) desc = 'Offline';
desc = desc || 'Online';
stats[i].status = desc;
}
this.response.body = { stats };
this.response.template = 'status.html';
}
}
class StatusUpdateHandler extends Handler {
async post(args) {
this.checkPerm(PERM_JUDGE);
await coll.updateOne({ _id: args._id }, { $set: args }, { upsert: true });
}
}
async function apply() {
Route('/status', StatusHandler);
Route('/status/update', StatusUpdateHandler);
}
global.Hydro.handler.status = module.exports = { StatusHandler, StatusUpdateHandler, apply };
+91
View File
@@ -0,0 +1,91 @@
const domain = require('../model/domain');
const system = require('../model/system');
const setting = require('../model/setting');
const { Route, Handler } = require('../service/server');
const { PERM_MANAGE } = require('../permission');
const hpm = require('../lib/hpm');
const loader = require('../loader');
class SystemHandler extends Handler {
async prepare({ domainId }) {
this.checkPerm(PERM_MANAGE);
this.domain = await domain.get(domainId);
}
}
class SystemDashboardHandler extends SystemHandler {
async get() {
const path = [
['Hydro', '/'],
['manage', '/manage'],
['manage_dashboard', null],
];
this.response.template = 'manage_dashboard.html';
this.response.body = { domain: this.domain, path };
}
}
class SystemModuleHandler extends SystemHandler {
async get() {
const installed = await hpm.getInstalled();
const path = [
['Hydro', '/'],
['manage', '/manage'],
['manage_module', null],
];
this.response.body = { installed, active: loader.active, path };
this.response.template = 'manage_module.html';
}
async postInstall({ url, id }) {
await hpm.install(url, id);
this.back();
}
async postDelete({ id }) {
await hpm.del(id);
this.back();
}
}
class SystemSettingHandler extends SystemHandler {
async get() {
this.response.template = 'manage_settings.html';
const current = {};
const settings = setting.SYSTEM_SETTINGS;
for (const s of settings) {
current[s.key] = await system.get(s.key);
}
this.response.body = {
current, settings,
};
}
async post(args) {
const tasks = [];
for (const key in args) {
if (typeof args[key] === 'object') {
const subtasks = [];
for (const sub in args[key]) {
subtasks.push(system.set(`${key}.${sub}`, args[key][sub]));
}
tasks.push(Promise.all(subtasks));
} else tasks.push(system.set(key, args[key]));
}
await Promise.all(tasks);
this.back();
}
}
async function apply() {
Route('/system', module.exports.SystemDashboardHandler);
Route('/system/module', module.exports.SystemModuleHandler);
Route('/system/setting', module.exports.SystemSettingHandler);
}
global.Hydro.handler.manage = module.exports = {
SystemDashboardHandler,
SystemModuleHandler,
SystemSettingHandler,
apply,
};