node-http-server – Blame information for rev 9

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  
29 // Configuration file.
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) =>
8 office 86 handler.handleClient(config, request, response, root, (error, level) => {
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, () => {
100 log.info('HTTP Server is listening on: ' +
101 config.net.address +
102 ' and port: ' +
103 config.net.port +
104 ' whilst serving files from: ' +
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.
8 office 112 certs.generateCertificates(
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) =>
124 handler.handleClient(config, request, response, root, (error, level) => {
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, () => {
138 // Print information on server startup.
139 log.info('HTTPs Server is listening on: ' +
140 config.net.address +
141 ' and port: ' +
142 config.net.port +
143 ' whilst serving files from: ' +
144 root
145 );
146 })
147 }
7 office 148 );
8 office 149 }
1 office 150 });