This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbiton-hybrid-app.js
executable file
·103 lines (84 loc) · 3.09 KB
/
biton-hybrid-app.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
#!/usr/bin/env node
'use strict'
const debug = require('debug')('biton:hybrid-app')
const biton = require('../biton-hybrid')
const express = require('express')
const http = require('http')
const path = require('path')
/* Parse environment configuration variables */
// Participate in peer discovery over Mainline DHT (default false)
// Fallback bootstrapping mechanism when the user cannot connect through trusted nodes
const STRANGERS = process.env.STRANGERS === 'true' || false
// Join the global biton swarm (default false)
// Recommended unless nodes are behind bandwidth capped connections
const GLOBALNET = process.env.JOINGLOBAL === 'true' || false
// Optional seed for joining a local network (e.g. 'myApp')
const LOCALNET = process.env.LOCALNET
// Magic bytes for connecting to independent networks
const NETMAGIC = process.env.NETMAGIC
const PORT = process.env.PORT || 5000
const HOST = process.env.HOST || process.env.NODE_ENV === 'production' ? '0.0.0.0' : '127.0.0.1'
console.log('biton webtorrent-hybrid client')
// Setup express behind the http module
const app = express()
const server = http.createServer(app)
// Serve static files in the public directory
app.use(express.static(path.join(__dirname, '/public')))
// Attach HTTP endpoints
app.get('/', function (req, res) {
res.redirect('/entangled')
})
app.get('/entangled', function (req, res) {
res.sendFile(path.join(__dirname, 'views/entangled.html'))
})
// Handle 404 for unrecognized URLs
app.get('*', function (req, res) {
res.status(404).send('404 Not Found')
})
server.listen(PORT, HOST, function () {
console.log('HTTP server running at http://%s:%s', server.address().address, server.address().port)
})
server.on('error', function (e) {
if (e.code === 'EADDRINUSE') {
console.log('Another application (a biton client or container?) is already listening on port %s', PORT)
} else {
console.log('Unexpected error at the HTTP server: ' + e.code)
}
exitHandler()
})
// Graceful shutdown. Close active connections. Delete logs and uncompleted chunks
function exitHandler (options = {}) {
if (server && server.listening) {
server.close()
}
if (node && !node.destroyed) {
console.log('Destroying biton wires...')
node.destroy()
}
const exitCode = options.exitCode || 0
process.exit(exitCode)
}
// Attach exit handlers
// process.on('exit', exitHandler.bind())
process.on('SIGINT', exitHandler.bind())
process.on('SIGTERM', exitHandler.bind())
process.on('SIGUSR1', exitHandler.bind())
process.on('SIGUSR2', exitHandler.bind())
process.on('uncaughtException', function (err) {
debug('uncaught exception: ', err.stack)
exitHandler({ exitCode: 1 })
})
// Start a biton client
const node = new biton({ strangers: STRANGERS, netMagic: NETMAGIC})
// Wait for node to generate a new identity and to be ready to join
node.once('bitonReady', function () {
if (GLOBALNET) {
console.log('Connecting to the global biton network')
const globalNet= node.joinGlobalNet()
}
if (LOCALNET) {
console.log('Connecting to local network %s', LOCALNET)
const localNet = node.joinLocalNet(LOCALNET)
}
})
node.getNewIdentity()