/**
 * Created by zhang on 2019/12/3.
 */
let express = require('express');

let config = require("./config/config.js");
let fileUtils = require("./modules/common/fileUtils");
let dbm = require("./config/db/db_manager");
let log = require("./logs/log_helper");

config.setupDb(process.env.NODE_ENV);

let path = require('path');
let fs = require('fs');


dbm.connect(process.env.NODE_ENV);
//这里现在只引入了定额工料机里的models,当其它模块的models修改后使用：./modules/**/models/*.js引入所有的模块
fileUtils.getGlobbedFiles('./modules/all_models/*.js').forEach(function(modelPath) {
    require(path.resolve(modelPath));
})




let app = express();
let _rootDir = __dirname;
app.locals.rootDir = _rootDir;
log.use(app);
app.use(express.static(_rootDir));

app.set('views', path.join(__dirname, 'web'));
app.engine('.html', require('ejs').__express);
app.set('view engine', 'html');

let bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({limit: '100mb', extended: false}));
app.use(bodyParser.json({limit: '100mb'}));



//加载路由文件
fileUtils.getGlobbedFiles('./modules/import/routes/*.js').forEach(function(modelPath) {
    require(path.resolve(modelPath))(app);
});

//app.use(express.static(_rootDir+"/web"));
//app.use(express.static(_rootDir+"/lib"));
//let rations_Router = require("./modules/rationLibEditor/routes/rationLibEditor_route");
//app.use("/rationLibEditor",rations_Router);

//-----------------

app.use(function(req, res, next) {
    res.status(404).send('404 Error');
});
app.use(function(err, req, res, next) {
    console.error(err.stack);
    res.status(500).send('500 Error');
});

//设置外增的Date对象Format函数
//备注： 经过测试nodejs 8.9.3版本不支持eval的方式修改prototype，为兼容考虑，把方法调整到stringUtil文件里
require('./public/stringUtil').setupDateFormat();

app.listen(2050, function(){
    console.log('import server started!');
});

