diff --git a/bin/run.js b/bin/run.js new file mode 100644 index 0000000..64b3bb7 --- /dev/null +++ b/bin/run.js @@ -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); +}); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..6c5cec3 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/server/service.js b/server/service.js new file mode 100644 index 0000000..00f8f4a --- /dev/null +++ b/server/service.js @@ -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; \ No newline at end of file