mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: setup: prefer mongo connection uri
This commit is contained in:
3 files changed
+12
-41
No files matched your search
@@ -41,6 +41,7 @@
|
|||||||
"mime-types": "^2.1.35",
|
"mime-types": "^2.1.35",
|
||||||
"moment-timezone": "^0.5.45",
|
"moment-timezone": "^0.5.45",
|
||||||
"mongodb": "^5.9.2",
|
"mongodb": "^5.9.2",
|
||||||
|
"mongodb-uri": "^0.9.7",
|
||||||
"nanoid": "^5.0.6",
|
"nanoid": "^5.0.6",
|
||||||
"nodemailer": "^6.9.11",
|
"nodemailer": "^6.9.11",
|
||||||
"notp": "^2.0.3",
|
"notp": "^2.0.3",
|
||||||
@@ -66,6 +67,7 @@
|
|||||||
"@types/koa-static-cache": "^5.1.4",
|
"@types/koa-static-cache": "^5.1.4",
|
||||||
"@types/lodash": "^4.14.202",
|
"@types/lodash": "^4.14.202",
|
||||||
"@types/mime-types": "^2.1.4",
|
"@types/mime-types": "^2.1.4",
|
||||||
|
"@types/mongodb-uri": "^0",
|
||||||
"@types/nodemailer": "^6.4.14",
|
"@types/nodemailer": "^6.4.14",
|
||||||
"@types/notp": "^2.0.5",
|
"@types/notp": "^2.0.5",
|
||||||
"@types/saslprep": "^1.0.3",
|
"@types/saslprep": "^1.0.3",
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import fs from 'fs-extra';
|
|||||||
import Koa, { Context } from 'koa';
|
import Koa, { Context } from 'koa';
|
||||||
import Body from 'koa-body';
|
import Body from 'koa-body';
|
||||||
import { MongoClient, WriteConcern } from 'mongodb';
|
import { MongoClient, WriteConcern } from 'mongodb';
|
||||||
|
import mongoUri from 'mongodb-uri';
|
||||||
import { Logger } from '../logger';
|
import { Logger } from '../logger';
|
||||||
|
|
||||||
const logger = new Logger('setup');
|
const logger = new Logger('setup');
|
||||||
@@ -18,10 +19,7 @@ async function get(ctx: Context) {
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
|
||||||
<meta http-equiv="X-UA-Compatible" content="chrome=1"/>
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
|
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
|
||||||
<meta name="msapplication-TileColor" content="#579e9a">
|
|
||||||
<meta name="theme-color" content="#56758f">
|
|
||||||
<link rel="stylesheet" media="all" href="/default.theme.css">
|
<link rel="stylesheet" media="all" href="/default.theme.css">
|
||||||
<title>Setup</title>
|
<title>Setup</title>
|
||||||
</head>
|
</head>
|
||||||
@@ -34,32 +32,8 @@ async function get(ctx: Context) {
|
|||||||
<form method="POST">
|
<form method="POST">
|
||||||
<div class="row"><div class="columns">
|
<div class="row"><div class="columns">
|
||||||
<label class="inverse material textbox">
|
<label class="inverse material textbox">
|
||||||
Database Host
|
MongoDB Connection URI
|
||||||
<input name="host" type="text" value="127.0.0.1" autofocus>
|
<input name="url" type="text" value="mongodb://user:pass@127.0.0.1:27017/hydro" autofocus>
|
||||||
</label>
|
|
||||||
</div></div>
|
|
||||||
<div class="row"><div class="columns">
|
|
||||||
<label class="inverse material textbox">
|
|
||||||
Database Port
|
|
||||||
<input name="port" type="number" value="27017">
|
|
||||||
</label>
|
|
||||||
</div></div>
|
|
||||||
<div class="row"><div class="columns">
|
|
||||||
<label class="inverse material textbox">
|
|
||||||
Database Name
|
|
||||||
<input name="name" type="text" value="hydro">
|
|
||||||
</label>
|
|
||||||
</div></div>
|
|
||||||
<div class="row"><div class="columns">
|
|
||||||
<label class="inverse material textbox">
|
|
||||||
Database Username
|
|
||||||
<input name="username" type="text" placeholder="Leave blank if none">
|
|
||||||
</label>
|
|
||||||
</div></div>
|
|
||||||
<div class="row"><div class="columns">
|
|
||||||
<label class="inverse material textbox">
|
|
||||||
Database Password
|
|
||||||
<input name="password" type="password" placeholder="Leave blank if none">
|
|
||||||
</label>
|
</label>
|
||||||
</div></div>
|
</div></div>
|
||||||
<div class="row"><div class="columns">
|
<div class="row"><div class="columns">
|
||||||
@@ -78,18 +52,13 @@ async function get(ctx: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function post(ctx: Context) {
|
async function post(ctx: Context) {
|
||||||
const {
|
|
||||||
host, port, name, username, password,
|
|
||||||
} = (ctx.request as any).body;
|
|
||||||
let mongourl = 'mongodb://';
|
|
||||||
if (username) mongourl += `${username}:${password}@`;
|
|
||||||
mongourl += `${host}:${port}/${name}`;
|
|
||||||
try {
|
try {
|
||||||
const Database = await MongoClient.connect(mongourl, {
|
const url = mongoUri.parse(ctx.request.body?.url);
|
||||||
|
const Database = await MongoClient.connect((ctx.request as any).body, {
|
||||||
readPreference: 'nearest',
|
readPreference: 'nearest',
|
||||||
writeConcern: new WriteConcern('majority'),
|
writeConcern: new WriteConcern('majority'),
|
||||||
});
|
});
|
||||||
const db = Database.db(name);
|
const db = Database.db(url.database);
|
||||||
const coll = db.collection<any>('system');
|
const coll = db.collection<any>('system');
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
coll.updateOne(
|
coll.updateOne(
|
||||||
@@ -104,9 +73,7 @@ async function post(ctx: Context) {
|
|||||||
),
|
),
|
||||||
]);
|
]);
|
||||||
fs.ensureDirSync(path.resolve(os.homedir(), '.hydro'));
|
fs.ensureDirSync(path.resolve(os.homedir(), '.hydro'));
|
||||||
fs.writeFileSync(path.resolve(os.homedir(), '.hydro', 'config.json'), JSON.stringify({
|
fs.writeFileSync(path.resolve(os.homedir(), '.hydro', 'config.json'), JSON.stringify({ url }));
|
||||||
host, port, name, username, password,
|
|
||||||
}));
|
|
||||||
ctx.body = '<h1>Done! Hydro is now starting.</h1>';
|
ctx.body = '<h1>Done! Hydro is now starting.</h1>';
|
||||||
resolve?.();
|
resolve?.();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
import {
|
import {
|
||||||
Collection, Db, IndexDescription, MongoClient,
|
Collection, Db, IndexDescription, MongoClient,
|
||||||
} from 'mongodb';
|
} from 'mongodb';
|
||||||
|
import mongoUri from 'mongodb-uri';
|
||||||
import { Time } from '@hydrooj/utils';
|
import { Time } from '@hydrooj/utils';
|
||||||
import { Logger } from '../logger';
|
import { Logger } from '../logger';
|
||||||
import options from '../options';
|
import options from '../options';
|
||||||
@@ -38,6 +39,7 @@ class MongoService {
|
|||||||
async start() {
|
async start() {
|
||||||
const opts = options() || {};
|
const opts = options() || {};
|
||||||
let mongourl = MongoService.buildUrl(opts);
|
let mongourl = MongoService.buildUrl(opts);
|
||||||
|
const url = mongoUri.parse(mongourl);
|
||||||
if (process.env.CI) {
|
if (process.env.CI) {
|
||||||
const { MongoMemoryServer } = require('mongodb-memory-server');
|
const { MongoMemoryServer } = require('mongodb-memory-server');
|
||||||
const mongod = await MongoMemoryServer.create();
|
const mongod = await MongoMemoryServer.create();
|
||||||
@@ -45,7 +47,7 @@ class MongoService {
|
|||||||
}
|
}
|
||||||
this.opts = opts;
|
this.opts = opts;
|
||||||
this.client = await MongoClient.connect(mongourl);
|
this.client = await MongoClient.connect(mongourl);
|
||||||
this.db = this.client.db(opts.name || 'hydro');
|
this.db = this.client.db(url.database || 'hydro');
|
||||||
await bus.parallel('database/connect', this.db);
|
await bus.parallel('database/connect', this.db);
|
||||||
setInterval(() => this.fixExpireAfter(), Time.hour);
|
setInterval(() => this.fixExpireAfter(), Time.hour);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user