fluffy – Blame information for rev 6
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | #!/usr/bin/env node |
2 | |||
3 | /*************************************************************************/ |
||
4 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
||
5 | /*************************************************************************/ |
||
6 | |||
7 | // Import packages. |
||
8 | const https = require('https'); |
||
9 | const http = require('http'); |
||
10 | const path = require('path'); |
||
11 | const fs = require('fs'); |
||
12 | const url = require('url'); |
||
13 | const moment = require('moment'); |
||
14 | const winston = require('winston'); |
||
15 | const yargs = require('yargs'); |
||
16 | const dns = require('dns'); |
||
3 | office | 17 | const was = require("was.js"); |
1 | office | 18 | |
19 | // Local imports. |
||
3 | office | 20 | const GET = require( |
1 | office | 21 | path |
22 | .resolve( |
||
23 | path.dirname(require.main.filename), |
||
5 | office | 24 | 'src/methods', |
3 | office | 25 | 'get' |
1 | office | 26 | ) |
27 | ); |
||
6 | office | 28 | const POST = require( |
29 | path |
||
30 | .resolve( |
||
31 | path.dirname(require.main.filename), |
||
32 | 'src/methods', |
||
33 | 'post' |
||
34 | ) |
||
35 | ); |
||
1 | office | 36 | const certs = require( |
37 | path |
||
38 | .resolve( |
||
39 | path.dirname(require.main.filename), |
||
5 | office | 40 | 'src/server', |
1 | office | 41 | 'certs' |
42 | ) |
||
43 | ); |
||
44 | |||
45 | // Load configuration file. |
||
46 | const config = require( |
||
47 | path |
||
48 | .resolve( |
||
49 | path.dirname(require.main.filename), |
||
50 | 'config' |
||
51 | ) |
||
52 | ); |
||
53 | |||
54 | // Get command-line arguments. |
||
55 | const argv = yargs |
||
56 | .version() |
||
57 | .option('root', { |
||
58 | alias: 'd', |
||
59 | describe: 'Path to the document root', |
||
60 | demandOption: true |
||
61 | }) |
||
62 | .help() |
||
63 | .argv |
||
64 | |||
65 | // Create various logging mechanisms. |
||
66 | // RFC5424 - { emerg: 0, alert: 1, crit: 2, error: 3, warning: 4, notice: 5, info: 6, debug: 7 } |
||
67 | winston.setLevels(winston.config.syslog.levels); |
||
68 | const log = new winston.Logger({ |
||
69 | transports: [ |
||
70 | new winston.transports.File({ |
||
71 | level: 'info', |
||
72 | filename: path.resolve( |
||
73 | path.dirname(require.main.filename), |
||
74 | config.log.file |
||
75 | ), |
||
76 | handleExceptions: true, |
||
77 | json: false, |
||
78 | maxsize: 1048576, // 1MiB. |
||
79 | maxFiles: 10, // Ten rotations. |
||
80 | colorize: false, |
||
81 | timestamp: () => moment() |
||
82 | .format('YYYYMMDDTHHmmss') |
||
83 | }), |
||
84 | new winston.transports.Console({ |
||
85 | level: 'info', |
||
86 | handleExceptions: true, |
||
87 | json: false, |
||
88 | colorize: true, |
||
89 | timestamp: () => moment() |
||
90 | .format('YYYYMMDDTHHmmss') |
||
91 | }) |
||
92 | ], |
||
93 | exitOnError: false |
||
94 | }); |
||
95 | |||
96 | fs.realpath(argv.root, (error, root) => { |
||
97 | if (error) { |
||
98 | log.error('Could not find document root: ' + argv.root); |
||
99 | process.exit(1); |
||
100 | } |
||
101 | |||
102 | // Start HTTP server. |
||
103 | http.createServer( |
||
104 | // authentication, |
||
105 | (request, response) => { |
||
3 | office | 106 | // Grab connecting address. |
107 | const address = request.socket.address(); |
||
108 | |||
1 | office | 109 | // Configuration path requested, so send the server configuration if allowed. |
110 | if(config.configuration.enable === true && |
||
111 | url.parse(request.url, true).path === |
||
112 | config.configuration.path) { |
||
113 | log.info('HTTP Server configuration requested by: ' + |
||
114 | address.address + ':' + |
||
115 | address.port |
||
116 | ); |
||
117 | response.setHeader('Content-Type', 'application/json'); |
||
118 | response.end(JSON.stringify(config)); |
||
119 | return; |
||
120 | } |
||
121 | |||
3 | office | 122 | // Switch on HTTP method. |
123 | was.lambda.switch( |
||
124 | request.method, |
||
125 | (o) => { |
||
126 | log.info('Unsupported HTTP \'' + request.method + '\' method requested by: ' + |
||
127 | address.address + ':' + |
||
128 | address.port |
||
129 | ); |
||
130 | }, |
||
131 | (o) => o === 'GET', |
||
132 | (o) => { |
||
133 | // Send the resource. |
||
134 | new GET().process(config, request, response, root) |
||
135 | .on('log', (data) => { |
||
136 | log.log(data.severity, data.message); |
||
137 | }) |
||
138 | .on('data', (result) => { |
||
139 | response.writeHead(result.status); |
||
140 | result.data |
||
141 | .on('readable', () => result.data.pipe(response)) |
||
142 | .on('end', () => response.end()); |
||
143 | }); |
||
144 | return true; |
||
145 | }, |
||
6 | office | 146 | (o) => o === 'POST', |
3 | office | 147 | (o) => { |
6 | office | 148 | new POST().process(config, request, response, root) |
149 | .on('log', (data) => { |
||
150 | log.log(data.severity, data.message); |
||
151 | }) |
||
152 | .on('data', (result) => { |
||
153 | response.setHeader('Content-Type', result.type); |
||
154 | response.writeHead(result.status); |
||
155 | result.data |
||
156 | .on('readable', () => result.data.pipe(response)) |
||
157 | .on('end', () => response.end()); |
||
158 | }); |
||
3 | office | 159 | return true; |
160 | } |
||
161 | ); |
||
1 | office | 162 | } |
163 | ).listen(config.net.port, config.net.address, () => { |
||
164 | log.info('HTTP Server accessible at: http://' + |
||
165 | config.net.address + |
||
166 | ':' + |
||
167 | config.net.port + |
||
168 | ' and serving files from directory: ' + |
||
169 | root |
||
170 | ); |
||
171 | }); |
||
172 | |||
173 | // Start HTTPs server if enabled. |
||
174 | if (config.ssl.enable) { |
||
175 | // Generate certificates for HTTPs. |
||
176 | certs.generate( |
||
177 | config.site.name, |
||
178 | config.net.address, |
||
179 | config.ssl.privateKeySize, |
||
180 | (certificates) => { |
||
181 | https.createServer( |
||
182 | // authentication, |
||
183 | { |
||
184 | key: certificates.privateKey, |
||
185 | cert: certificates.certificate, |
||
186 | }, |
||
187 | (request, response) => { |
||
3 | office | 188 | // Grab connecting address. |
189 | const address = request.socket.address(); |
||
190 | |||
1 | office | 191 | // Configuration path requested, so send the server configuration if allowed. |
192 | if(config.configuration.enable === true && |
||
193 | url.parse(request.url, true).path === |
||
194 | config.configuration.path) { |
||
195 | log.info('HTTP Server configuration requested by: ' + |
||
196 | address.address + ':' + |
||
197 | address.port |
||
198 | ); |
||
199 | response.setHeader('Content-Type', 'application/json'); |
||
200 | response.end(JSON.stringify(config)); |
||
201 | return; |
||
202 | } |
||
203 | |||
3 | office | 204 | // Switch on HTTP method. |
205 | was.lambda.switch( |
||
206 | request.method, |
||
207 | (o) => { |
||
208 | log.info('Unsupported HTTP \'' + request.method + '\' method requested by: ' + |
||
209 | address.address + ':' + |
||
210 | address.port |
||
211 | ); |
||
212 | }, |
||
213 | (o) => o === 'GET', |
||
214 | (o) => { |
||
215 | // Send the resource. |
||
216 | new GET().process(config, request, response, root) |
||
217 | .on('log', (data) => { |
||
218 | log.log(data.severity, data.message); |
||
219 | }) |
||
220 | .on('data', (result) => { |
||
221 | response.setHeader('Content-Type', result.type); |
||
222 | response.writeHead(result.status); |
||
223 | result.data |
||
224 | .on('readable', () => result.data.pipe(response)) |
||
225 | .on('end', () => response.end()); |
||
226 | }); |
||
227 | return true; |
||
228 | }, |
||
6 | office | 229 | (o) => o === 'POST', |
3 | office | 230 | (o) => { |
6 | office | 231 | new POST().process(config, request, response, root) |
232 | .on('log', (data) => { |
||
233 | log.log(data.severity, data.message); |
||
234 | }) |
||
235 | .on('data', (result) => { |
||
236 | response.setHeader('Content-Type', result.type); |
||
237 | response.writeHead(result.status); |
||
238 | result.data |
||
239 | .on('readable', () => result.data.pipe(response)) |
||
240 | .on('end', () => response.end()); |
||
241 | }); |
||
3 | office | 242 | return true; |
243 | } |
||
244 | ); |
||
1 | office | 245 | } |
246 | ).listen(config.ssl.port, config.ssl.address, () => { |
||
247 | log.info('HTTPs Server accessible at: https://' + |
||
248 | config.ssl.address + |
||
249 | ':' + |
||
250 | config.ssl.port + |
||
251 | ' and serving files from directory: ' + |
||
252 | root |
||
253 | ); |
||
254 | }) |
||
255 | } |
||
256 | ); |
||
257 | } |
||
258 | }); |