-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
112 lines (95 loc) · 3.17 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// server.js
// require modules
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var unirest = require('unirest');
var express = require('express');
var telegramController = require('./app/controllers/telegram_controller');
// app init
var app = express();
// app.setMaxListeners(0);
require('events').EventEmitter.defaultMaxListeners = Infinity;
// require mongoose models
var AuthApplication = require('./app/models/auth_application');
// use bodyParser
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
// Mongoose promise lib
mongoose.Promise = global.Promise;
// db connection
mongoose.connect('mongodb://localhost:27017/Mhint');
// set port
var port = process.env.PORT || 3000;
// logs and authorization
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
// if (req.url == "/apikey") {
// var date = new Date();
// console.log("["+date+"][DEVREQUEST]["+req.method+"]["+req.url+"]");
// next();
// } else {
// AuthApplication.findOne({api_key: req.headers.authorization}).exec(function(err, app){
// if (err) {
// res.send(err);
// } else {
// if (app) {
// if (app.status == "pending") {
// res.json({status: 401 ,message: "unauthorized, your api_key request in pending"});
// } else if (app.status == "revoked") {
// res.json({status: 401 ,message: "unauthorized, your api_key was revoked"});
// } else if (app.status == "active") {
// var date = new Date();
// console.log("["+date+"]["+app.app_name+"]["+req.method+"]["+req.url+"]");
// next();
// } else {
// res.json({status: 401 ,message: "unauthorized"});
// }
// } else {
// res.json({status: 401 ,message: "unauthorized"});
// }
// }
// });
// }
if (req.headers.authorization) {
console.log(req.headers.authorization);
}
var date = new Date();
var append = "";
if (req.body.mail) {
append = "[" + req.body.mail + "]";
} else if (req.query.mail) {
append = "[" + req.query.mail + "]";
} else if (req.query.id) {
append = "[" + req.query.id + "]";
}
console.log("[" + date + "][" + req.method + "][" + req._parsedUrl.pathname + "]" + append);
next();
});
// User Router
var user_router = require('./app/routers/user_router');
app.use(user_router);
// Food Router
var food_router = require('./app/routers/food_router');
app.use(food_router);
// Need Router
var need_router = require('./app/routers/need_router');
app.use(need_router);
// Shopping Router
var shopping_router = require('./app/routers/shopping_router');
app.use(shopping_router);
// Allergenic Diet Router
var allergenic_diet_router = require('./app/routers/allergenic_diet_router');
app.use(allergenic_diet_router);
// Chatbot Router
var chatbot_router = require('./app/routers/chatbot_router');
app.use(chatbot_router);
// Chatbot Router
var admin_router = require('./app/routers/admin_router');
app.use('/admin', admin_router);
// start server
app.listen(port);
console.log('Server started on port ' + port);
telegramController.init();