-
Notifications
You must be signed in to change notification settings - Fork 7
/
server.coffee
43 lines (36 loc) · 1.3 KB
/
server.coffee
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
fs = require('fs')
stat = require('node-static')
url = require('url')
httpProxy = require('http-proxy')
walletPath = process.env.WALLET_PATH
dogeblockdUrl = process.env.DOGEBLOCKD_URL
port = 8080
if fs.existsSync(walletPath)
console.log "Serving wallet from '#{walletPath}'."
console.log "Connecting to Dogeblockd at '#{dogeblockdUrl}'."
fileServer = new stat.Server(walletPath)
proxy = httpProxy.createProxyServer()
proxy.on 'error', (error, req, res) ->
console.log 'proxy error', error
if !res.headersSent
res.writeHead 500, 'content-type': 'application/json'
json = error: 'proxy_error', reason: error.message
res.end JSON.stringify(json)
require('http').createServer((req, res) ->
console.log "'#{req.url}'"
if req.url == '/servers.json'
res.end JSON.stringify(servers: [])
else if req.url.match(/^\/_api/)
req.url = req.url.replace(/^\/_api/, '/api')
req.url += '/' if req.url == '/api'
console.log "proxying to '#{dogeblockdUrl}#{req.url}'"
proxy.web req, res, target: dogeblockdUrl
else
req.addListener('end', ->
fileServer.serve(req, res)
).resume()
).listen(port)
console.log "Listening at http://0.0.0.0:#{port}"
else
console.log "Wallet path not found '#{walletPath}', set via WALLET_PATH."
process.exit 1