node-http-server – Diff between revs 12 and 13
?pathlinks?
Rev 12 | Rev 13 | |||
---|---|---|---|---|
1 | #!/usr/bin/env node |
1 | #!/usr/bin/env node |
|
2 | |
2 | |
|
3 | /*************************************************************************/ |
3 | /*************************************************************************/ |
|
4 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
4 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
|
5 | /*************************************************************************/ |
5 | /*************************************************************************/ |
|
6 | |
6 | |
|
7 | // Import packages. |
7 | // Import packages. |
|
8 | const auth = require("http-auth"); |
8 | const auth = require("http-auth"); |
|
9 | const https = require('https'); |
9 | const https = require('https'); |
|
10 | const http = require('http'); |
10 | const http = require('http'); |
|
11 | const path = require('path'); |
11 | const path = require('path'); |
|
12 | const fs = require('fs'); |
12 | const fs = require('fs'); |
|
13 | const url = require('url'); |
13 | const url = require('url'); |
|
14 | const moment = require('moment'); |
14 | const moment = require('moment'); |
|
15 | const winston = require('winston'); |
15 | const winston = require('winston'); |
|
16 | const yargs = require('yargs'); |
16 | const yargs = require('yargs'); |
|
17 | const dns = require('dns'); |
17 | const dns = require('dns'); |
|
18 | |
18 | |
|
19 | // Local imports. |
19 | // Local imports. |
|
20 | const handler = require( |
20 | const handler = require( |
|
21 | path |
21 | path |
|
22 | .resolve(__dirname, 'src', 'handler') |
22 | .resolve(__dirname, 'src', 'handler') |
|
23 | ); |
23 | ); |
|
24 | const certs = require( |
24 | const certs = require( |
|
25 | path |
25 | path |
|
26 | .resolve(__dirname, 'src', 'certs') |
26 | .resolve(__dirname, 'src', 'certs') |
|
27 | ); |
27 | ); |
|
28 | |
28 | |
|
29 | // Load configuration file. |
29 | // Load configuration file. |
|
30 | const config = require( |
30 | const config = require( |
|
31 | path |
31 | path |
|
32 | .resolve(__dirname, 'config') |
32 | .resolve(__dirname, 'config') |
|
33 | ); |
33 | ); |
|
34 | |
34 | |
|
35 | // Get command-line arguments. |
35 | // Get command-line arguments. |
|
36 | const argv = yargs |
36 | const argv = yargs |
|
37 | .version() |
37 | .version() |
|
38 | .option('root', { |
38 | .option('root', { |
|
39 | alias: 'd', |
39 | alias: 'd', |
|
40 | describe: 'Path to the document root', |
40 | describe: 'Path to the document root', |
|
41 | demandOption: true |
41 | demandOption: true |
|
42 | }) |
42 | }) |
|
43 | .help() |
43 | .help() |
|
44 | .argv |
44 | .argv |
|
45 | |
45 | |
|
46 | // Create various logging mechanisms. |
46 | // Create various logging mechanisms. |
|
47 | const log = new winston.Logger({ |
47 | const log = new winston.Logger({ |
|
48 | transports: [ |
48 | transports: [ |
|
49 | new winston.transports.File({ |
49 | new winston.transports.File({ |
|
50 | level: 'info', |
50 | level: 'info', |
|
51 | filename: path.resolve(__dirname, config.log.file), |
51 | filename: path.resolve(__dirname, config.log.file), |
|
52 | handleExceptions: true, |
52 | handleExceptions: true, |
|
53 | json: false, |
53 | json: false, |
|
54 | maxsize: 1048576, // 1MiB. |
54 | maxsize: 1048576, // 1MiB. |
|
55 | maxFiles: 10, // Ten rotations. |
55 | maxFiles: 10, // Ten rotations. |
|
56 | colorize: false, |
56 | colorize: false, |
|
57 | timestamp: () => moment().format('YYYYMMDDTHHmmss') |
57 | timestamp: () => moment().format('YYYYMMDDTHHmmss') |
|
58 | }), |
58 | }), |
|
59 | new winston.transports.Console({ |
59 | new winston.transports.Console({ |
|
60 | level: 'info', |
60 | level: 'info', |
|
61 | handleExceptions: true, |
61 | handleExceptions: true, |
|
62 | json: false, |
62 | json: false, |
|
63 | colorize: true, |
63 | colorize: true, |
|
64 | timestamp: () => moment().format('YYYYMMDDTHHmmss') |
64 | timestamp: () => moment().format('YYYYMMDDTHHmmss') |
|
65 | }) |
65 | }) |
|
66 | ], |
66 | ], |
|
67 | exitOnError: false |
67 | exitOnError: false |
|
68 | }); |
68 | }); |
|
69 | |
69 | |
|
70 | fs.realpath(argv.root, (error, root) => { |
70 | fs.realpath(argv.root, (error, root) => { |
|
71 | if (error) { |
71 | if (error) { |
|
72 | log.error('Could not find document root: ' + argv.root); |
72 | log.error('Could not find document root: ' + argv.root); |
|
73 | process.exit(1); |
73 | process.exit(1); |
|
74 | } |
74 | } |
|
75 | |
75 | |
|
76 | // Create digest authentication. |
76 | // Create digest authentication. |
|
77 | const authentication = auth.digest({ |
77 | const authentication = auth.digest({ |
|
78 | realm: config.auth.realm, |
78 | realm: config.auth.realm, |
|
79 | file: path.resolve(__dirname, config.auth.digest) |
79 | file: path.resolve(__dirname, config.auth.digest) |
|
80 | }); |
80 | }); |
|
81 | |
81 | |
|
82 | // Start HTTP server. |
82 | // Start HTTP server. |
|
83 | http.createServer( |
83 | http.createServer( |
|
84 | // authentication, |
84 | // authentication, |
|
85 | (request, response) => |
85 | (request, response) => |
|
86 | handler.process(config, request, response, root, (error, level) => { |
86 | handler.process(config, request, response, root, (error, level) => { |
|
87 | switch (level) { |
87 | switch (level) { |
|
88 | case handler.error.level.INFO: |
88 | case handler.error.level.INFO: |
|
89 | log.info(error); |
89 | log.info(error); |
|
90 | break; |
90 | break; |
|
91 | case handler.error.level.WARN: |
91 | case handler.error.level.WARN: |
|
92 | log.warn(error); |
92 | log.warn(error); |
|
93 | break; |
93 | break; |
|
94 | case handler.error.level.ERROR: |
94 | case handler.error.level.ERROR: |
|
95 | log.error(error); |
95 | log.error(error); |
|
96 | break; |
96 | break; |
|
97 | } |
97 | } |
|
98 | }) |
98 | }) |
|
99 | ).listen(config.net.port, config.net.address, () => { |
99 | ).listen(config.net.port, config.net.address, () => { |
|
100 | log.info('HTTP Server accessible at: http://' + |
100 | log.info('HTTP Server accessible at: http://' + |
|
101 | config.net.address + |
101 | config.net.address + |
|
102 | ':' + |
102 | ':' + |
|
103 | config.net.port + |
103 | config.net.port + |
|
104 | '.' + |
104 | ' and serving files from directory: ' + |
|
105 | root |
105 | root |
|
106 | ); |
106 | ); |
|
107 | }); |
107 | }); |
|
108 | |
108 | |
|
109 | // Start HTTPs server if enabled. |
109 | // Start HTTPs server if enabled. |
|
110 | if (config.ssl.enable) { |
110 | if (config.ssl.enable) { |
|
111 | // Generate certificates for HTTPs. |
111 | // Generate certificates for HTTPs. |
|
112 | certs.generate( |
112 | certs.generate( |
|
113 | config.site.name, |
113 | config.site.name, |
|
114 | config.net.address, |
114 | config.net.address, |
|
115 | config.ssl.privateKeySize, |
115 | config.ssl.privateKeySize, |
|
116 | (certificates) => { |
116 | (certificates) => { |
|
117 | https.createServer( |
117 | https.createServer( |
|
118 | // authentication, |
118 | // authentication, |
|
119 | { |
119 | { |
|
120 | key: certificates.privateKey, |
120 | key: certificates.privateKey, |
|
121 | cert: certificates.certificate, |
121 | cert: certificates.certificate, |
|
122 | }, |
122 | }, |
|
123 | (request, response) => |
123 | (request, response) => |
|
124 | handler.process(config, request, response, root, (error, level) => { |
124 | handler.process(config, request, response, root, (error, level) => { |
|
125 | switch (level) { |
125 | switch (level) { |
|
126 | case handler.error.level.INFO: |
126 | case handler.error.level.INFO: |
|
127 | log.info(error); |
127 | log.info(error); |
|
128 | break; |
128 | break; |
|
129 | case handler.error.level.WARN: |
129 | case handler.error.level.WARN: |
|
130 | log.warn(error); |
130 | log.warn(error); |
|
131 | break; |
131 | break; |
|
132 | case handler.error.level.ERROR: |
132 | case handler.error.level.ERROR: |
|
133 | log.error(error); |
133 | log.error(error); |
|
134 | break; |
134 | break; |
|
135 | } |
135 | } |
|
136 | }) |
136 | }) |
|
137 | ).listen(config.ssl.port, config.ssl.address, () => { |
137 | ).listen(config.ssl.port, config.ssl.address, () => { |
|
138 | log.info('HTTPs Server accessible at: https://' + |
138 | log.info('HTTPs Server accessible at: https://' + |
|
139 | config.ssl.address + |
139 | config.ssl.address + |
|
140 | ':' + |
140 | ':' + |
|
141 | config.ssl.port + |
141 | config.ssl.port + |
|
142 | '.' + |
142 | ' and serving files from directory: ' + |
|
143 | root |
143 | root |
|
144 | ); |
144 | ); |
|
145 | }) |
145 | }) |
|
146 | } |
146 | } |
|
147 | ); |
147 | ); |
|
148 | } |
148 | } |
|
149 | }); |
149 | }); |
|
150 | |
150 | |
|
151 | |
151 | |
|
152 |
|
152 |
|
|
153 | |
153 | |
|
154 | |
154 | |
|
155 | |
155 | |