UNPKG

jmms

Version:

Jmms cli tools, Jmms is a java meta-micro-service framework

104 lines (82 loc) 2.78 kB
const _ = require('lodash'); const p = require('path'); const fs = require('fs'); const log = require('./log'); const utils = require('./utils'); const initServer = (app, options) => { var _server = utils.loadJsonOrYaml(app.path('server')); if(null == _server){ _server = {}; } if(!_server.port) { _server.port = 8080; } if(!_server['context-path']) { _server['context-path'] = ''; } if(options && options.port) { const intPort = parseInt(options.port); if(!intPort) { log.error("Invalid port option '" + this.options.port + "', must be a number"); process.exit(1); } _server.port = intPort; } const api = app.api; _server.baseUrl = 'http://localhost:' + _server.port + _server['context-path']; api.baseUrl = _server.baseUrl + (api.basePath ? api.basePath : ''); return _server; } const app = { dir : undefined, //the path of app directory configPath : undefined, // the path of app config file; config : undefined, //the config.json db : undefined, //the db object in config.json api : undefined, //the api object in config.json server : undefined, //the server.json with extra info entitiesDir : undefined, init : (dir, options) => { app.dir = dir; //config app.configPath = utils.existsJsonOrYaml(p.join(dir, 'config')); app.config = utils.loadJsonOrYamlFile(app.configPath); //api if(!app.config.api) { app.config.api = {}; } app.api = app.config.api; const webConfig = utils.loadJsonOrYaml(app.path('config.web')); if(null != webConfig) { _.merge(app.config, webConfig); } if(!app.api.basePath) { app.api.basePath = ""; }else if(app.api.basePath == "/") { app.api.basePath = ""; } log.debug('api base path "' + app.api.basePath + '"'); //server app.server = initServer(app, options); //db const devConfig = utils.loadJsonOrYaml(app.path('config-dev')); if(null != devConfig) { app.db = devConfig.db; } if(!app.db || _.isEmpty(app.db)) { app.db = app.config.db; } //entities app.entitiesDir = app.path('entities'); return app; }, exists: () => { return !_.isUndefined(this.dir); }, //The path or sub path of app directory. path: (sub) => { return sub ? p.join(app.dir, sub) : app.dir; }, //The path relative to entities directory. entityPath: (file) => { return p.join(app.entitiesDir, file); } } module.exports = app;