This repository has been archived by the owner on May 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
128 lines (99 loc) · 2.52 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
'use strict';
var path = require('path');
var fs = require('fs');
var express = require('express');
var app = express();
var React = require('react');
var Compiler = require('songdown-compiler');
/*
* =====
* UTILS
* =====
*/
var strToBool = function(str) {
if (str === 'true') {
return true;
} else {
return false;
}
};
var strToInt = function(str) {
return parseInt(str, 10);
}
/*
* =============
* CONFIGURATION
* =============
*/
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
/*
* ==========
* MIDDLEWARE
* ==========
*/
// Do this to make sure we're always using https. This is so that we avoid issues with protocols.
if (process.env.NODE_ENV === 'production') {
app.use(function(req, res, next) {
if (req.headers['X-Forwarded-Proto'] === 'https') {
res.redirect('https://' + req.headers.host + req.url);
} else {
next();
}
});
}
app.use('/static', express.static('public'));
/*
* ======
* ROUTES
* ======
*/
var toApp = function(req, res, next) {
req.url = '/';
next();
};
app.get('/edit', toApp);
app.get('/edit/:artist/:name', toApp);
app.get('/song', toApp);
app.get('/song/:artist/:name', toApp);
app.get('/404', toApp);
app.get('/', function(req, res) {
var protocol = process.env.NODE_ENV === 'production' ? 'https://' : 'http://';
var base = protocol + req.get('host') + '/';
var jsonFile = path.join(__dirname, 'public/songs.json');
var partials = {
scriptUrl: base + 'static/app.js',
songsJsonUrl: process.env.NODE_ENV !== 'production' && fs.existsSync(jsonFile) ?
base + 'static/songs.json'
: 'https://raw.githubusercontent.com/1vasari/songdown-songs/master/songs.json'
};
res.render('index', partials);
});
app.get('/print', function(req, res) {
// Sort out types and stuff.
var compilerOptions = {
fontSize: strToInt(req.query.fontSize),
showChords: strToBool(req.query.showChords),
showComments: strToBool(req.query.showComments),
showGOTOs: strToBool(req.query.showGOTOs),
source: req.query.source,
theme: req.query.theme,
transpose: strToInt(req.query.transpose)
};
var partials = {
artist: req.query.artist,
html: React.renderToStaticMarkup(React.createElement(Compiler, compilerOptions)),
key: req.query.key,
name: req.query.name
};
res.render('print', partials);
});
/*
* =========
* LISTENING
* =========
*/
var port = process.env.PORT || 5000;
app.listen(port, function() {
console.log('Listening on port %d for requests.', port);
});