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