This repository has been archived by the owner on May 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
195 lines (164 loc) · 4.41 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
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
var isProduction = (process.env.NODE_ENV === 'production');
var http = require('http');
var port = (isProduction ? 80 : 8000);
var url = require('url');
var async = require('async');
var consolidate = require('consolidate'),
express = require('express'),
swig = require('swig'),
dynoSrc = require('dynosrc'),
git = dynoSrc.git,
_ = require('underscore'),
marked = require('marked'),
fs = require('fs'),
app = express();
marked.setOptions({
gfm: true,
tables: true,
breaks: false,
pedantic: false,
sanitize: true,
smartLists: true,
smartypants: false,
langPrefix: 'lang-'
});
// BEGIN DYNOSOURCE CONFIG
dynoSrc.globals({
assetsDir: __dirname + '/assets'
});
app.use(dynoSrc.middleware());
dynoSrc.assets({
'ryanstevens/ModelFlow': {
filename: 'package.json',
source: 'git',
head: '8050f1'
},
'jquery': {
head: '1.9.1',
source: 'asset'
},
'backbone-min': {
head: '1.0.0',
source: 'asset'
},
'home': {
head: '0.0.1',
source: 'asset'
},
// for dynoProxy tests - do not remove
'proxy': {
head: '0.0.1',
source: 'asset'
},
'water': {
head: '0.0.1',
source: 'asset'
},
'leash': {
head: '0.0.1',
source: 'asset'
}
});
// END DYNOSOURCE
app.use('/img', express.static(__dirname + '/src/img'));
app.use('/js', express.static(__dirname + '/src/js'));
app.use('/css', express.static(__dirname + '/build/css'));
app.engine('html', consolidate.swig);
app.set('view engine', 'html');
app.set('views', __dirname + '/build/html');
// Wtf is wrong with swig? This works locally, but not when we deploy.
swig.setDefaults({ cache: false });
swig.setFilter('scriptsafe', function (input) {
return input.replace(/\r?\n/gm, '\\n').replace(/(['"])/gm, '\\$1');
});
function serveAnimal(animal, res) {
fs.createReadStream(__dirname + '/'+animal+'/index.html').pipe(res);
}
app.get('/', function (req, res) {
var subdomain = req.get('host').split('.')[0].toLowerCase();
if (subdomain === 'dogs') return serveAnimal('dogs', res);
if (subdomain === 'cats') return serveAnimal('cats', res);
dynoSrc.getPatches(req, {
patches: ['jquery', 'home']
}, function(err, patches) {
// Notice that we're setting cache:false here, hack to work around server
// issues. But this seems to have no effect locally, have to use the
// setDefaults call above.
res.render('home.html', {
title: 'Home',
patches: patches,
cache: false
});
});
});
app.get('/testdrive', function (req, res) {
res.render('testdrive.html', {
title: 'Test Drive',
cache: false
});
});
app.get('/credits', function (req, res) {
res.render('credits.html', {
title: 'Credits',
cache: false
});
});
app.get('/proxy-demo', function (req, res) {
res.render('proxy-demo.html', {
title: 'dynoProxy Demo',
cache: false
});
});
app.get('/getting-started', function (req, res) {
dynoSrc.readMe(function(err, contentst) {
marked(contentst, function(err, md) {
res.render('getting-started.html', {
title: 'Getting Started',
readme : md,
cache: false
});
});
});
});
app.get('/git-clone', function(req, res) {
var repo = git.getRepo('ModelFlow', 'ryanstevens');
repo.clone().done(function(branch) {
res.send(branch);
});
});
app.get('/github/file', function(req, res) {
var query = url.parse(req.url, true).query;
git.getGitHubRaw(query.repo, query.file, query.hash, function(err, file) {
fs.createReadStream(file).pipe(res);
});
});
app.get('/github/diff', function(req, res) {
var query = url.parse(req.url, true).query;
console.log("Creating patch file from github ", query);
async.parallel([
function(next) {
git.getGitHubRaw(query.repo, query.file, query.from, next);
},
function(next) {
git.getGitHubRaw(query.repo, query.file, query.to, next);
}
], function(err, files) {
git.fileDiff(files[0], files[1]).pipe(res);
});
});
app.get('/bower/diff', function(req, res) {
var query = url.parse(req.url, true).query;
console.log("Creating patch file ", query);
async.parallel([
function(next) {
git.getGitHubRaw(query.repo, query.file, query.from, next);
},
function(next) {
git.getGitHubRaw(query.repo, query.file, query.to, next);
}
], function(err, files) {
git.fileDiff(files[0], files[1]).pipe(res);
});
});
app.listen(port);
console.log('Running server on port %s.', port);