UNPKG

3.82 kBJavaScriptView Raw
1/*
2ExpressJS for volebo.net
3
4Copyright (C) 2016-2017 Volebo <dev@volebo.net>
5Copyright (C) 2016-2017 Koryukov Maksim <maxkoryukov@gmail.com>
6
7This program is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21"use strict";
22
23require('dotenv').config({ silent: true });
24
25const debug = require('debug')('volebo:express');
26const fs = require('fs');
27const http = require('http');
28const express = require('express');
29
30const Config = require('./src/config');
31const createListener = require('./src/server');
32
33// TODO : #2 use LOGGER!!!
34// BUG: #2
35const log = console;
36
37let deprecated_error_die = function(done, msg) {
38 var e = new Error(msg);
39 done(e);
40}
41
42/*
43====================================
44EXPORT
45====================================
46*/
47
48let vbexp = function(options) {
49
50 let app = createListener(options);
51
52 app.start = function app_start(done)
53 {
54 if (!done) {
55 done = function(err) {
56 if (err) {
57 throw err;
58 }
59 }
60 }
61 // append error handlers:
62 app._onStarting();
63
64 // Get port from environment and store in Express.
65 let host = app.config.server.host;
66 let port = app.config.server.port;
67 let localpath = app.config.server.path;
68
69 let useLocalPath = !! localpath;
70
71 if (useLocalPath) {
72 debug('will listen on localpath');
73 host = null;
74 port = null;
75
76 try {
77 let stat = fs.statSync(localpath);
78 if (stat.isSocket()) {
79 debug('remove existing socket file', localpath);
80
81 fs.unlinkSync(localpath);
82 } else {
83 deprecated_error_die(done, 'Can not start server, listening on NON-SOCKET file');
84 }
85 } catch (e) {
86 if (e.code === 'ENOENT' && e.syscall==='stat') {
87 // do nothing, localpath-file does not exist
88 } else {
89 throw e;
90 }
91 }
92 } else {
93 debug('will listen on host:port');
94 localpath = null;
95 }
96
97 // Create HTTP server.
98 let server = http.createServer(app);
99
100 /**
101 * Event listener for HTTP server "listening" event.
102 */
103 let onListening = function onListening() {
104 var addr = server.address();
105 var bind = typeof addr === 'string'
106 ? `pipe ${addr}`
107 : `address ${addr.address} port ${addr.port}`;
108 log.info('Listening on ' + bind);
109
110 done();
111 }
112
113 let onError = function onError(error) {
114
115 // TODO : #2 handle errors and write to the error log!!
116
117 if (error.syscall !== 'listen') {
118 throw error;
119 }
120
121 var bind = useLocalPath
122 ? 'Pipe ' + localpath
123 : 'Port ' + port;
124
125 // handle specific listen errors with friendly messages
126 switch (error.code) {
127 case 'EACCES':
128 deprecated_error_die(done, bind + ' requires elevated privileges');
129 break;
130 case 'EADDRINUSE':
131 deprecated_error_die(done, bind + ' is already in use');
132 break;
133 default:
134 return done(error);
135 }
136 }
137
138 server.on('error', onError);
139 server.on('listening', onListening);
140
141 // Listen on provided port, on all network interfaces.
142 if(useLocalPath) {
143 server.listen(localpath)
144 } else {
145 server.listen(port, host)
146 }
147
148 app.close = function() {
149 return server.close.apply(server, arguments);
150 };
151
152 return;
153 }
154
155 return app;
156}
157
158vbexp.Router = function vbexp_Router() {
159 return express.Router.apply(express, arguments);
160}
161
162vbexp.Config = Config;
163
164exports = module.exports = vbexp;