fluffy – Blame information for rev 2

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 #!/usr/bin/env node
2  
3 /*************************************************************************/
4 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
5 /*************************************************************************/
6  
7 // Import packages.
8 const https = require('https');
9 const http = require('http');
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');
16 const dns = require('dns');
17 const HttpCache = require("http-cache");
18  
19 // Local imports.
20 const Handler = require(
21 path
22 .resolve(
23 path.dirname(require.main.filename),
24 'src',
25 'handler'
26 )
27 );
28 const certs = require(
29 path
30 .resolve(
31 path.dirname(require.main.filename),
32 'src',
33 'certs'
34 )
35 );
36  
37 // Load configuration file.
38 const config = require(
39 path
40 .resolve(
41 path.dirname(require.main.filename),
42 'config'
43 )
44 );
45  
46 // Get command-line arguments.
47 const argv = yargs
48 .version()
49 .option('root', {
50 alias: 'd',
51 describe: 'Path to the document root',
52 demandOption: true
53 })
54 .help()
55 .argv
56  
57 // Create various logging mechanisms.
58 // RFC5424 - { emerg: 0, alert: 1, crit: 2, error: 3, warning: 4, notice: 5, info: 6, debug: 7 }
59 winston.setLevels(winston.config.syslog.levels);
60 const log = new winston.Logger({
61 transports: [
62 new winston.transports.File({
63 level: 'info',
64 filename: path.resolve(
65 path.dirname(require.main.filename),
66 config.log.file
67 ),
68 handleExceptions: true,
69 json: false,
70 maxsize: 1048576, // 1MiB.
71 maxFiles: 10, // Ten rotations.
72 colorize: false,
73 timestamp: () => moment()
74 .format('YYYYMMDDTHHmmss')
75 }),
76 new winston.transports.Console({
77 level: 'info',
78 handleExceptions: true,
79 json: false,
80 colorize: true,
81 timestamp: () => moment()
82 .format('YYYYMMDDTHHmmss')
83 })
84 ],
85 exitOnError: false
86 });
87  
88 fs.realpath(argv.root, (error, root) => {
89 if (error) {
90 log.error('Could not find document root: ' + argv.root);
91 process.exit(1);
92 }
93  
94 // Create server-side cache.
95 const httpcache = new HttpCache();
96  
97 // Start HTTP server.
98 http.createServer(
99 // authentication,
100 (request, response) => {
101 // Configuration path requested, so send the server configuration if allowed.
102 if(config.configuration.enable === true &&
103 url.parse(request.url, true).path ===
104 config.configuration.path) {
105 const address = request.socket.address();
106 log.info('HTTP Server configuration requested by: ' +
107 address.address + ':' +
108 address.port
109 );
110 response.setHeader('Content-Type', 'application/json');
111 response.end(JSON.stringify(config));
112 return;
113 }
114  
115 // Process and cache the resource.
116 httpcache(request, response, () => {
117 new Handler().process(config, request, response, root)
118 .on('log', (data) => {
119 log.log(data.severity, data.message);
120 })
121 .on('data', (result) => {
122 response.setHeader('Content-Type', result.type);
123 response.writeHead(result.status);
124 result.data
125 .on('readable', () => result.data.pipe(response))
126 .on('end', () => response.end());
127 });
128 });
129 }
130 ).listen(config.net.port, config.net.address, () => {
131 log.info('HTTP Server accessible at: http://' +
132 config.net.address +
133 ':' +
134 config.net.port +
135 ' and serving files from directory: ' +
136 root
137 );
138 });
139  
140 // Start HTTPs server if enabled.
141 if (config.ssl.enable) {
142 // Generate certificates for HTTPs.
143 certs.generate(
144 config.site.name,
145 config.net.address,
146 config.ssl.privateKeySize,
147 (certificates) => {
148 https.createServer(
149 // authentication,
150 {
151 key: certificates.privateKey,
152 cert: certificates.certificate,
153 },
154 (request, response) => {
155 // Configuration path requested, so send the server configuration if allowed.
156 if(config.configuration.enable === true &&
157 url.parse(request.url, true).path ===
158 config.configuration.path) {
159 const address = request.socket.address();
160 log.info('HTTP Server configuration requested by: ' +
161 address.address + ':' +
162 address.port
163 );
164 response.setHeader('Content-Type', 'application/json');
165 response.end(JSON.stringify(config));
166 return;
167 }
168  
2 office 169 // Process and cache the resource.
1 office 170 httpcache(request, response, () => {
171 new Handler().process(config, request, response, root)
172 .on('log', (data) => {
173 log.log(data.severity, data.message);
174 })
175 .on('data', (result) => {
176 response.setHeader('Content-Type', result.type);
177 response.writeHead(result.status);
178 result.data
179 .on('readable', () => result.data.pipe(response))
180 .on('end', () => response.end());
181 });
182 });
183 }
184 ).listen(config.ssl.port, config.ssl.address, () => {
185 log.info('HTTPs Server accessible at: https://' +
186 config.ssl.address +
187 ':' +
188 config.ssl.port +
189 ' and serving files from directory: ' +
190 root
191 );
192 })
193 }
194 );
195 }
196 });