|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +require('make-promises-safe'); |
| 4 | +const explain = require('explain-error'); |
| 5 | +const envSchema = require('env-schema'); |
| 6 | +const ytSource = require('u-wave-source-youtube'); |
| 7 | +const scSource = require('u-wave-source-soundcloud'); |
| 8 | +const uwave = require('..'); |
| 9 | +const pkg = require('../package.json'); |
| 10 | +const argv = require('minimist')(process.argv.slice(2)); |
| 11 | + |
| 12 | +if (argv.h || argv.help) { |
| 13 | + console.log('u-wave-core'); |
| 14 | + console.log('Version', pkg.version); |
| 15 | + console.log(); |
| 16 | + console.log('Environment Variables:'); |
| 17 | + console.log(' SECRET'); |
| 18 | + console.log(' A secret key used for encrypting passwords. Must be a 64-character hexadecimal string (= 256 bits).'); |
| 19 | + console.log(' PORT'); |
| 20 | + console.log(' Port to listen on. Defaults to 6042.'); |
| 21 | + console.log(' REDIS_URL'); |
| 22 | + console.log(' URL of the Redis instance to connect to. Defaults to redis://localhost:6379/.'); |
| 23 | + console.log(' MONGODB_URL'); |
| 24 | + console.log(' URL of the MongoDB database to use. Defaults to mongodb://localhost:27017/uwave.'); |
| 25 | + console.log(); |
| 26 | + process.exit(0); |
| 27 | +} |
| 28 | + |
| 29 | +const config = envSchema({ |
| 30 | + schema: { |
| 31 | + type: 'object', |
| 32 | + required: ['SECRET'], |
| 33 | + properties: { |
| 34 | + PORT: { |
| 35 | + type: 'number', |
| 36 | + default: 6042, |
| 37 | + }, |
| 38 | + REDIS_URL: { |
| 39 | + type: 'string', |
| 40 | + format: 'uri', |
| 41 | + default: 'redis://localhost:6379', |
| 42 | + }, |
| 43 | + MONGODB_URL: { |
| 44 | + type: 'string', |
| 45 | + format: 'uri', |
| 46 | + default: 'mongodb://localhost:27017/uwave', |
| 47 | + }, |
| 48 | + SECRET: { |
| 49 | + type: 'string', |
| 50 | + regex: '^[0-9a-fA-F]+$', |
| 51 | + min: 64, |
| 52 | + max: 64, |
| 53 | + }, |
| 54 | + YOUTUBE_API_KEY: { |
| 55 | + type: 'string', |
| 56 | + }, |
| 57 | + SOUNDCLOUD_API_KEY: { |
| 58 | + type: 'string', |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | +}); |
| 63 | + |
| 64 | +const port = Number(argv.port || config.PORT); |
| 65 | + |
| 66 | +const secret = Buffer.from(config.SECRET, 'hex'); |
| 67 | + |
| 68 | +const uw = uwave({ |
| 69 | + port, |
| 70 | + redis: config.REDIS_URL, |
| 71 | + mongo: config.MONGODB_URL, |
| 72 | + secret, |
| 73 | +}); |
| 74 | + |
| 75 | +uw.express.set('json spaces', 2); |
| 76 | + |
| 77 | +uw.on('mongoError', (err) => { |
| 78 | + throw explain(err, 'Could not connect to MongoDB. Is it installed and running?'); |
| 79 | +}); |
| 80 | + |
| 81 | +uw.on('redisError', (err) => { |
| 82 | + throw explain(err, 'Could not connect to the Redis server. Is it installed and running?'); |
| 83 | +}); |
| 84 | + |
| 85 | +if (config.YOUTUBE_API_KEY) { |
| 86 | + uw.source(ytSource, { |
| 87 | + key: config.YOUTUBE_API_KEY, |
| 88 | + }); |
| 89 | +} |
| 90 | +if (config.SOUNDCLOUD_API_KEY) { |
| 91 | + uw.source(scSource, { |
| 92 | + key: config.SOUNDCLOUD_API_KEY, |
| 93 | + }); |
| 94 | +} |
| 95 | + |
| 96 | +uw.listen(port).then(() => { |
| 97 | + console.log(`Now listening on ${port}`); |
| 98 | +}, (error) => { |
| 99 | + console.error(error.stack); |
| 100 | + process.exit(1); |
| 101 | +}); |
0 commit comments