1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | "use strict";
|
22 |
|
23 | require('dotenv').config({ silent: true });
|
24 |
|
25 | const debug = require('debug')('volebo:express');
|
26 | const fs = require('fs');
|
27 | const http = require('http');
|
28 | const express = require('express');
|
29 |
|
30 | const Config = require('./src/config');
|
31 | const createListener = require('./src/server');
|
32 |
|
33 |
|
34 |
|
35 | const log = console;
|
36 |
|
37 | let deprecated_error_die = function(done, msg) {
|
38 | var e = new Error(msg);
|
39 | done(e);
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | let 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 |
|
62 | app._onStarting();
|
63 |
|
64 |
|
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 |
|
88 | } else {
|
89 | throw e;
|
90 | }
|
91 | }
|
92 | } else {
|
93 | debug('will listen on host:port');
|
94 | localpath = null;
|
95 | }
|
96 |
|
97 |
|
98 | let server = http.createServer(app);
|
99 |
|
100 | |
101 |
|
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 |
|
116 |
|
117 | if (error.syscall !== 'listen') {
|
118 | throw error;
|
119 | }
|
120 |
|
121 | var bind = useLocalPath
|
122 | ? 'Pipe ' + localpath
|
123 | : 'Port ' + port;
|
124 |
|
125 |
|
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 |
|
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 |
|
158 | vbexp.Router = function vbexp_Router() {
|
159 | return express.Router.apply(express, arguments);
|
160 | }
|
161 |
|
162 | vbexp.Config = Config;
|
163 |
|
164 | exports = module.exports = vbexp;
|