Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Michael Sofaer
committed
Jul 19, 2012
1 parent
4475274
commit e1a6efa
Showing
7 changed files
with
88 additions
and
4 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
.DS_Store | ||
node_modules |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
web: node web.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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); |