Skip to content

Commit

Permalink
Added better logging to skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
Henrik Stene committed Nov 10, 2017
1 parent 7c1629f commit a0d8460
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"cors": "^2.7.1",
"express": "^4.3.2",
"path": "^0.12.7",
"request": "2.82.0"
"request": "2.82.0",
"winston": "^2.4.0"
}
}
26 changes: 17 additions & 9 deletions webapp.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
/* Logging */
var logger = require('winston');
logger.remove(logger.transports.Console);
logger.add(logger.transports.Console, {'timestamp':true});

/* Web server */
var express = require('express');
var cors = require('cors');
var teslajs = require('teslajs')
var app = express();
var requestClient = require('request');

app.use(cors());

var server = app.listen(3000, function() {
var host = server.address().address;
var port = server.address().port;

console.log('Server is listening at http://%s:%s', host, port);
logger.log("info", "Server is listening at http://%s:%s", host, port);
});

// API
app.get('/test', function (request, response) {
console.log(request);
logger.log("info", request);
authorize(request, response, function (err, res, body) {
console.log(body);
logger.log("info", body);
response.set('Content-Type', 'application/json');
response.statusCode = res.statusCode
return response.send(body);
Expand All @@ -35,9 +41,9 @@ function authorize(request, response, callback) {
var server_port = process.env.AUTH_SERVER_PORT;

if (server_ip == null || server_port == null) {
console.log("Missing auth server configuration.")
console.log("ip:" + server_ip)
console.log("port:" + server_port)
logger.log("error", "Missing auth server configuration.")
logger.log("error", "ip:" + server_ip)
logger.log("error", "port:" + server_port)
response.statusCode = 500;

return response.end("Missing auth server configuration.");
Expand All @@ -55,9 +61,11 @@ function authorize(request, response, callback) {
if (res && (res.statusCode === 200 || res.statusCode === 201)) {
callback(err, res, body);
} else {
if (res) {
console.log("Access denied:" + res.statusCode);
}
if (res) {
logger.log("info","Access denied:" + res.statusCode);
} else {
logger.log("info","Access denied with unknown response");
}

response.statusCode = 500
response.body = body;
Expand Down

0 comments on commit a0d8460

Please sign in to comment.