mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 09:36:08 +08:00
core: add 'hydrooj db' method
This commit is contained in:
1 parent
5513bd6929
commit
551cf9c19f
4 files changed
+30
-6
No files matched your search
@@ -5,6 +5,7 @@ const path = require('path');
|
||||
const cluster = require('cluster');
|
||||
const fs = require('fs-extra');
|
||||
const { argv } = require('yargs');
|
||||
const child = require('child_process');
|
||||
|
||||
if (!cluster.isMaster) {
|
||||
const hydro = require('../dist/loader');
|
||||
@@ -14,11 +15,25 @@ if (!cluster.isMaster) {
|
||||
process.exit(1);
|
||||
});
|
||||
} else {
|
||||
fs.ensureDirSync(path.resolve(os.homedir(), '.hydro'));
|
||||
const addonPath = path.resolve(os.homedir(), '.hydro', 'addon.json');
|
||||
const hydroPath = path.resolve(os.homedir(), '.hydro');
|
||||
fs.ensureDirSync(hydroPath);
|
||||
const addonPath = path.resolve(hydroPath, 'addon.json');
|
||||
if (!fs.existsSync(addonPath)) fs.writeFileSync(addonPath, '[]');
|
||||
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 {
|
||||
const ui = argv.ui || '@hydrooj/ui-default';
|
||||
require.resolve(ui);
|
||||
|
||||
@@ -36,6 +36,7 @@ homework_main: Homework
|
||||
homework_scoreboard: Scoreboard
|
||||
judge_playground: Judge Playground
|
||||
main: Home
|
||||
manage_dashboard: Control Panel
|
||||
manage_module: Module Management
|
||||
manage_script: Scripts
|
||||
manage_setting: System Settings
|
||||
@@ -58,6 +59,7 @@ perm_training: Trainings
|
||||
problem_create: Problem Create
|
||||
problem_detail: Problem Detail
|
||||
problem_edit: Problem Edit
|
||||
problem_files: Problem Files
|
||||
problem_import: Import Problem
|
||||
problem_main: Problem Set
|
||||
problem_solution: Problem Solution
|
||||
@@ -87,6 +89,7 @@ setting_smtp: SMTP Settings
|
||||
setting_usage: Usage Preference
|
||||
timeago_locale: en_US
|
||||
training_create: Training Create
|
||||
training_detail: Training Detail
|
||||
training_edit: Training Edit
|
||||
training_main: Training
|
||||
user_detail: User Detail
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "hydrooj",
|
||||
"version": "2.20.32",
|
||||
"version": "2.20.33",
|
||||
"bin": "bin/hydrooj.js",
|
||||
"main": "dist/loader.js",
|
||||
"typings": "dist/loader.d.ts",
|
||||
|
||||
@@ -3,6 +3,7 @@ import * as bus from './bus';
|
||||
import { Collections } from '../interface';
|
||||
|
||||
interface MongoConfig {
|
||||
protocol?: string,
|
||||
username?: string,
|
||||
password?: string,
|
||||
host?: string,
|
||||
@@ -19,12 +20,17 @@ class MongoService {
|
||||
public db2: Db;
|
||||
private opts: MongoConfig;
|
||||
|
||||
async start(opts: MongoConfig) {
|
||||
this.opts = opts;
|
||||
let mongourl = 'mongodb://';
|
||||
static buildUrl(opts: MongoConfig) {
|
||||
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;
|
||||
}
|
||||
|
||||
async start(opts: MongoConfig) {
|
||||
this.opts = opts;
|
||||
const mongourl = MongoService.buildUrl(opts);
|
||||
this.client = await MongoClient.connect(mongourl, { useNewUrlParser: true, useUnifiedTopology: true });
|
||||
this.db = this.client.db(opts.name);
|
||||
this.client2 = await MongoClient.connect(mongourl, { useNewUrlParser: true, useUnifiedTopology: true });
|
||||
|
||||
Reference in new issue
Block a user