Selaa lähdekoodia

Add society apis and logging

Matt Coles 9 vuotta sitten
vanhempi
commit
a7b6cdd4af
1 muutettua tiedostoa jossa 59 lisäystä ja 0 poistoa
  1. 59 0
      index.js

+ 59 - 0
index.js

@@ -3,10 +3,69 @@ var https = require('https');
3 3
 var express = require('express');
4 4
 var path = require('path');
5 5
 var forceSSL = require('express-force-ssl');
6
+var route_manager = require("../ip-project-server/utils/route-manager.js");
7
+var scheduler = require("../ip-project-server/presenters/schedule-controller.js");
8
+var bodyParser = require('body-parser');
9
+var basic_auth = require('basic-auth');
10
+var FileStreamRotator = require('file-stream-rotator');
11
+var morgan = require('morgan');
12
+var fs = require('fs');
13
+var logDirectory = 'log'
6 14
 var app = express();
7 15
 
16
+// Set up authentication and existing events.
17
+var log_passwd = fs.readFileSync('../ip-project-server/logpasswd', 'utf-8');
18
+var auth = function (req, res, next) {
19
+  function unauthorized(res) {
20
+    res.set('WWW-Authenticate', 'Basic realm=Authorization Required');
21
+    return res.sendStatus(401);
22
+  };
23
+  var user = basic_auth(req);
24
+  if (!user || !user.name || !user.pass) {
25
+    return unauthorized(res);
26
+  };
27
+  if (user.name === 'logs' && user.pass === log_passwd.trim()) {
28
+    return next();
29
+  } else {
30
+    return unauthorized(res);
31
+  };
32
+};
33
+scheduler.register_existing_events();
34
+
35
+// Always use SSL, comes first.
8 36
 app.use(forceSSL);
37
+app.use(bodyParser.json()); // for parsing application/json
38
+app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
39
+
40
+// Send CORS headers on api route.
41
+app.use('/soc-api/*', function(req, res, next) {
42
+  res.header("Access-Control-Allow-Origin", "*");
43
+  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
44
+  next();
45
+});
46
+
47
+// Logging comes next
48
+fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory)
49
+var accessLogStream = FileStreamRotator.getStream({
50
+  date_format: 'YYYYMMDD',
51
+  filename: logDirectory + '/access-%DATE%.log',
52
+  frequency: 'daily',
53
+  verbose: false
54
+})
55
+app.use(morgan('short', {stream: accessLogStream}))
56
+app.use(morgan('short'));
57
+
58
+// Check the /soc-api/ routes.
59
+app.use('/soc-api/v1/', route_manager);
60
+
61
+// Serve the log files
62
+app.use('/log', auth, express.static('log'));
63
+app.use('/log', auth, serveIndex('log', {'icons': true}));
64
+
65
+// Static site fallback
9 66
 app.use('/', express.static('../www/'));
67
+
68
+// 404 Anything Else
10 69
 app.use(function (req,res,next) {
11 70
   res.status(404).sendFile(path.resolve('../www_res/404/index.html'));
12 71
 });