core: add 'hydrooj db' method

This commit is contained in:
undefined committed 2021-03-26 16:38:51 +08:00
1 parent 5513bd6929
commit 551cf9c19f
4 files changed
+30 -6

No files matched your search

+17 -2
View File
@@ -5,6 +5,7 @@ const path = require('path');
const cluster = require('cluster'); const cluster = require('cluster');
const fs = require('fs-extra'); const fs = require('fs-extra');
const { argv } = require('yargs'); const { argv } = require('yargs');
const child = require('child_process');
if (!cluster.isMaster) { if (!cluster.isMaster) {
const hydro = require('../dist/loader'); const hydro = require('../dist/loader');
@@ -14,11 +15,25 @@ if (!cluster.isMaster) {
process.exit(1); process.exit(1);
}); });
} else { } else {
fs.ensureDirSync(path.resolve(os.homedir(), '.hydro')); const hydroPath = path.resolve(os.homedir(), '.hydro');
const addonPath = path.resolve(os.homedir(), '.hydro', 'addon.json'); fs.ensureDirSync(hydroPath);
const addonPath = path.resolve(hydroPath, 'addon.json');
if (!fs.existsSync(addonPath)) fs.writeFileSync(addonPath, '[]'); if (!fs.existsSync(addonPath)) fs.writeFileSync(addonPath, '[]');
let addons = JSON.parse(fs.readFileSync(addonPath).toString()); let addons = JSON.parse(fs.readFileSync(addonPath).toString());
if (argv._[0] === 'db') {
function buildUrl(opts) {
let mongourl = `${opts.protocol}://`;
if (opts.username) mongourl += `${opts.username}:${opts.password}@`;
mongourl += `${opts.host}:${opts.port}/${opts.name}`;
if (opts.url) mongourl = opts.url;
return mongourl;
}
const dbConfig = fs.readFileSync(path.resolve(hydroPath, 'config.json'), 'utf-8');
const url = buildUrl(JSON.parse(dbConfig));
return child.spawn('mongo', [url], { stdio: 'inherit' });
}
try { try {
const ui = argv.ui || '@hydrooj/ui-default'; const ui = argv.ui || '@hydrooj/ui-default';
require.resolve(ui); require.resolve(ui);
+3
View File
@@ -36,6 +36,7 @@ homework_main: Homework
homework_scoreboard: Scoreboard homework_scoreboard: Scoreboard
judge_playground: Judge Playground judge_playground: Judge Playground
main: Home main: Home
manage_dashboard: Control Panel
manage_module: Module Management manage_module: Module Management
manage_script: Scripts manage_script: Scripts
manage_setting: System Settings manage_setting: System Settings
@@ -58,6 +59,7 @@ perm_training: Trainings
problem_create: Problem Create problem_create: Problem Create
problem_detail: Problem Detail problem_detail: Problem Detail
problem_edit: Problem Edit problem_edit: Problem Edit
problem_files: Problem Files
problem_import: Import Problem problem_import: Import Problem
problem_main: Problem Set problem_main: Problem Set
problem_solution: Problem Solution problem_solution: Problem Solution
@@ -87,6 +89,7 @@ setting_smtp: SMTP Settings
setting_usage: Usage Preference setting_usage: Usage Preference
timeago_locale: en_US timeago_locale: en_US
training_create: Training Create training_create: Training Create
training_detail: Training Detail
training_edit: Training Edit training_edit: Training Edit
training_main: Training training_main: Training
user_detail: User Detail user_detail: User Detail
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "hydrooj", "name": "hydrooj",
"version": "2.20.32", "version": "2.20.33",
"bin": "bin/hydrooj.js", "bin": "bin/hydrooj.js",
"main": "dist/loader.js", "main": "dist/loader.js",
"typings": "dist/loader.d.ts", "typings": "dist/loader.d.ts",
+9 -3
View File
@@ -3,6 +3,7 @@ import * as bus from './bus';
import { Collections } from '../interface'; import { Collections } from '../interface';
interface MongoConfig { interface MongoConfig {
protocol?: string,
username?: string, username?: string,
password?: string, password?: string,
host?: string, host?: string,
@@ -19,12 +20,17 @@ class MongoService {
public db2: Db; public db2: Db;
private opts: MongoConfig; private opts: MongoConfig;
async start(opts: MongoConfig) { static buildUrl(opts: MongoConfig) {
this.opts = opts; let mongourl = `${opts.protocol}://`;
let mongourl = 'mongodb://';
if (opts.username) mongourl += `${opts.username}:${opts.password}@`; if (opts.username) mongourl += `${opts.username}:${opts.password}@`;
mongourl += `${opts.host}:${opts.port}/${opts.name}`; mongourl += `${opts.host}:${opts.port}/${opts.name}`;
if (opts.url) mongourl = opts.url; if (opts.url) mongourl = opts.url;
return mongourl;
}
async start(opts: MongoConfig) {
this.opts = opts;
const mongourl = MongoService.buildUrl(opts);
this.client = await MongoClient.connect(mongourl, { useNewUrlParser: true, useUnifiedTopology: true }); this.client = await MongoClient.connect(mongourl, { useNewUrlParser: true, useUnifiedTopology: true });
this.db = this.client.db(opts.name); this.db = this.client.db(opts.name);
this.client2 = await MongoClient.connect(mongourl, { useNewUrlParser: true, useUnifiedTopology: true }); this.client2 = await MongoClient.connect(mongourl, { useNewUrlParser: true, useUnifiedTopology: true });