2013-05-22 01:48:04 -05:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2013-06-13 09:21:14 -05:00
|
|
|
var pkg_file = '../package.yaml';
|
2013-05-31 01:26:11 -05:00
|
|
|
var fs = require('fs');
|
|
|
|
var yaml = require('js-yaml');
|
2013-05-22 01:48:04 -05:00
|
|
|
var commander = require('commander');
|
2013-05-31 01:26:11 -05:00
|
|
|
var server = require('../lib/index');
|
2013-06-07 20:16:28 -05:00
|
|
|
var crypto = require('crypto');
|
2013-06-13 09:21:14 -05:00
|
|
|
var pkg = require(pkg_file);
|
2013-05-22 01:48:04 -05:00
|
|
|
|
|
|
|
commander
|
2013-09-23 23:40:46 -05:00
|
|
|
.option('-l, --listen <[host:]port>', 'host:port number to listen on (default: localhost:4873)')
|
2013-06-13 09:21:14 -05:00
|
|
|
.option('-c, --config <config.yaml>', 'use this configuration file (default: ./config.yaml)')
|
2013-05-31 01:26:11 -05:00
|
|
|
.version(pkg.version)
|
2013-05-22 01:48:04 -05:00
|
|
|
.parse(process.argv);
|
|
|
|
|
2013-09-23 23:15:13 -05:00
|
|
|
var config, config_path;
|
2013-06-07 20:16:28 -05:00
|
|
|
if (commander.config) {
|
2013-09-23 23:15:13 -05:00
|
|
|
config_path = commander.config;
|
|
|
|
config = yaml.safeLoad(fs.readFileSync(config_path, 'utf8'));
|
2013-06-07 20:16:28 -05:00
|
|
|
} else {
|
2013-09-23 23:15:13 -05:00
|
|
|
config_path = './config.yaml';
|
2013-06-13 09:21:14 -05:00
|
|
|
try {
|
2013-09-23 23:15:13 -05:00
|
|
|
config = yaml.safeLoad(fs.readFileSync(config_path, 'utf8'));
|
2013-06-13 09:21:14 -05:00
|
|
|
} catch(err) {
|
|
|
|
var created_config = require('../lib/config_gen')();
|
|
|
|
config = yaml.safeLoad(created_config.yaml);
|
|
|
|
console.log('starting with default config, use user: "%s", pass: "%s" to authenticate', created_config.user, created_config.pass);
|
2013-09-23 23:15:13 -05:00
|
|
|
fs.writeFileSync(config_path, created_config.yaml);
|
2013-06-07 20:16:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!config.user_agent) config.user_agent = 'Sinopia/'+pkg.version;
|
2013-09-23 23:15:13 -05:00
|
|
|
if (!config.self_path) config.self_path = config_path;
|
2013-06-07 20:16:28 -05:00
|
|
|
|
2013-09-25 03:10:40 -05:00
|
|
|
// command line || config file || default
|
|
|
|
var hostport = commander.listen || String(config.listen || '') || '4873';
|
|
|
|
|
2013-09-23 23:40:46 -05:00
|
|
|
hostport = hostport.split(':');
|
2013-05-31 01:26:11 -05:00
|
|
|
if (hostport.length < 2) {
|
|
|
|
hostport = [undefined, hostport[0]];
|
2013-05-22 01:48:04 -05:00
|
|
|
}
|
2013-06-20 13:40:04 -05:00
|
|
|
if (hostport[0] == null) {
|
|
|
|
hostport[0] = 'localhost';
|
|
|
|
}
|
2013-06-07 20:16:28 -05:00
|
|
|
server(config).listen(hostport[1], hostport[0]);
|
2013-06-20 13:40:04 -05:00
|
|
|
console.log('Server is listening on http://%s:%s/', hostport[0], hostport[1]);
|
2013-05-22 01:48:04 -05:00
|
|
|
|