forked from 3D-Hearts/Cardiac-Cases
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
362 lines (296 loc) · 11.8 KB
/
server.js
File metadata and controls
362 lines (296 loc) · 11.8 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
const express = require('express');
const Handlebars = require('handlebars');
const exphbs = require('express-handlebars');
const PORT = process.env.PORT || 5100;
const app = express();
const bcrypt = require('bcrypt');
const passport = require('passport')
const flash = require('express-flash')
const session = require('express-session')
const methodOverride = require('method-override')
//////////// GET STORYBLOK CONTENT //////////////
var StoryblokClient = require('storyblok-js-client')
// These variables will contain the information to be templated
var ben = new Object();
var ayanthi = new Object();
var amin = new Object();
var lucy = new Object();
// To compare login details for authentication
let users = []
const Storyblok = new StoryblokClient({
accessToken: "cxBOVdDowPMBH7h41jTGuQtt"
})
Storyblok.get('cdn/stories', {
version: 'published'
})
.then((response) => {
response.data.stories.forEach(async (story) => {
// // The following commented code is an attempt to dynamically generate sections depending on
// // the amount of scheme in Storyblok. I was not able to get it to work so we have to just
// // stick with the sections already defined in Storyblok for now.
// // Grab the content section so we can template the relevant information
// var content = story.content
// // Parse the 'Define Sections' schema to grab the titles of the sections to template
// var section_titles = story.content.define_sections.split("\n")
// // For each section name we'll add it to the Object we defined earlier
// section_titles.forEach((title) => {
// ben[`${title}`] = content[`${title}`].split("\n")
// })
// ben.xray = `https://s3.amazonaws.com${content.xray.slice(1)}`
// ben.name = content.name
// console.log(ben)
switch (story.content.name) {
case "Ben":
ben = story.content
break
case "Ayanthi":
ayanthi = story.content
break
case "Amin":
amin = story.content
break
case "Lucy":
lucy = story.content
break
default:
break
}
})
})
.catch((error) => {
console.log(error)
})
/////////////////////////////////////////////////
//////////// HANDLEBARS HELPERS /////////////////
// Custom function helper to create lists
Handlebars.registerHelper("list", function (context) {
// The lists in Storyblok have bullet points starting with '--'
if (context.includes("--")) {
// We'll split the list by '--' to give us arrays to iterate over
const lines = context.split("--")
var list = "<p class='case-content'>"
// Iterate over the lines, sandwiching them inbetween <li> tags, then add them into the list var
for (i in lines) {
if (i == 0) {
list = list + lines[i]
} else {
list = list + "<br><span>• </span>" + lines[i]
// list = list + "<li>" + lines[i] + "</li>"
}
}
return new Handlebars.SafeString(list + "</p>")
} else {
return new Handlebars.SafeString("<p class='case-content'>" + context + "</p>")
}
})
Handlebars.registerHelper("review", function (context) {
if (context === "<<start>>" || context === "<<end>>") return
// The lists in Storyblok have bullet points starting with '--'
if (context.includes("--")) {
// We'll split the list by '--' to give us arrays to iterate over
const lines = context.split("--")
var list = "<ul>"
// Iterate over the lines, sandwiching them inbetween <li> tags, then add them into the list var
for (i in lines) {
if (i == 0) {
list = list + lines[i]
} else {
list = list + "<li>" + lines[i] + "</li>"
}
}
return new Handlebars.SafeString(list + "</ul>")
} else {
return new Handlebars.SafeString("<li>" + context + "</li>")
}
})
Handlebars.registerHelper("interactionSteps", function (context) {
return new Handlebars.SafeString("<p class='step'>" + context + "</p>")
})
Handlebars.registerHelper('toLowerCase', function(str) {
return str.toLowerCase();
})
Handlebars.registerHelper('makeModuleSlides', function(context, heading) {
//to have dynamic sections, have ben.preInteraction and ben.postInteraction instead of passing the different sections(preliminary info, background info, oe, interaction, explanation)
// Split the Storyblok section into an array of lines
const lines = context.split("\n")
// Iterate over lines
for(i in lines) {
// ... to check for either the start or end of the section
if(lines[i] === "<<start>>") return new Handlebars.SafeString("<section class='content module-slide'><h1>" + heading + "</h1>")
if(lines[i] === "<<end>>") return new Handlebars.SafeString("</section>")
// ... to check if the line is a list of bullet points
if(lines[i].includes("--")) {
// We'll split the line by "--" to make an array of bullet points
const points = lines[i].split("--")
let list = "<p>"
for (j in points) {
if (j == 0) {
list = list + points[j]
} else {
list = list + "<br><span>• </span>" + points[j]
}
}
return new Handlebars.SafeString(list + "</ul>")
// Otherwise, it's a straight up sentence and just return it
} else {
return new Handlebars.SafeString("<p>" + lines[i] + "</p>")
}
}
})
Handlebars.registerHelper('nameCheck', function(caseName, nameCheck) {
return caseName === nameCheck
})
/////////////////////////////////////////////////
// Passport is used for authentication
const initializePassport = require('./passport-config')
initializePassport(
passport,
username => users.find(user => user.username === username),
id => users.find(user => user.id === id)
)
app.use(express.static(__dirname + '/public'))
app.use(express.urlencoded({ extended: false }))
app.use(flash())
app.use(session({
secret: bcrypt.hash(Date.now().toString(), 10).toString(),
resave: false,
saveUninitialized: false
}))
app.use(passport.initialize())
app.use(passport.session())
app.use(methodOverride('_method'))
app.get('/', checkAuthenticated, (req, res) => {
res.render('index', {
title: '3D Hearts Workshop'
})
})
app.get('/index.html', checkAuthenticated, (req, res) => {
res.redirect('/')
})
app.get('/login', checkNotAuthenticated, (req, res) => {
res.render('login')
Storyblok.get('cdn/stories', {
version: 'published'
})
.then((response) => {
response.data.stories.forEach(async (story) => {
if (story.name == "login") {
users.push({
id: Date.now().toString(),
username: story.content.username,
password: await bcrypt.hash(story.content.password, 10)
})
}
})
})
})
app.post('/login', checkNotAuthenticated, passport.authenticate('local', {
successRedirect: '/',
failureRedirect: '/login',
failureFlash: true
}))
app.get('/pre-workshop-module', checkAuthenticated, (req, res) => {
res.render('pre-workshop-module', {
title: "Pre-Workshop Module"
})
})
app.get('/:heartId-:stage.html', checkAuthenticated, (req, res) => {
// These variables used to call the .hbs file according to the URL
var heartId = req.params.heartId
var stage = req.params.stage
// pass story information into client depending on heartId
switch (heartId) {
// Ben
case "17040":
res.render(`${stage}`, {
title: `Ben, a 3-month old boy`,
name: ben.name,
heartId: heartId,
preliminary_information: ben.preliminary_information.split("\n"),
background_history: ben.background_history.split("\n"),
on_examination: ben.on_examination.split("\n"),
differential_diagnoses: ben.differential_diagnoses.split("\n"),
xray: `https://s3.amazonaws.com${ben.xray.slice(1)}`,
model: `https://s3.amazonaws.com${ben.model.slice(1)}`,
cardiac_explanation: ben.cardiac_explanation.split("\n"),
explanation: ben.explanation.split("\n")
})
break
// Ayanthi
case "19401":
res.render(`${stage}`, {
title: `Ayanthi, a 1-month old girl`,
name: ayanthi.name,
heartId: heartId,
preliminary_information: ayanthi.preliminary_information.split("\n"),
background_history: ayanthi.background_history.split("\n"),
on_examination: ayanthi.on_examination.split("\n"),
differential_diagnoses: ayanthi.differential_diagnoses.split("\n"),
xray: `https://s3.amazonaws.com${ayanthi.xray.slice(1)}`,
model: `https://s3.amazonaws.com${ayanthi.model.slice(1)}`,
cardiac_explanation: ayanthi.cardiac_explanation.split("\n"),
explanation: ayanthi.explanation.split("\n")
})
break
// Amin
case "16751":
res.render(`${stage}`, {
title: `Amin, a 6-month old boy`,
name: amin.name,
heartId: heartId,
preliminary_information: amin.preliminary_information.split("\n"),
background_history: amin.background_history.split("\n"),
on_examination: amin.on_examination.split("\n"),
differential_diagnoses: amin.differential_diagnoses.split("\n"),
xray: `https://s3.amazonaws.com${amin.xray.slice(1)}`,
model: `https://s3.amazonaws.com${amin.model.slice(1)}`,
cardiac_explanation: amin.cardiac_explanation.split("\n"),
explanation: amin.explanation.split("\n")
})
break
// Lucy
case "19863":
res.render(`${stage}`, {
title: `Lucy, a 7 day old neonate`,
name: lucy.name,
heartId: heartId,
preliminary_information: lucy.preliminary_information.split("\n"),
background_history: lucy.background_history.split("\n"),
on_examination: lucy.on_examination.split("\n"),
progress: lucy.progress.split("\n"),
differential_diagnoses: lucy.differential_diagnoses.split("\n"),
xray: `https://s3.amazonaws.com${lucy.xray.slice(1)}`,
model: `https://s3.amazonaws.com${lucy.model.slice(1)}`,
cardiac_explanation: lucy.cardiac_explanation.split("\n"),
explanation: lucy.explanation.split("\n")
})
break
default:
break
}
})
app.use(function (req, res) {
res.status(404).render('404')
})
app.engine('.hbs', exphbs({
defaultLayout: 'main',
extname: '.hbs',
partialsDir: 'views/components'
}));
app.set('view engine', '.hbs')
app.set('views', 'views')
function checkAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next()
}
return res.redirect('/login')
}
function checkNotAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return res.redirect('/')
}
next()
}
app.listen(PORT, function () {
console.log(`Server running on port ${PORT}`)
})