UNPKG

1.4 kBJavaScriptView Raw
1'use strict';
2
3var INHERIT = require('inherit'),
4 QFS = require('q-io/fs'),
5 QHTTP = require('q-io/http'),
6 PATH = require('./path'),
7 LOGGER = require('./logger'),
8 U = require('./util'),
9
10 Server = require('./base-server').Server;
11
12exports.Server = INHERIT(Server, {
13
14 startServer: function(requestHandler) {
15 var _this = this,
16 started = this.opts.socketOnly? []: this.__base(requestHandler);
17
18 if (!(_this.opts.socket || _this.opts.socketOnly)) return started;
19
20 // Start server on unix socket
21 var sockServer = QHTTP.Server(requestHandler);
22
23 sockServer.node.on('error', this.errorHandler.bind(this, this.opts.socketPath));
24
25 U.mkdirs(PATH.dirname(_this.opts.socketPath));
26
27 started.push(sockServer.listen(_this.opts.socketPath).then(function(listener) {
28
29 // Remove unix socket on server stop
30 sockServer.stopped.fin(function() {
31 return QFS.remove(_this.opts.socketPath);
32 });
33
34 // Change permissions of unix socket on server start
35 return QFS.chmod(_this.opts.socketPath, _this.opts.socketMode)
36 .then(function() {
37 LOGGER.finfo('Server is listening on unix socket %s', _this.opts.socketPath);
38
39 return listener;
40 });
41
42 }));
43
44 return started;
45 }
46});
47