node-http-server – Blame information for rev 35

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