Permalink
Browse files

Some node.js stuff for accounts

  • Loading branch information...
1 parent 4475274 commit e1a6efa38644d82f389978ebba2309a2e7f69561 @triggity triggity committed Jul 19, 2012
Showing with 88 additions and 4 deletions.
  1. +1 −0 .gitignore
  2. +1 −0 Procfile
  3. +4 −3 cloudflare.json
  4. +10 −0 db.js
  5. +11 −0 package.json
  6. +17 −1 test/sample-spec.js
  7. +44 −0 web.js
View
@@ -1 +1,2 @@
.DS_Store
+node_modules
View
@@ -0,0 +1 @@
+web: node web.js
View
@@ -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":{
@@ -24,8 +24,9 @@
"interface": [
{
"type": "string",
- "name": "food",
+ "name": "Favorite Food",
"description": "Please tell us your favorite food.",
+ "id": "food",
"domain_request": true
},
{
View
10 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();
View
@@ -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"
+ }
+}
View
@@ -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")
@@ -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(){
View
44 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.