This repository has been archived by the owner on May 7, 2021. It is now read-only.
forked from TheRealJon/personae-gratae
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
166 lines (143 loc) · 5.02 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
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
// OpenShift sample Node application
const express = require('express'),
avatarStorage = require('./server/avatarStorage'),
imgFilter = require('./server/imgFilter'),
handlebars = require('express-handlebars'),
navItems = require('./server/navItems'),
morgan = require('morgan'),
mongodb = require('mongodb');
multer = require('multer'),
path = require('path'),
app = express(),
upload = multer({storage: avatarStorage, fileFilter: imgFilter });
Object.assign=require('object-assign')
// Use handlebars template engine
app.set('views', __dirname+'/src/views');
app.engine('handlebars', handlebars({layoutsDir: 'src/views/layouts', defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
// Serve static files from specific folders folder
app.use('/assets', express.static('./build/assets'));
app.use('/avatars', express.static('./data/avatars'));
// Server logging
app.use(morgan('combined'));
// middleware for nav items
app.use(function(req, res, next){
navItems.forEach(function(item){
item.active = req.path.match(item.pattern) ? true : false;
});
next();
});
var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080,
ip = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0',
mongoURL = process.env.OPENSHIFT_MONGODB_DB_URL || process.env.MONGO_URL,
mongoURLLabel = '';
if (mongoURL == null && process.env.DATABASE_SERVICE_NAME) {
var mongoServiceName = process.env.DATABASE_SERVICE_NAME.toUpperCase(),
mongoHost = process.env[mongoServiceName + '_SERVICE_HOST'],
mongoPort = process.env[mongoServiceName + '_SERVICE_PORT'],
mongoDatabase = process.env[mongoServiceName + '_DATABASE'],
mongoPassword = process.env[mongoServiceName + '_PASSWORD']
mongoUser = process.env[mongoServiceName + '_USER'];
if (mongoHost && mongoPort && mongoDatabase) {
mongoURLLabel = mongoURL = 'mongodb://';
if (mongoUser && mongoPassword) {
mongoURL += mongoUser + ':' + mongoPassword + '@';
}
// Provide UI label that excludes user id and pw
mongoURLLabel += mongoHost + ':' + mongoPort + '/' + mongoDatabase;
mongoURL += mongoHost + ':' + mongoPort + '/' + mongoDatabase;
}
}
var db = null,
dbDetails = new Object();
var initDb = function(callback) {
if (mongoURL == null) {
console.log("Cannot connect to MongoDB. No URL provided.");
return;
}
if (mongodb == null) {
console.log("Cannot connect to MongoDB. No client instance is present.");
return;
}
mongodb.connect(mongoURL, function(err, conn) {
if (err) {
callback(err);
return;
}
db = conn;
dbDetails.databaseName = db.databaseName;
dbDetails.url = mongoURLLabel;
dbDetails.type = 'MongoDB';
console.log('Connected to MongoDB at: %s', dbDetails.url);
});
};
// TODO break out route handlers into separate js files for organization
app.get('/', function (req, res) {
db.collection('personas').find({}).toArray(function(err, result){
if (err) throw err
personas = result;
console.log('All personas successfully loaded');
res.render('home', {personas: result, navItems});
});
});
app.get('/persona/:id/card', function(req, res){
var id = new mongodb.ObjectID(req.params.id);
db.collection('personas').find({ _id: id }).toArray(function(err, result){
if(err) throw err;
if(result.length > 0){
res.render('persona-card', {persona: result[0], navItems});
} else {
res.render('404');
}
});
});
app.get('/persona/:id/details', function(req, res){
// TODO retrieve persona from mongodb
var id = new mongodb.ObjectID(req.params.id);
db.collection('personas').find({ _id: id }).toArray(function(err, result){
if(err) throw err;
if(result.length > 0){
res.render('persona-details', {persona: result[0], navItems});
} else {
res.render('404');
}
});
})
app.get('/create', function(req, res){
res.render('create-persona', {navItems});
});
app.post('/create', upload.single('photo'), function(req, res){
var persona = {};
persona.name = req.body.name;
persona.jobTitle = req.body.jobTitle;
persona.keysToSuccess = req.body.keysToSuccess;
persona.dangers = req.body.dangers;
persona.quote = req.body.quote;
persona.network = req.body.quote;
persona.photo = '/avatars/' + req.file.filename;
persona.network = req.body.network;
persona.dayInTheLife = {};
persona.skills = [];
persona.dayInTheLife.summary = req.body.dayInTheLife;
req.body.skills.forEach(function(skill, index){
persona.skills.push({
name: skill,
rating: req.body.ratings[index]
});
});
db.collection('personas').insertOne(persona, function(err, res){
if (err) throw err;
});
res.redirect('/');
});
// error handling
app.use(function(err, req, res, next){
console.error(err.stack);
res.status(500).send('Something bad happened!');
});
initDb(function(err){
console.log('Error connecting to Mongo. Message:\n'+err);
});
app.listen(port, ip);
console.log('Server running on http://%s:%s', ip, port);
module.exports = app;