-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
112 lines (94 loc) · 3.02 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
var assert = require('assert')
var http = require('http')
var Archiver = require('hypercore-archiver')
var DatServer = require('archiver-server')
var appa = require('appa')
var Api = require('.')
var DAT_KEY_REGEX = /\/([0-9a-f]{64})/i
module.exports = function (dir, opts, cb) {
assert.equal(typeof dir, 'string', 'directory required')
if (typeof opts === 'function') {
cb = opts
opts = {}
}
opts = opts || {}
var app = appa({log: {level: 'silent'}})
var archives = Archiver(dir)
var datServer = DatServer(archives, {swarm: true, http: true})
var api = Api(archives)
var apiServer = http.createServer(app)
var archiveServer = http.createServer(datServer.httpRequest)
app.on('/add', function (req, res, ctx) {
if (req.method !== 'POST') {
return app.error(405, 'Method not allowed').pipe(res)
}
api.add(ctx.body, function (err, code, data) {
if (err) return app.error(code, err.message).pipe(res)
app.send(code, data).pipe(res)
})
})
app.on('/remove', function (req, res, ctx) {
if (req.method !== 'POST') {
return app.error(405, 'Method not allowed').pipe(res)
}
api.remove(ctx.body, function (err, code, data) {
if (err) return app.error(code, err.message).pipe(res)
app.send(code, data).pipe(res)
})
})
app.on('/progress/:key', function (req, res, ctx) {
if (req.method !== 'GET') {
return app.error(405, 'Method not allowed').pipe(res)
}
var keyMatch = DAT_KEY_REGEX.exec(req.url)
if (!keyMatch) {
return app.error(404, 'Not found').pipe(res)
}
var key = keyMatch[1]
api.archiveProgress(key, function (err, code, data) {
if (err) return app.error(code, err.message).pipe(res)
app.send(code, data).pipe(res)
})
})
app.on('/status', function (req, res, ctx) {
if (req.method !== 'GET') {
return app.error(405, 'Method not allowed').pipe(res)
}
api.status(function (err, code, data) {
if (err) return app.error(code, err.message).pipe(res)
app.send(code, data).pipe(res)
})
})
app.on('/status/:key', function (req, res, ctx) {
if (req.method !== 'GET') {
return app.error(405, 'Method not allowed').pipe(res)
}
var keyMatch = DAT_KEY_REGEX.exec(req.url)
if (!keyMatch) {
return app.error(404, 'Not found').pipe(res)
}
var key = keyMatch[1]
api.archiveProgress(key, function (err, code, data) {
if (err) return app.error(code, err.message).pipe(res)
app.send(code, data).pipe(res)
})
})
apiServer.listen(3000, function () {
console.log('api server started at http://127.0.0.1:3000')
cb(null)
})
archiveServer.listen(8000, function () {
console.log('archive server started at http://127.0.0.1:8000')
})
datServer.swarm.on('listening', function () {
console.log('Listening for connections on the Dat Network')
})
function close (cb) {
apiServer.close(function () {
archiveServer.close(function () {
datServer.swarm.close(cb)
})
})
}
return close
}