-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
CHARUEL Thomas (INTERN)
authored and
CHARUEL Thomas (INTERN)
committed
Apr 26, 2017
0 parents
commit f6d01e5
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + '×tamp=' + 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; |