Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
CHARUEL Thomas (INTERN) authored and CHARUEL Thomas (INTERN) committed Apr 26, 2017
0 parents commit f6d01e5
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
22 changes: 22 additions & 0 deletions bin/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';
const request = require('superagent');
const service = require('../server/service');
const http = require('http');

const server = http.createServer(service);
server.listen();

server.on('listening', function() {
console.log(`IRIS-Time is listening on ${server.address().port} in ${service.get('env')} mode.`);

const announce = () => {
request.put(`http://127.0.0.1:3000/service/time/${server.address().port}`, (err, res) => {
if(err) {
console.log(err);
console.log("Error connecting to Iris");
}
});
};
announce();
setInterval(announce, 15*1000);
});
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "iris-time",
"version": "1.0.0",
"description": "",
"main": "server/service.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.14.0",
"moment": "^2.15.0",
"superagent": "^2.3.0"
}
}
36 changes: 36 additions & 0 deletions server/service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

const express = require('express');
const service = express();
const request = require('superagent');
const moment = require('moment');


service.get('/service/:location', (req, res, next) => {

request.get('https://maps.googleapis.com/maps/api/geocode/json?address=' + req.params.location + '&key=GEO_API_KEY', (err, response) => {
if(err) {
console.log(err);
return res.sendStatus(500);
}

const location = response.body.results[0].geometry.location;
const timestamp = +moment().format('X');

request.get('https://maps.googleapis.com/maps/api/timezone/json?location=' + location.lat + ',' + location.lng + '&timestamp=' + timestamp + '&key=TIMEZONE_API_KEY', (err, response) => {
if(err) {
console.log(err);
return res.sendStatus(500);
}

const result = response.body;

const timeString = moment.unix(timestamp + result.dstOffset + result.rawOffset).utc().format('dddd, MMMM Do YYYY, h:mm:ss a');

res.json({result: timeString});
});
});

});

module.exports = service;

0 comments on commit f6d01e5

Please sign in to comment.