|
| 1 | +#! /usr/bin/env node |
| 2 | +'use strict' |
| 3 | + |
| 4 | +const UpRingKV = require('.') |
| 5 | +const fs = require('fs') |
| 6 | +const path = require('path') |
| 7 | +const http = require('http') |
| 8 | +const querystring = require('querystring') |
| 9 | +const Transform = require('readable-stream').Transform |
| 10 | +const pino = require('pino-http') |
| 11 | +const pump = require('pump') |
| 12 | +const args = require('minimist')(process.argv.slice(2), { |
| 13 | + boolean: ['help', 'verbose'], |
| 14 | + default: { |
| 15 | + port: 0, |
| 16 | + points: 100, |
| 17 | + timeout: 200, |
| 18 | + verbose: false |
| 19 | + }, |
| 20 | + alias: { |
| 21 | + port: 'p', |
| 22 | + points: 'P', |
| 23 | + help: 'h', |
| 24 | + timeout: 't', |
| 25 | + verbose: 'V' |
| 26 | + } |
| 27 | +}) |
| 28 | + |
| 29 | +if (args.help) { |
| 30 | + console.log(fs.readFileSync(path.join(__dirname, 'help.txt'))) |
| 31 | + process.exit(1) |
| 32 | +} |
| 33 | + |
| 34 | +if (args.version) { |
| 35 | + console.log('upring-kv', 'v' + require('./package').version) |
| 36 | + process.exit(1) |
| 37 | +} |
| 38 | + |
| 39 | +const db = UpRingKV({ |
| 40 | + base: args._, |
| 41 | + logLevel: args.verbose ? 'debug' : 'info', |
| 42 | + hashring: { |
| 43 | + replicaPoints: args.points, |
| 44 | + joinTimeout: args.timeout |
| 45 | + } |
| 46 | +}) |
| 47 | + |
| 48 | +db.upring.on('up', function () { |
| 49 | + console.log('to start a new peer, copy and paste the following in a new terminal:') |
| 50 | + console.log('node example', this.whoami()) |
| 51 | + |
| 52 | + const logger = pino(db.upring.logger) |
| 53 | + |
| 54 | + const server = http.createServer(function (req, res) { |
| 55 | + logger(req, res) |
| 56 | + switch (req.method) { |
| 57 | + case 'PUT': |
| 58 | + case 'POST': |
| 59 | + handlePost(req, res) |
| 60 | + break |
| 61 | + case 'GET': |
| 62 | + handleGet(req, res) |
| 63 | + break |
| 64 | + default: |
| 65 | + res.statusCode = 404 |
| 66 | + res.end() |
| 67 | + } |
| 68 | + }) |
| 69 | + |
| 70 | + server.listen(args.port, function (err) { |
| 71 | + if (err) { |
| 72 | + throw err |
| 73 | + } |
| 74 | + |
| 75 | + console.log('server listening on', server.address()) |
| 76 | + }) |
| 77 | + |
| 78 | + function handleGet (req, res) { |
| 79 | + const split = req.url.split('?') |
| 80 | + const key = split[0] |
| 81 | + const query = querystring.parse(split[1]) |
| 82 | + if (query.live) { |
| 83 | + res.writeHead(200, { |
| 84 | + 'Content-Type': 'text/event-stream', |
| 85 | + 'Cache-Control': 'no-cache', |
| 86 | + 'Connection': 'keep-alive' |
| 87 | + }) |
| 88 | + var transform = new Transform({ |
| 89 | + objectMode: true, |
| 90 | + transform (chunk, enc, cb) { |
| 91 | + this.push('data:' + JSON.stringify(chunk.value) + '\n\n') |
| 92 | + cb() |
| 93 | + } |
| 94 | + }) |
| 95 | + pump(db.liveUpdates(key), transform, res) |
| 96 | + return |
| 97 | + } else { |
| 98 | + db.get(key, function (err, data) { |
| 99 | + if (err) { |
| 100 | + res.statusCode = 500 |
| 101 | + res.end(err.message) |
| 102 | + return |
| 103 | + } |
| 104 | + |
| 105 | + if (!data) { |
| 106 | + res.statusCode = 404 |
| 107 | + res.end() |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + res.setHeader('Content-Type', data.contentType) |
| 112 | + res.end(data.value) |
| 113 | + }) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + function handlePost (req, res) { |
| 118 | + var str = '' |
| 119 | + |
| 120 | + req.on('data', function (chunk) { |
| 121 | + str += chunk.toString() |
| 122 | + }) |
| 123 | + |
| 124 | + req.on('error', function (err) { |
| 125 | + res.statusCode = 500 |
| 126 | + res.end(err.message) |
| 127 | + }) |
| 128 | + |
| 129 | + req.on('end', function () { |
| 130 | + db.put(req.url, { |
| 131 | + contentType: req.headers['content-type'], |
| 132 | + value: str |
| 133 | + }, function (err) { |
| 134 | + if (err) { |
| 135 | + res.statusCode = 500 |
| 136 | + res.end(err.message) |
| 137 | + } else { |
| 138 | + res.statusCode = 200 |
| 139 | + res.end() |
| 140 | + } |
| 141 | + }) |
| 142 | + }) |
| 143 | + } |
| 144 | +}) |
0 commit comments