node-http-server – Blame information for rev 29
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | #!/usr/bin/env node |
2 | |||
9 | office | 3 | /*************************************************************************/ |
4 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
||
5 | /*************************************************************************/ |
||
6 | |||
1 | office | 7 | // Import packages. |
8 | const https = require('https'); |
||
7 | office | 9 | const http = require('http'); |
1 | office | 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'); |
||
7 | office | 16 | const dns = require('dns'); |
1 | office | 17 | |
8 | office | 18 | // Local imports. |
19 | const handler = require( |
||
20 | path |
||
14 | office | 21 | .resolve( |
15 | office | 22 | path.dirname(require.main.filename), |
23 | 'src', |
||
14 | office | 24 | 'handler' |
25 | ) |
||
8 | office | 26 | ); |
27 | const certs = require( |
||
28 | path |
||
14 | office | 29 | .resolve( |
30 | path.dirname(require.main.filename), |
||
31 | 'src', |
||
32 | 'certs' |
||
33 | ) |
||
8 | office | 34 | ); |
35 | |||
12 | office | 36 | // Load configuration file. |
8 | office | 37 | const config = require( |
38 | path |
||
14 | office | 39 | .resolve( |
15 | office | 40 | path.dirname(require.main.filename), |
14 | office | 41 | 'config' |
42 | ) |
||
8 | office | 43 | ); |
44 | |||
1 | office | 45 | // Get command-line arguments. |
46 | const argv = yargs |
||
47 | .version() |
||
48 | .option('root', { |
||
49 | alias: 'd', |
||
50 | describe: 'Path to the document root', |
||
51 | demandOption: true |
||
52 | }) |
||
53 | .help() |
||
54 | .argv |
||
55 | |||
56 | // Create various logging mechanisms. |
||
29 | office | 57 | // RFC5424 - { emerg: 0, alert: 1, crit: 2, error: 3, warning: 4, notice: 5, info: 6, debug: 7 } |
58 | winston.setLevels(winston.config.syslog.levels); |
||
1 | office | 59 | const log = new winston.Logger({ |
60 | transports: [ |
||
61 | new winston.transports.File({ |
||
62 | level: 'info', |
||
14 | office | 63 | filename: path.resolve( |
64 | path.dirname(require.main.filename), |
||
65 | config.log.file |
||
66 | ), |
||
1 | office | 67 | handleExceptions: true, |
68 | json: false, |
||
69 | maxsize: 1048576, // 1MiB. |
||
70 | maxFiles: 10, // Ten rotations. |
||
71 | colorize: false, |
||
14 | office | 72 | timestamp: () => moment() |
73 | .format('YYYYMMDDTHHmmss') |
||
1 | office | 74 | }), |
75 | new winston.transports.Console({ |
||
76 | level: 'info', |
||
77 | handleExceptions: true, |
||
78 | json: false, |
||
79 | colorize: true, |
||
14 | office | 80 | timestamp: () => moment() |
81 | .format('YYYYMMDDTHHmmss') |
||
1 | office | 82 | }) |
83 | ], |
||
84 | exitOnError: false |
||
85 | }); |
||
86 | |||
8 | office | 87 | fs.realpath(argv.root, (error, root) => { |
7 | office | 88 | if (error) { |
89 | log.error('Could not find document root: ' + argv.root); |
||
90 | process.exit(1); |
||
91 | } |
||
92 | |||
93 | // Start HTTP server. |
||
94 | http.createServer( |
||
95 | // authentication, |
||
96 | (request, response) => |
||
29 | office | 97 | handler.process(config, request, response, root, (result) => { |
98 | |||
99 | // Log the message by severity. |
||
100 | log.log(result.severity, result.message); |
||
101 | if(result.status) |
||
102 | response.status = result.status; |
||
103 | |||
104 | // If no data stream is made available or the content type is not |
||
105 | // set then there is no further reason to bother with the request. |
||
106 | if(typeof result.data === 'undefined' || result.data === null || |
||
107 | typeof result.type === 'undefined' || result.type === null) |
||
108 | return; |
||
109 | |||
110 | // Set the content type and send the data. |
||
111 | response.setHeader('Content-Type', result.type); |
||
112 | result.data |
||
113 | .on('open', () => result.data.pipe(response)); |
||
114 | |||
8 | office | 115 | }) |
7 | office | 116 | ).listen(config.net.port, config.net.address, () => { |
12 | office | 117 | log.info('HTTP Server accessible at: http://' + |
7 | office | 118 | config.net.address + |
12 | office | 119 | ':' + |
7 | office | 120 | config.net.port + |
13 | office | 121 | ' and serving files from directory: ' + |
8 | office | 122 | root |
7 | office | 123 | ); |
124 | }); |
||
125 | |||
126 | // Start HTTPs server if enabled. |
||
8 | office | 127 | if (config.ssl.enable) { |
7 | office | 128 | // Generate certificates for HTTPs. |
10 | office | 129 | certs.generate( |
7 | office | 130 | config.site.name, |
131 | config.net.address, |
||
8 | office | 132 | config.ssl.privateKeySize, |
133 | (certificates) => { |
||
134 | https.createServer( |
||
135 | // authentication, |
||
136 | { |
||
137 | key: certificates.privateKey, |
||
138 | cert: certificates.certificate, |
||
139 | }, |
||
140 | (request, response) => |
||
29 | office | 141 | handler.process(config, request, response, root, (result) => { |
142 | // Log the message by severity. |
||
143 | log.log(result.severity, result.message); |
||
144 | if(result.status) |
||
145 | response.status = result.status; |
||
146 | |||
147 | // If no data stream is made available or the content type is not |
||
148 | // set then there is no further reason to bother with the request. |
||
149 | if(typeof result.data === 'undefined' || result.data === null || |
||
150 | typeof result.type === 'undefined' || result.type === null) |
||
151 | return; |
||
152 | |||
153 | // Set the content type and send the data. |
||
154 | response.setHeader('Content-Type', result.type); |
||
155 | result.data |
||
156 | .on('open', () => result.data.pipe(response)); |
||
8 | office | 157 | }) |
158 | ).listen(config.ssl.port, config.ssl.address, () => { |
||
12 | office | 159 | log.info('HTTPs Server accessible at: https://' + |
160 | config.ssl.address + |
||
161 | ':' + |
||
162 | config.ssl.port + |
||
13 | office | 163 | ' and serving files from directory: ' + |
8 | office | 164 | root |
165 | ); |
||
166 | }) |
||
167 | } |
||
7 | office | 168 | ); |
8 | office | 169 | } |
1 | office | 170 | }); |