优化安装流程

This commit is contained in:
masnn committed 2020-06-07 13:59:22 +08:00
1 parent 63d55cedf4
commit 508f5644c3
30 files changed
+279 -139

No files matched your search

+3 -3
View File
@@ -10,9 +10,9 @@ mongourl += `${options.host}:${options.port}/${options.name}`;
*/
let db = null;
Mongo.MongoClient.connect(mongourl, { useNewUrlParser: true, useUnifiedTopology: true })
.then((Database) => {
db = Database.db(options.name);
db.Db = Database;
.then((Client) => {
db = Client.db(options.name);
db.Client = Client;
bus.publish('system_database_connected', null);
});
+9 -4
View File
@@ -98,6 +98,8 @@ class Handler {
constructor(ctx) {
this.ctx = ctx;
this.request = {
host: ctx.request.host,
hostname: ctx.request.hostname,
ip: ctx.request.ip,
headers: ctx.request.headers,
cookies: ctx.cookies,
@@ -193,8 +195,8 @@ class Handler {
token.TYPE_SESSION,
this._handler.expireSeconds,
{
update_ip: this.request.ip,
update_ua: this.request.headers['user-agent'] || '',
updateIp: this.request.ip,
updateUa: this.request.headers['user-agent'] || '',
},
) : { uid: 1 };
if (!this.session) this.session = { uid: 1 };
@@ -302,6 +304,9 @@ function Route(route, RouteHandler, permission = null) {
const args = {
...ctx.params, ...ctx.query, ...ctx.request.body, domainId: 'system',
};
if (h.request.host !== await system.get('server.host')) {
args.domainId = h.request.url.split('//')[1].split(`.${await system.get('server.host')}`)[0] || '';
}
if (h.___prepare) await h.___prepare(args);
if (permission) h.checkPerm(permission);
@@ -437,8 +442,8 @@ async function start() {
app.use(morgan(':method :url :status :res[content-length] - :response-time ms'));
app.use(router.routes()).use(router.allowedMethods());
Route('*', Handler);
server.listen(await system.get('listen.port'));
console.log('Server listening at: %s', await system.get('listen.port'));
server.listen(await system.get('server.port'));
console.log('Server listening at: %s', await system.get('server.port'));
}
global.Hydro.service.server = module.exports = {
+23 -18
View File
@@ -1,8 +1,8 @@
const fs = require('fs');
const os = require('os');
const path = require('path');
const http = require('http');
const Koa = require('koa');
const morgan = require('koa-morgan');
const Body = require('koa-body');
const Router = require('koa-router');
const cache = require('koa-static-cache');
@@ -19,20 +19,11 @@ class Loader {
}
}
class Nunjucks extends nunjucks.Environment {
constructor() {
super(new Loader(), { autoescape: true, trimBlocks: true });
this.addFilter('json', (self) => JSON.stringify(self), false);
this.addFilter('base64_encode', (s) => Buffer.from(s).toString('base64'));
}
}
const env = new Nunjucks();
const env = new nunjucks.Environment(new Loader(), { autoescape: true, trimBlocks: true });
function render(name) {
return new Promise((resolve, reject) => {
env.render(name, {
typeof: (o) => typeof o,
static_url: (str) => `/${str}`,
handler: { renderTitle: (str) => str },
_: (str) => str,
}, (err, res) => {
if (err) reject(err);
@@ -40,15 +31,16 @@ function render(name) {
});
});
}
async function setup() {
const app = new Koa();
const server = http.createServer(app.callback());
const router = new Router();
app.keys = ['Hydro'];
app.use(cache(path.join(process.cwd(), '.uibuild'), {
app.use(cache(path.join(os.tmpdir(), 'hydro', 'builtin'), {
maxAge: 365 * 24 * 60 * 60,
}));
app.use(Body({ formidable: { maxFileSize: 256 * 1024 * 1024 } }));
app.use(Body());
router.get('/', async (ctx) => {
ctx.body = await render('setup.html');
ctx.response.type = 'text/html';
@@ -66,9 +58,23 @@ async function setup() {
useNewUrlParser: true, useUnifiedTopology: true,
});
const db = Database.db(name);
// Check Write Access
await db.collection('test').insertOne({ test: 'test' });
await db.collection('test').deleteOne({ test: 'test' });
await Promise.all([
db.collection('system').updateOne(
{ _id: 'server.host' },
{ $set: { value: ctx.request.host } },
{ upsert: true },
),
db.collection('system').updateOne(
{ _id: 'server.hostname' },
{ $set: { value: ctx.request.hostname } },
{ upsert: true },
),
db.collection('system').updateOne(
{ _id: 'server.url' },
{ $set: { value: ctx.request.href } },
{ upsert: true },
),
]);
fs.writeFileSync(path.resolve(process.cwd(), 'config.json'), JSON.stringify({
host, port, name, username, password,
}));
@@ -79,7 +85,6 @@ async function setup() {
}
});
});
app.use(morgan(':method :url :status :res[content-length] - :response-time ms'));
app.use(router.routes()).use(router.allowedMethods());
server.listen(8888);
console.log('Server listening at: 8888');