Bump path-parse from 1.0.6 to 1.0.7
[KisSync.git] / src / main.js
blob325d2e4999e6050128e2d4d1f3b2a99e910f45e0
1 import Config from './config';
2 import * as Switches from './switches';
3 import { eventlog } from './logger';
4 require('source-map-support').install();
6 const LOGGER = require('@calzoneman/jsli')('main');
8 try {
9 Config.load('config.yaml');
10 } catch (e) {
11 LOGGER.fatal(
12 "Failed to load configuration: %s",
15 process.exit(1);
18 const sv = require('./server').init();
20 if (!Config.get('debug')) {
21 process.on('uncaughtException', error => {
22 LOGGER.fatal('Uncaught exception: %s', error.stack);
23 });
25 process.on('SIGINT', () => {
26 LOGGER.info('Caught SIGINT; shutting down');
27 sv.shutdown();
28 });
31 // TODO: this can probably just be part of servsock.js
32 // servsock should also be refactored to send replies instead of
33 // relying solely on tailing logs
34 function handleLine(line) {
35 if (line === '/reload') {
36 LOGGER.info('Reloading config');
37 try {
38 Config.load('config.yaml');
39 } catch (e) {
40 LOGGER.error(
41 "Failed to load configuration: %s",
45 require('./web/pug').clearCache();
46 } else if (line.indexOf('/switch') === 0) {
47 const args = line.split(' ');
48 args.shift();
49 if (args.length === 1) {
50 LOGGER.info('Switch ' + args[0] + ' is ' +
51 (Switches.isActive(args[0]) ? 'ON' : 'OFF'));
52 } else if (args.length === 2) {
53 Switches.setActive(args[0], args[1].toLowerCase() === 'on' ? true : false);
54 LOGGER.info('Switch ' + args[0] + ' is now ' +
55 (Switches.isActive(args[0]) ? 'ON' : 'OFF'));
57 } else if (line.indexOf('/reload-partitions') === 0) {
58 sv.reloadPartitionMap();
59 } else if (line.indexOf('/save') === 0) {
60 sv.forceSave();
61 } else if (line.indexOf('/unloadchan') === 0) {
62 const args = line.split(/\s+/); args.shift();
63 if (args.length) {
64 const name = args.shift();
65 const chan = sv.getChannel(name);
66 const users = Array.prototype.slice.call(chan.users);
67 chan.emit('empty');
68 users.forEach(function (u) {
69 u.kick('Channel shutting down');
70 });
71 eventlog.log('[acp] ' + 'SYSTEM' + ' forced unload of ' + name);
73 } else if (line.indexOf('/reloadcert') === 0) {
74 sv.reloadCertificateData();
78 // Go Go Gadget Service Socket
79 if (Config.get('service-socket.enabled')) {
80 LOGGER.info('Opening service socket');
81 const ServiceSocket = require('./servsock');
82 const sock = new ServiceSocket();
83 sock.init(
84 line => {
85 try {
86 handleLine(line);
87 } catch (error) {
88 LOGGER.error(
89 'Error in UNIX socket command handler: %s',
90 error.stack
94 Config.get('service-socket.socket')
98 let stdinbuf = '';
99 process.stdin.on('data', function (data) {
100 stdinbuf += data;
101 if (stdinbuf.indexOf('\n') !== -1) {
102 let line = stdinbuf.substring(0, stdinbuf.indexOf('\n'));
103 stdinbuf = stdinbuf.substring(stdinbuf.indexOf('\n') + 1);
104 try {
105 handleLine(line);
106 } catch (error) {
107 LOGGER.error('Command line input handler failed: %s', error.stack);
112 // Hi I'm Mr POSIX! Look at me!
113 process.on('SIGUSR2', () => {
114 sv.reloadCertificateData();
117 require('bluebird');
118 process.on('unhandledRejection', function (reason, _promise) {
119 LOGGER.error('Unhandled rejection: %s', reason.stack);