node-http-server – Blame information for rev 31
?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. |
31 | office | 19 | const Handler = require( |
8 | office | 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) => |
||
31 | office | 97 | new Handler().process(config, request, response, root) |
98 | .on('log', (data) => { |
||
99 | log.log(data.severity, data.message); |
||
100 | }) |
||
101 | .on('data', (result) => { |
||
102 | // Set the response status. |
||
103 | response.status = result.status; |
||
104 | // Set the content type and send the data. |
||
105 | response.setHeader('Content-Type', result.type); |
||
106 | result.data |
||
107 | .on('readable', () => result.data.pipe(response)) |
||
108 | .on('end', () => response.end()); |
||
109 | }) |
||
7 | office | 110 | ).listen(config.net.port, config.net.address, () => { |
12 | office | 111 | log.info('HTTP Server accessible at: http://' + |
7 | office | 112 | config.net.address + |
12 | office | 113 | ':' + |
7 | office | 114 | config.net.port + |
13 | office | 115 | ' and serving files from directory: ' + |
8 | office | 116 | root |
7 | office | 117 | ); |
118 | }); |
||
119 | |||
120 | // Start HTTPs server if enabled. |
||
8 | office | 121 | if (config.ssl.enable) { |
7 | office | 122 | // Generate certificates for HTTPs. |
10 | office | 123 | certs.generate( |
7 | office | 124 | config.site.name, |
125 | config.net.address, |
||
8 | office | 126 | config.ssl.privateKeySize, |
127 | (certificates) => { |
||
128 | https.createServer( |
||
129 | // authentication, |
||
130 | { |
||
131 | key: certificates.privateKey, |
||
132 | cert: certificates.certificate, |
||
133 | }, |
||
134 | (request, response) => |
||
31 | office | 135 | new Handler().process(config, request, response, root) |
136 | .on('log', (data) => { |
||
137 | log.log(data.severity, data.message); |
||
138 | }) |
||
139 | .on('data', (result) => { |
||
140 | // Set the response status. |
||
141 | response.status = result.status; |
||
142 | // Set the content type and send the data. |
||
143 | response.setHeader('Content-Type', result.type); |
||
144 | result.data |
||
145 | .on('readable', () => result.data.pipe(response)) |
||
146 | .on('end', () => response.end()); |
||
147 | }) |
||
8 | office | 148 | ).listen(config.ssl.port, config.ssl.address, () => { |
12 | office | 149 | log.info('HTTPs Server accessible at: https://' + |
150 | config.ssl.address + |
||
151 | ':' + |
||
152 | config.ssl.port + |
||
13 | office | 153 | ' and serving files from directory: ' + |
8 | office | 154 | root |
155 | ); |
||
156 | }) |
||
157 | } |
||
7 | office | 158 | ); |
8 | office | 159 | } |
1 | office | 160 | }); |