-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
87 lines (78 loc) · 2.42 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
const open = require('open');
const yargs = require('yargs/yargs');
const {hideBin} = require('yargs/helpers');
require('dotenv').config();
global.__config = require(__dirname + '/config');
const config = global.__config;
const settings = require(config.root + '/src/all-settings');
const startServer = require(config.root + '/src/utility/startServer.js');
// const argv = parseArgs(process.argv.slice(2));
const argv = yargs(hideBin(process.argv))
.scriptName('media-server')
.usage('$0 [args]', 'A localhost Media Server', (yargs) => {
yargs.option('verbose', {
alias: 'v',
type: 'boolean',
description: 'Run with verbose logging',
count: true,
});
yargs.option('location', {
alias: 'l',
type: 'string',
description: 'the path to media content directory',
});
yargs.option('port', {
alias: 'p',
type: 'number',
description: 'the port to run server on',
});
yargs.option('gui', {
alias: 'g',
type: 'boolean',
description: 'open gui mode for configuring settings',
});
yargs.option('image', {
type: 'boolean',
description: 'enable display of images',
default: false,
});
yargs.option('video', {
type: 'boolean',
description: 'enable display of videos',
default: true,
});
})
.alias('h', 'help')
.version(false).argv;
Object.assign(settings, argv);
console.log(process.argv.slice(2).length && settings.location == undefined);
if (process.argv.slice(2).length === 0 && settings.location == undefined) {
argv.g = true;
}
const PORT = argv.port || process.env.PORT || 3000;
settings.port = PORT;
console.log(settings);
if (argv.g) {
const admin = require(config.root + '/src/admin/index.js');
const adminServer = admin.listen(parseInt(PORT) + 1, 'localhost', () => {
console.log('admin server is up');
const url =
'http://' +
adminServer.address().address +
':' +
adminServer.address().port;
console.log('listening at ' + url);
open(url);
});
adminServer.on('error', (err) => {
if (err.errno === -98) {
const url = 'http://' + err.address + ':' + err.port;
console.log('Server already running at '+ url);
open(url);
}
});
admin.on('close', () => {});
} else {
const app = require(config.root + '/src/user/index.js');
startServer(app, PORT);
}