-
Notifications
You must be signed in to change notification settings - Fork 75
/
app.js
executable file
·183 lines (137 loc) · 5.19 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* Module dependencies.
*/
var settings
//if settings.js doesn't exist, let the user know and exit
try {
settings = require('./settings/settings.js');
} catch (e) {
console.log("No settings.js file detected in settings folder. Try copying the settings/settings.js.example to settings/settings.js and add in settings.");
return;
}
var pg = require('pg'),
express = require('express'),
http = require('http'),
path = require('path'),
settings = require('./settings/settings'),
common = require("./common"),
cors = require('cors'),
fs = require("fs"),
_ = require("underscore"),
https = require('https');
app = express();
//PostGres Connection String
global.conString = "postgres://" + settings.pg.username + ":" + settings.pg.password + "@" + settings.pg.server + ":" + settings.pg.port + "/" + settings.pg.database;
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; //fixes stream.js:94 UNABLE_TO_VERIFY_LEAF_SIGNATURE problem when using SSL
// all environments
app.set('ipaddr', settings.application.ip);
app.set('port', process.env.PORT || settings.application.port);
if (process.env.PORT) {
settings.application.port = process.env.PORT;
}
app.set('views', 'shared_views');
app.set('view engine', 'jade');
app.set('trust proxy', true);
app.enable("jsonp callback"); //TODO: Remove this if not needed because of CORS
app.use(express.favicon(path.join(__dirname, 'public/img/favicon.png')));
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('eobfgl-shoe'));
app.use(express.session());
//Set up a public folder.
app.use(require('less-middleware')({
src: __dirname + '/public'
}));
//Items in these folders will be served statically.
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'GPModels')));
//support for storing authentication credentials
var passport = { authenticationFunctions: []};
//This must be after app.use(passport.initialize())
app.use(cors());
app.use(app.router);
//Load in all endpoint routes
//Root Request - show table list
app.get('/', passport.authenticationFunctions, function (req, res) {
res.redirect('/services');
});
//Keep a list of services that are active
var services = [];
//TODO - Loop thru endpoints folder and require everything in there
var tables = require('./endpoints/tables');
app.use(tables.app(passport));
services.push({ name: "PostGres Table Endpoints", link: "/services/tables" })
var geoprocessing = require('./endpoints/geoprocessing');
app.use(geoprocessing.app(passport));
services.push({ name: "Geoprocessing", link: "/services/geoprocessing" })
var utilities = require('./endpoints/utilities');
app.use(utilities.app(passport));
services.push({ name: "Utilities", link: "/services/utilities" })
var tiles;
try {
tiles = require('./endpoints/tiles');
services.push({ name: "Static Vector Tile Services", link: "/services/vector-tiles" });
services.push({ name: "Static Image Tile Services", link: "/services/image-tiles" });
} catch (e) {
tiles = null;
console.log("Mapnik module has an error. Skipping this module. Reason: " + e);
}
if (tiles) {
app.use(tiles.app(passport));
}
var datablaster;
try {
datablaster = require('./endpoints/datablaster');
} catch (e) {
datablaster = null;
console.log("Datablaster not properly installed. Skipping. Reason: No blast_config.js file found in endpoints/datablaster");
}
if (datablaster)
app.use(datablaster.app(passport));
//Create default /services route
app.all('/services', function (req, res) {
//Display default page with list of services
var args = common.getArguments(req);
args.view = "services_list";
args.path = req.path;
args.host = settings.application.publichost || req.headers.host;
args.link = (req.secure ? "https:" : "http:") + "//" + args.host + "/services";
args.services = services;
common.respond(req, res, args);
});
//Configure HTTPS if present
if(settings.ssl && settings.ssl.pfx && settings.ssl.password){
//Use HTTPS
var SSLoptions = {
pfx: fs.readFileSync(settings.ssl.pfx),
passphrase: settings.ssl.password
};
//Create web server (https)
https.createServer(SSLoptions, app).listen(app.get('port'), app.get('ipaddr'), function() {
var startMessage = "SpatialServer listening (HTTPS)";
if (app.get('ipaddr')) {
startMessage += ' on IP:' + app.get('ipaddr') + ', ';
}
startMessage += ' on port ' + app.get('port');
console.log(startMessage);
});
}else{
//Use HTTP
//Create web server
http.createServer(app).listen(app.get('port'), app.get('ipaddr'), function () {
var startMessage = "SpatialServer listening";
if (app.get('ipaddr')) {
startMessage += ' on IP:' + app.get('ipaddr') + ', ';
}
startMessage += ' on port ' + app.get('port');
console.log(startMessage);
});
}
//Look for any errors (this signature is for error handling), this is generally defined after all other app.uses.
app.use(function (err, req, res, next) {
console.error(err.stack);
common.log(err.message);
res.send(500, 'There was an error with the web service. Please try your operation again.');
common.log('There was an error with the web servcice. Please try your operation again.');
});