node-http-server – Blame information for rev 13

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