Created
March 6, 2012 07:45
-
-
Save RandomEtc/1984749 to your computer and use it in GitHub Desktop.
@mourner's suncalc for heroku... moved to https://github.com/RandomEtc/suncalc-api
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
{ | |
"name": "suncalc-api", | |
"description": "An HTTP Pony for @mourner's suncalc js library.", | |
"version": "0.0.0", | |
"main": "app.js", | |
"engines": { | |
"node": "~v0.4.7" | |
}, | |
"dependencies": { | |
"suncalc": "~1.0.0", | |
"express": "~2.5.8" | |
} | |
} |
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
web: node app.js |
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
"use strict"; | |
var express = require("express"), | |
SunCalc = require("suncalc"); | |
var app = express.createServer(); | |
app.configure(function(){ | |
app.enable("jsonp callback"); | |
app.use(express.bodyParser()); | |
app.use(app.router); | |
app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); | |
}); | |
function requireFloat(name) { | |
return function(req,res, next) { | |
if (name in req.query && !isNaN(parseFloat(req.query[name]))) { | |
return next(); | |
} else { | |
res.send(name + " required; must be a float.", 400); | |
} | |
} | |
} | |
app.get('/times', requireFloat('lat'), requireFloat('lon'), function(req,res){ | |
var lat = parseFloat(req.query.lat), | |
lon = parseFloat(req.query.lon), | |
date = Date.parse(req.query.date) || Date.now(); | |
res.send(SunCalc.getTimes(date, lat, lon)); | |
}) | |
app.get('/position', requireFloat('lat'), requireFloat('lon'), function(req,res){ | |
var lat = parseFloat(req.query.lat), | |
lon = parseFloat(req.query.lon), | |
date = Date.parse(req.query.date) || Date.now(); | |
res.send(SunCalc.getPosition(date, lat, lon)); | |
}) | |
app.listen(process.env.PORT); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment