Skip to content

Instantly share code, notes, and snippets.

@RandomEtc
Created March 6, 2012 07:45
Show Gist options
  • Save RandomEtc/1984749 to your computer and use it in GitHub Desktop.
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
{
"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"
}
}
web: node app.js
"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