|
| 1 | +const |
| 2 | + log = require('./lib/log.js').get('Worker'), |
| 3 | + bson = require('bson'), |
| 4 | + Koa = require('koa'), |
| 5 | + Router = require('koa-router'), |
| 6 | + MongoStore = require('koa-session2-mongostore'), |
| 7 | + path = require('path'), |
| 8 | + mongo = require('mongodb'), |
| 9 | + EventEmitter = require('events'); |
| 10 | + |
| 11 | +module.exports = class Hydro extends EventEmitter { |
| 12 | + constructor(Hydro_config) { |
| 13 | + super(); |
| 14 | + this.cfg = Hydro_config; |
| 15 | + this.config = {}; |
| 16 | + this.locales = {}; |
| 17 | + this.lib = {}; |
| 18 | + this.status = {}; |
| 19 | + this.sockets = []; |
| 20 | + this.routes = []; |
| 21 | + this.routers = []; |
| 22 | + this.serviceID = new bson.ObjectID(); |
| 23 | + this.sessionOpt = { |
| 24 | + store: new MongoStore({ |
| 25 | + collection: 'session', |
| 26 | + url: this.cfg.db_url, |
| 27 | + dbName: this.cfg.db_name, |
| 28 | + collName: 'session' |
| 29 | + }) |
| 30 | + }; |
| 31 | + this.app = new Koa(); |
| 32 | + this.app.keys = this.cfg.keys || ['Hydro']; |
| 33 | + this.router = new Router(); |
| 34 | + this.server = require('http').createServer(this.app.callback()); |
| 35 | + this.io = require('socket.io')(this.server, { cookie: true }); |
| 36 | + } |
| 37 | + async load() { |
| 38 | + await this.connectDatabase(); |
| 39 | + await this.mountLib(); |
| 40 | + await require('./lib/deploy.js')(this.db, this.lib); |
| 41 | + this.app.use((require('./modules/trace/index.js'))({ |
| 42 | + sourceMap: false |
| 43 | + })); |
| 44 | + this.app.use(require('./modules/static')(path.join(__dirname, 'public'), false)); |
| 45 | + this.app.use(require('koa-morgan')(':method :url :status :res[content-length] - :response-time ms')); |
| 46 | + this.app.use(require('koa-body')({ |
| 47 | + patchNode: true, |
| 48 | + multipart: true, |
| 49 | + formidable: { |
| 50 | + uploadDir: path.join(__dirname, 'uploads'), |
| 51 | + keepExtensions: true |
| 52 | + } |
| 53 | + })); |
| 54 | + this.app.use(require('koa-session2')(this.sessionOpt)); |
| 55 | + this.app.use(async (ctx, next) => { |
| 56 | + let that = this; |
| 57 | + ctx.state.url = function (name, params) { |
| 58 | + let route = that.router.route(name); |
| 59 | + if (route) { |
| 60 | + let args = Array.prototype.slice.call(arguments, 1); |
| 61 | + return route.url.apply(route, args); |
| 62 | + } |
| 63 | + return '#'; |
| 64 | + }; |
| 65 | + ctx.state._ = msg => this.locales[ctx.state.user.language][msg] || msg; |
| 66 | + await next(); |
| 67 | + }); |
| 68 | + //TODO(masnn) Load main routes. |
| 69 | + this.app.use(this.router.routes()).use(this.router.allowedMethods()); |
| 70 | + this.status.loaded = true; |
| 71 | + } |
| 72 | + async listen() { |
| 73 | + await this.server.listen(this.cfg.port); |
| 74 | + this.status.listening = true; |
| 75 | + } |
| 76 | + async stop() { |
| 77 | + await this.server.close(); |
| 78 | + this.status.listening = false; |
| 79 | + } |
| 80 | + async destory() { |
| 81 | + |
| 82 | + } |
| 83 | + async restart() { |
| 84 | + process.emit('restart'); |
| 85 | + } |
| 86 | + async connectDatabase() { |
| 87 | + try { |
| 88 | + let Database = await require('mongodb').MongoClient.connect( |
| 89 | + this.cfg.db_url, { useNewUrlParser: true, useUnifiedTopology: true } |
| 90 | + ); |
| 91 | + this.db = Database.db(this.cfg.db_name); |
| 92 | + this.gfs = require('gridfs')(this.db, mongo); |
| 93 | + } catch (e) { |
| 94 | + log.error('Unable to connect to database.'); |
| 95 | + log.error(e); |
| 96 | + process.exit(1); |
| 97 | + } |
| 98 | + } |
| 99 | + async mountLib() { |
| 100 | + let conf = require('./lib/config.js'); |
| 101 | + this.lib.conf = new conf({ db: this.db }); |
| 102 | + let user = require('./lib/user.js'); |
| 103 | + this.lib.user = new user({ db: this.db }); |
| 104 | + } |
| 105 | +}; |
0 commit comments