forked from garbados/dat-gateway
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbin.js
executable file
·69 lines (66 loc) · 1.82 KB
/
bin.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
#!/usr/bin/env node
'use strict'
const DatGateway = require('.')
const os = require('os')
const mkdirp = require('mkdirp')
const pkg = require('./package.json')
require('yargs')
.version(pkg.version)
.command({
command: '$0',
aliases: ['start'],
builder: function (yargs) {
yargs.options({
port: {
alias: 'p',
description: 'Port for the gateway to listen on.',
default: 3000
},
dir: {
alias: 'd',
description: 'Directory to use as a cache.',
coerce: function (value) {
return value.replace('~', os.homedir())
},
default: '~/.dat-gateway',
normalize: true
},
max: {
alias: 'm',
description: 'Maximum number of archives allowed in the cache.',
default: 20
},
period: {
description: 'Number of milliseconds between cleaning the cache of expired archives.',
default: 60 * 1000 // every minute
},
ttl: {
alias: 't',
description: 'Number of milliseconds before archives expire.',
default: 10 * 60 * 1000 // ten minutes
},
redirect: {
alias: 'r',
description: 'Whether to use subdomain redirects',
default: false
}
})
},
handler: function (argv) {
const { dir, max, period, port, ttl, redirect } = argv
mkdirp.sync(dir) // make sure it exists
const gateway = new DatGateway({ dir, max, period, ttl, redirect })
gateway
.load()
.then(() => {
return gateway.listen(port)
})
.then(function () {
console.log('[dat-gateway] Now listening on port ' + port)
})
.catch(console.error)
}
})
.alias('h', 'help')
.config()
.parse()