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