node-http-server – Blame information for rev 14

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.
19 const handler = require(
20 path
14 office 21 .resolve(
22 path.dirname(require.main.filename),
23 'src',
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(
40 path.dirname(require.main.filename),
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.
57 const log = new winston.Logger({
58 transports: [
59 new winston.transports.File({
60 level: 'info',
14 office 61 filename: path.resolve(
62 path.dirname(require.main.filename),
63 config.log.file
64 ),
1 office 65 handleExceptions: true,
66 json: false,
67 maxsize: 1048576, // 1MiB.
68 maxFiles: 10, // Ten rotations.
69 colorize: false,
14 office 70 timestamp: () => moment()
71 .format('YYYYMMDDTHHmmss')
1 office 72 }),
73 new winston.transports.Console({
74 level: 'info',
75 handleExceptions: true,
76 json: false,
77 colorize: true,
14 office 78 timestamp: () => moment()
79 .format('YYYYMMDDTHHmmss')
1 office 80 })
81 ],
82 exitOnError: false
83 });
84  
8 office 85 fs.realpath(argv.root, (error, root) => {
7 office 86 if (error) {
87 log.error('Could not find document root: ' + argv.root);
88 process.exit(1);
89 }
90  
91 // Start HTTP server.
92 http.createServer(
93 // authentication,
94 (request, response) =>
11 office 95 handler.process(config, request, response, root, (error, level) => {
8 office 96 switch (level) {
97 case handler.error.level.INFO:
98 log.info(error);
99 break;
100 case handler.error.level.WARN:
101 log.warn(error);
102 break;
103 case handler.error.level.ERROR:
104 log.error(error);
105 break;
106 }
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) =>
11 office 133 handler.process(config, request, response, root, (error, level) => {
8 office 134 switch (level) {
135 case handler.error.level.INFO:
136 log.info(error);
137 break;
138 case handler.error.level.WARN:
139 log.warn(error);
140 break;
141 case handler.error.level.ERROR:
142 log.error(error);
143 break;
144 }
145 })
146 ).listen(config.ssl.port, config.ssl.address, () => {
12 office 147 log.info('HTTPs Server accessible at: https://' +
148 config.ssl.address +
149 ':' +
150 config.ssl.port +
13 office 151 ' and serving files from directory: ' +
8 office 152 root
153 );
154 })
155 }
7 office 156 );
8 office 157 }
1 office 158 });