Skip to content

Commit

Permalink
Some node.js stuff for accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Sofaer committed Jul 19, 2012
1 parent 4475274 commit e1a6efa
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
.DS_Store
node_modules
1 change: 1 addition & 0 deletions Procfile
@@ -0,0 +1 @@
web: node web.js
7 changes: 4 additions & 3 deletions cloudflare.json
Expand Up @@ -2,9 +2,9 @@
"main":"public/javascripts/sample_app.js",
"name":"Sample App",
"description": "A sample application to get you started with CloudFlare apps",
"version": "0.0.2a",
"version": "0.0.2b",
"account": {
"callback_url": "https://localhost:3000/api",
"callback_url": "http://10.0.2.2:3000/api",
"user_fields": ["email"]
},
"config":{
Expand All @@ -24,8 +24,9 @@
"interface": [
{
"type": "string",
"name": "food",
"name": "Favorite Food",
"description": "Please tell us your favorite food.",
"id": "food",
"domain_request": true
},
{
Expand Down
10 changes: 10 additions & 0 deletions db.js
@@ -0,0 +1,10 @@
#! /usr/bin/env node
var pg = require('pg');

function create_db(){
pg.connect(process.env.DATABASE_URL || "tcp://michael:1234@localhost/michael", function(err, client) {
client.query("CREATE TABLE accounts(account_id BIGINT PRIMARY KEY, status VARCHAR(8), cdate TIMESTAMPTZ default now());");
})
}

create_db();
11 changes: 11 additions & 0 deletions package.json
@@ -0,0 +1,11 @@
{
"name": "node-example",
"version": "0.0.1",
"dependencies": {
"pg": "0.6.15",
"express": "2.5.x"
},
"engines": {
"node": "0.8.x"
}
}
18 changes: 17 additions & 1 deletion test/sample-spec.js
Expand Up @@ -37,7 +37,7 @@ describe(["app_json"], function(json){
describe("interface", function(){
describe("select", function(){
beforeEach(function(){
this.select = json.config.interface[0]
this.select = json.config.interface[1]
})
it("should have an id", function(){
expect(this.select.id).toEqual("lottery")
Expand All @@ -56,6 +56,22 @@ describe(["app_json"], function(json){
})

})
describe("string", function(){
beforeEach(function(){
this.select = json.config.interface[0]
})
it("should have an id", function(){
expect(this.select.id).toEqual("food")
})
it("should have a name and description", function(){
expect(this.select.name).toBeDefined()
expect(this.select.description).toBeDefined()
})
it("should claim to be a select", function(){
expect(this.select.type).toEqual("string")
})
})

})
describe("assets", function(){
describe("logos", function(){
Expand Down
44 changes: 44 additions & 0 deletions web.js
@@ -0,0 +1,44 @@
var express = require('express');
var qs = require('querystring');
var crypto = require('crypto');
var pg = require('pg');

var app = express.createServer(express.logger());
app.use(express.bodyParser());

function create_account(account_id, respond){
pg.connect(process.env.DATABASE_URL || "tcp://michael:1234@localhost/michael", function(err, client) {
console.log(err)
var query = client.query('insert into accounts(account_id) values ('+account_id+')');

query.on('end', function() {
respond("approve");
});

query.on('error', function() {
respond("approve");
});
});
}

function valid(data) {
var hmac_secret = '09aed14f2a579b0f50965418c67b600d';
var hmac = crypto.createHmac("sha256", hmac_secret);
hmac.update(data.account_id);
return data.sig == hmac.digest('hex');
}
app.post('/api/accounts', function(request, response) {
if (!valid(request.body)) {
response.send("Bad HMAC");
} else {
var respond = function(status){
response.send('{"account_id":"'+request.body.account_id+'","status":"'+status+'"}');
}
create_account(request.body.account_id, respond);
};
});

var port = process.env.PORT || 5000;
app.listen(port, function() {
console.log("Listening on " + port);
});

0 comments on commit e1a6efa

Please sign in to comment.