forked from votesavvyrhok/votesavvy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
114 lines (79 loc) · 2.99 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
/*jshint node:true*/
//------------------------------------------------------------------------------
// node.js starter application for Bluemix
//------------------------------------------------------------------------------
require('dotenv').load();
// This application uses express as it's web server
// for more info, see: http://expressjs.com
var express = require('express');
// cfenv provides access to your Cloud Foundry environment
// for more info, see: https://www.npmjs.com/package/cfenv
var cfenv = require('cfenv');
var https = require('https');
var JSON = require('JSON');
var database;
// create a new express server
var app = express();
// routing
require('./routes/represent')(app);
// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/public');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
// get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();
// start server on the specified port and binding host
app.listen(appEnv.port, appEnv.bind, function () {
// print a message when the server starts listening
console.log('server starting on ' + appEnv.url);
});
var dbCredentials = {
databaseName: 'voters'
};
var cloudant;
function useDatabase(callback) {
cloudant.db.create(dbCredentials.databaseName, function (err, res) {
if (err) {
console.log('database already created');
}
});
cloudant.db.list(function (err, all_dbs) {
console.log('All my databases: %s', all_dbs.join(', '))
});
database = cloudant.use(dbCredentials.databaseName);
callback();
}
function initializeDatabase(callback) {
if (process.env.VCAP_SERVICES) {
var vcapServices = JSON.parse(process.env.VCAP_SERVICES);
if (vcapServices.cloudantNoSQLDB) {
var credentials = vcapServices.cloudantNoSQLDB[0].credentials;
dbCredentials.host = credentials.host;
dbCredentials.port = credentials.port;
dbCredentials.user = credentials.username;
dbCredentials.password = credentials.password;
dbCredentials.url = credentials.url;
}
cloudant = require('cloudant')(dbCredentials.url);
useDatabase(callback);
}
else {
if (process.env.cloudant_hostname && process.env.cloudant_username && process.env.cloudant_password) {
dbCredentials.host = process.env.cloudant_hostname;
dbCredentials.user = process.env.cloudant_username;
dbCredentials.password = process.env.cloudant_password;
cloudant = require('cloudant')({
hostname: dbCredentials.host,
account: dbCredentials.user,
password: dbCredentials.password
});
useDatabase(callback);
}
}
}
var apiRequire=function(){
require('./routes/api')(app,cloudant);
}
initializeDatabase(apiRequire);
console.log('votesavvy application running');