Skip to content

Commit

Permalink
setting up a basic flapi server
Browse files Browse the repository at this point in the history
  • Loading branch information
joelongstreet committed Jan 26, 2014
0 parents commit bf1be4d
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
node_modules
.DS_Store
*.db
15 changes: 15 additions & 0 deletions index.js
@@ -0,0 +1,15 @@
var express = require('express');
var http = require('http');
var routes = require('./lib/routes');
var app = express();


app.get('/', routes.getAuthURL);
app.get('/user/authorize', routes.getAuthURL);
app.get('/auth_callback', routes.authCallback);
app.get('/api/:method', routes.makeFlickrRequest);


http.createServer(app).listen(3000, function(){
console.log('Express server listening on port 3000');
});
39 changes: 39 additions & 0 deletions lib/flickrConnect.js
@@ -0,0 +1,39 @@
var dirty = require('dirty');
var Flapi = require('flapi');


exports.createClient = function(next){

var settings = dirty('./db/settings.db');
settings.on('load', function(){

var opts = {
oauth_consumer_key : process.env.FLICKR_KEY,
oauth_consumer_secret : process.env.FLICKR_SECRET,
perms : 'delete'
};

var oauthOpts = settings.get('oauth');
if(oauthOpts){
opts.oauth_token = oauthOpts.oauth_token;
opts.oauth_token_secret = oauthOpts.oauth_token_secret;
}

makeNewCient(opts);
});


var makeNewCient = function(opts){
var client = new Flapi(opts);
if(!opts.oauth_token){
client.authApp('http://localhost:3000/auth_callback', function(oauthResults){
console.log('Flickr Initialized');
settings.set('oauth', oauthResults);
next(client);
});
} else{
console.log('Flickr Initialized');
next(client);
}
};
};
47 changes: 47 additions & 0 deletions lib/routes.js
@@ -0,0 +1,47 @@
var dirty = require('dirty');
var users = dirty('./db/users.db');

var flickrClient;
require('./flickrConnect').createClient(function(client){
flickrClient = client;
});


exports.getAuthURL = function(req, res){
res.redirect(flickrClient.getUserAuthURL());
};


// If there's an oauth_token, we know this is a user auth.
// otherwise, it's just the app trying to auth, let it pass
exports.authCallback = function(req, res){
if(req.query.oauth_token){
flickrClient.getUserAccessToken(req.query.oauth_verifier, function(accessToken){
users.set(accessToken.user_nsid, accessToken);
res.send('Succesfully Authorized.');
});
} else {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end();
}
};


exports.makeFlickrRequest = function(req, res){
var accessToken = users.get(req.query.user_id);
var apiMethod = 'flickr.' + req.params.method;
var params = {};

for(var key in req.query){
params[key] = req.query[key];
}

flickrClient.api({
method : apiMethod,
params : params,
accessToken : accessToken,
next : function(data){
res.send(data);
}
});
};
13 changes: 13 additions & 0 deletions package.json
@@ -0,0 +1,13 @@
{
"name": "flickr-app-server",
"version": "0.0.0",
"description": "",
"main": "index.js",
"repository": "",
"author": "Joe Longstreet",
"license": "BSD",
"dependencies": {
"dirty": "~0.9.7",
"express": "~3.4.8"
}
}

0 comments on commit bf1be4d

Please sign in to comment.