This repository has been archived by the owner on Oct 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
149 lines (127 loc) · 4.54 KB
/
app.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var options = processArguments(process.argv.slice(2));
if(options.sourceFile) {
var fs = require('fs');
var cool = require('cool-ascii-faces');
var appPath = __dirname;
//Web Modules
var express = require('express');
var bodyParser = require('body-parser');
var url = require('url');
var app = express();
//Activating Settings
var getCalls = 0;
var postCalls = 0;
var settings = JSON.parse(fs.readFileSync(options.sourceFile));
if (process.env.VERBOSE) {
console.log(settings);
}
//Own Modules
var Database = require(appPath + '/lib/db');
var Services = require(appPath + '/lib/serviceManager');
var db = new Database(settings.db);
var serviceManager = new Services(settings, db);
//Setting up Web Server
app.set('port', (settings.port || options.port || process.env.PORT || 9001));
app.use(express.static(appPath + '/public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(function (req, res, next) {
res.header('Content-Type', 'application/json');
if (settings.CORS.allow_remote) {
res.header("Access-Control-Allow-Origin", settings.CORS.routes);
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
}
next();
});
app.get('/', function (request, response) {
getCalls++;
var result = '';
var rData = url.parse(request.url, true);
var times = process.env.TIMES || 5;
if (process.env.VERBOSE) {
console.log("//---------------------GET Request" + getCalls + "---------------------//");
console.log(JSON.stringify(rData, undefined, 4));
}
for (i = 0; i < times; i++)
result += (i + 1) + ".- " + cool() + "<br/>";
response.send(result);
});
app.post('*', function (request, response) {
postCalls++;
var data = request.body;
var rData = url.parse(request.url, true);
if (process.env.VERBOSE) {
console.log("//---------------------POST Request#" + postCalls + "---------------------//");
console.log(JSON.stringify(rData, undefined, 4));
}
serviceManager.handleService(rData.pathname, data, function (err, result) {
if (err) {
if (err == 404) {
//Servicio no encontrado
response.writeHead(404);
response.end();
}
else {
//Error en el servicio
response.writeHead(err);
response.end(result);
}
} else {
response.end(result);
}
});
});
app.listen(app.get('port'), function () {
console.log("Express app is running at localhost:" + app.get('port'))
});
} else{
if(options.help)
printHelp();
else
console.log("The app need a file to serve, use -h to help");
}
//---------------------------------------------------------------------------------------
//Utilities functions
function processArguments(arguments) {
var options = {};
if (arguments.length) {
for(var i = 0; i < arguments.length; i++){
arg = arguments[i];
switch (arg) {
case "-v":
options.VERBOSE = process.env.VERBOSE = true;
break;
case "-s":
options.sourceFile = arguments[i + 1];
break;
case "-p":
options.port = parseInt(arguments[i + 1]);
break;
case "-h":
options.help = true;
}
}
//Default values
}
if (process.env.VERBOSE) {
console.log("-----------------Program Options-----------------");
console.log(JSON.stringify(options, undefined, 4));
}
return options;
}
function printHelp(){
console.log("//---------------------------//");
console.log("easyqJS - Help - WELCOME");
console.log("");
console.log("Use: node app.js [options] -s your_services.json");
console.log("");
console.log("\tOptions: ");
console.log("\t-h\t\tPrint this help");
console.log("\t-v\t\tActivate VERBOSE mode in the app");
console.log("\t-p\t\tSet a port for runtime server - DEFAULT:9001");
console.log("");
console.log("Example: node app.js -p 8080 -s services.json");
console.log("//---------------------------//");
}