forked from Blockstream/esplora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev-server.js
75 lines (56 loc) · 2.2 KB
/
dev-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
import fs, { promises as fsp } from 'fs'
import pug from 'pug'
import pathu from 'path'
import glob from 'glob'
import express from 'express'
import browserify from 'browserify-middleware'
import cssjanus from 'cssjanus'
const rpath = p => pathu.join(__dirname, p)
const app = express()
app.engine('pug', pug.__express)
app.use(require('morgan')('dev'))
if (process.env.CORS_ALLOW) {
app.use((req, res, next) => {
res.set('Access-Control-Allow-Origin', process.env.CORS_ALLOW)
next()
})
}
if (process.env.PRERENDER_URL) {
app.use((req, res, next) => {
if (req.query.nojs != null) return res.redirect(303, process.env.PRERENDER_URL+req.path)
next()
})
}
const custom_assets = (process.env.CUSTOM_ASSETS||'').split(/ +/).filter(Boolean)
, custom_css = (process.env.CUSTOM_CSS ||'').split(/ +/).filter(Boolean)
const p = fn => (req, res, next) => fn(req, res).catch(next)
app.get('/', (req, res) => res.render(rpath('client/index.pug')))
app.get('/app.js', browserify(rpath('client/src/run-browser.js')))
// Merges the main stylesheet from www/style.css with the custom css files
app.get('/style.css', p(async (req, res) =>
res.type('css').send(await prepCss())))
const prepCss = async _ =>
(await Promise.all([ rpath('www/style.css'), ...custom_css ].map(path => fsp.readFile(path))))
.join('\n')
// Automatically adjust CSS for RTL using cssjanus
app.get('/style-rtl.css', p(async (req, res) =>
res.type('css').send(cssjanus.transform(await prepCss()))))
// Add handlers for custom asset overrides
custom_assets.forEach(pattern => {
console.log(process.env.CUSTOM_ASSETS, pattern)
// pattern could also be a simple path
const paths = glob.sync(pattern)
paths.forEach(path => {
const name = pathu.basename(path)
, stat = fs.statSync(path)
stat.isDirectory()
? app.use('/'+name, express.static(path))
: app.get('/'+name, (req, res) => res.sendFile(path))
})
})
// And finally the default fallback assets from www/
app.use('/', express.static(rpath('www')))
app.use((req, res) => res.render(rpath('client/index.pug')))
app.listen(process.env.PORT || 5000, function(){
console.log(`HTTP server running on ${this.address().address}:${this.address().port}`)
})