Blame view

app.js 3.32 KB
李奇 authored
1
/**
2
 * yoho-activity-platform app
李奇 authored
3 4 5 6
 * @author: leo<qi.li@yoho.cn>
 * @date: 2017/6/23
 */
'use strict';
李奇 authored
7
const cors = require('cors');
李奇 authored
8
const path = require('path');
李奇 authored
9
const express = require('express');
陈峰 authored
10
const config = require('./config/common');
李奇 authored
11 12
const bluebird = require('bluebird');
const ynLib = require('yoho-node-lib');
陈峰 authored
13
const {SqlHelper, Yoho} = require('./utils');
李奇 authored
14
const bodyParser = require('body-parser');
李奇 authored
15
const compression = require('compression');
李奇 authored
16
const cookieParser = require('cookie-parser');
李奇 authored
17
const cookieSession = require('cookie-session');
李奇 authored
18
const favicon = require('serve-favicon');
陈峰 authored
19 20 21
const moment = require('moment');
const _ = require('lodash');
const pkg = require('./package.json');
邱骏 authored
22
const Sender = require('influx-batch-sender');
李奇 authored
23
邱骏 authored
24
李奇 authored
25 26 27
let logger;
const app = express();
陈峰 authored
28
app.locals.devEnv = app.get('env') === 'development';
李奇 authored
29
app.locals.isTest = app.get('env') === 'test3';
陈峰 authored
30 31 32 33
app.locals.isProduction = app.get('env') === 'production';
app.locals.version = pkg.version;
app.locals.startTime = moment().format('YYYYMMDDHH');
邱骏 authored
34
// config.yohoVerifyUdid = '1a61c0b4db7b6e27999b1237977b5347eb503956'; // 临时用于红包雨的udid
邱骏 authored
35
李奇 authored
36 37 38 39
// 全局注册library
ynLib.global(config);
logger = global.yoho.logger;
陈峰 authored
40 41 42 43 44 45
// zookeeper
if (config.zookeeperServer) {
    require('yoho-zookeeper')(config.zookeeperServer, 'yap', app.locals.yap = {}, false, {});
}

李奇 authored
46
global.Promise = bluebird;
陈峰 authored
47
global.yoho.utils = {
陈峰 authored
48 49
    mysqlCli: new SqlHelper(config.mysql.database),
    yoho: Yoho
陈峰 authored
50
};
李奇 authored
51
邱骏 authored
52 53
global.yoho.sender = new Sender(config.monitorReport); // 初始化数据上报
李奇 authored
54 55 56 57 58 59
app.use(cookieSession({
    name: 'yoho_activity',
    secret: 'activity@yoho',
    maxAge: 24 * 60 * 60 * 1000
}));
app.use(compression());
李奇 authored
60
app.use(favicon(path.join(__dirname, '/favicon.ico')));
陈峰 authored
61
app.use(express.static(path.join(__dirname, 'public/dist')));
李奇 authored
62 63 64 65 66
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: false
}));
app.use(cookieParser());
陈峰 authored
67 68
app.use((req, res, next) => {
    req.user = {}; // 全局的用户数据
李奇 authored
69
    req.yoho = {}; // req和res绑定yoho对象,用于传递全局数据
陈峰 authored
70
    req.cookies.from = 'action';
陈峰 authored
71 72 73

    next();
});
李奇 authored
74
app.use(global.yoho.httpCtx());
李奇 authored
75 76
app.enable('trust proxy');
李奇 authored
77 78 79 80 81
app.use(global.yoho.hbs({
    extname: '.hbs',
    defaultLayout: 'layout',
    layoutsDir: path.join(__dirname, 'doraemon/views'),
    partialsDir: path.join(__dirname, 'doraemon/views/partial'),
陈峰 authored
82 83
    views: path.join(__dirname, 'doraemon/views'),
    helpers: _.assign(global.yoho.helpers, require('./utils/helpers'))
李奇 authored
84 85 86
}));

李奇 authored
87 88
try {
李奇 authored
89
    // 允许跨域
李奇 authored
90 91
    app.use(cors({
        credentials: true,
李奇 authored
92
        origin: config.corsAllowOrigin
李奇 authored
93
    }));
李奇 authored
94
陈峰 authored
95
    const setYohoData = require('./doraemon/middleware/set-yoho-data');
李奇 authored
96
    const errorHandler = require('./doraemon/middleware/error-handler');
陈峰 authored
97 98
    const devtools = require('./doraemon/middleware/devtools');
陈峰 authored
99
    app.use(setYohoData());
陈峰 authored
100 101 102 103
    if (app.locals.devEnv) {
        app.use(devtools());
    }
郝肖肖 authored
104 105
    // docker验证项目是否正常发布
    app.use('/node/status.html', (req, res) => {
陈峰 authored
106
        return res.status(200).end();
郝肖肖 authored
107 108
    });
陈峰 authored
109
    require('./dispatch')(app);
李奇 authored
110
    app.all('*', errorHandler.notFound); // 404
李奇 authored
111
陈峰 authored
112
    // YOHO 后置中间件
李奇 authored
113
    app.use(errorHandler.serverError);
李奇 authored
114 115 116 117 118 119

} catch (err) {
    logger.error(err);
}

app.listen(config.port, function() {
120
    logger.info(`yoho-activity-platform started successfully, listening on port:${config.port}`);
李奇 authored
121
});