Created
July 27, 2012 14:41
-
-
Save gavinmyers/3188410 to your computer and use it in GitHub Desktop.
REST, CoffeeScript, NodeJS, Redis
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
express = require "express" | |
crypto = require "crypto" | |
user = require "./src/user" | |
project = require "./src/project" | |
app = express.createServer() | |
app.listen 8008 | |
app.get '/', (req, res) -> | |
res.write JSON.stringify {version:0.3} | |
res.end "" | |
app.get '/api/:token', (req,res) -> | |
token = req.params.token | |
user.get token, (c) -> | |
res.end JSON.stringify c | |
app.get '/api/:token/project/new/:name', (req,res) -> | |
token = req.params.token | |
name = req.params.name | |
user.get token, (agn) -> | |
if !agn then return res.end "" | |
id = crypto.randomBytes(18).toString('hex') | |
org = agn.client | |
project.create {"name":name,"client":org,"id":id}, (crs) -> | |
res.write JSON.stringify crs | |
res.end "" | |
app.get '/api/:token/project/:id', (req,res) -> | |
token = req.params.token | |
id = req.params.id | |
user.get token, (agn) -> | |
if !agn then return res.end "" | |
org = agn.client | |
project.get org,id, (crs) -> | |
res.write JSON.stringify crs | |
res.end "" |
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
async = require "async" | |
request = require "request" | |
redis = require "redis" | |
crypto = require "crypto" | |
main = require "../main" | |
url = "http://127.0.0.1:8008" | |
console.log '\u001B[2J\u001B[0;0f' | |
rdscl = redis.createClient() | |
org = crypto.randomBytes(18).toString('hex') | |
token = crypto.randomBytes(18).toString('hex') | |
invorg = crypto.randomBytes(18).toString('hex') | |
invtoken = crypto.randomBytes(18).toString('hex') | |
currentproject = "" | |
invproject = "1-abc-def-ghi-1" | |
async.series [ | |
(cb) -> | |
console.log "==> INIT" | |
rdscl.flushdb() | |
cb 0,null | |
(cb) -> | |
console.log "==> SETUP" | |
rdscl.set "client:#{org}", JSON.stringify {name:"ABC Corp",id:org, type:0} | |
rdscl.set "token:#{token}", JSON.stringify {client:org} | |
rdscl.get "token:#{token}", (e,c) -> | |
obj = JSON.parse c | |
if obj.client != org | |
throw new Error "redis isn't working with token/org" | |
cb 0,null | |
(cb) -> request.get "#{url}", (err,res,body) -> | |
console.log "==> VERSION" | |
obj = JSON.parse body | |
if obj.version != 0.3 | |
throw new Error "version not setup" | |
cb 0,null | |
(cb) -> request.get "#{url}/api/#{token}",(err,res,body) -> | |
console.log "==> TOKEN" | |
obj = JSON.parse body | |
if obj.client != org | |
throw new Error "redis/org/token issue #{org} is not " + obj.client | |
cb 0,null | |
(cb) -> request.get "#{url}/api/#{invtoken}",(err,res,body) -> | |
console.log "==> INVALID TOKEN" | |
obj = JSON.parse body | |
if obj | |
throw new Error "body should be null, not #{obj} nothing to do here" | |
cb 0,null | |
(cb) -> request.get "#{url}/api/#{invtoken}/project/new/test1",(err,res,body) -> | |
if body | |
throw new Error "body should be null when creating a new project" | |
console.log "==> NEW project INVALID" | |
cb 0,null | |
(cb) -> request.get "#{url}/api/#{token}/project/new/test%20123",(err,res,body) -> | |
if !body | |
throw new Error "body should not be null when creating a new project" | |
obj = JSON.parse body | |
currentproject = obj.id | |
if obj.name != "test 123" | |
throw new Error "project name " + obj.name + " not set" | |
console.log "==> NEW project VALID" | |
cb 0,null | |
(cb) -> request.get "#{url}/api/#{token}/project/new/test%20456",(err,res,body) -> | |
if !body | |
throw new Error "body should not be null when creating a new project" | |
obj = JSON.parse body | |
currentproject = obj.id | |
if obj.name != "test 456" | |
throw new Error "project name " + obj.name + " not set" | |
console.log "==> NEW project VALID" | |
cb 0,null | |
(cb) -> request.get "#{url}/api/#{token}/project/#{currentproject}",(err,res,body) -> | |
obj = JSON.parse body | |
if !obj | |
throw new Error "body should not be null when viewing project #{currentproject}" | |
console.log "==> VIEW project VALID" | |
cb 0,null | |
(cb) -> request.get "#{url}/api/#{token}/project/#{invproject}",(err,res,body) -> | |
obj = JSON.parse body | |
if obj | |
throw new Error "silly redis, no project exits for #{invproject}" | |
console.log "==> VIEW project INVALID" | |
cb 0,null | |
(cb) -> request.get "#{url}/api/#{token}/project/list",(err,res,body) -> | |
obj = JSON.parse body | |
if obj.length != 2 | |
throw new Error "we should have 2 projects" | |
console.log "==> LIST projectS" | |
cb 0,null | |
], | |
(err,res) -> | |
console.log '\u001B[2J\u001B[0;0f' | |
console.log ":-)" | |
process.exit 1 |
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
redis = require "redis" | |
rdscl = redis.createClient() | |
exports.get = (org,id,cb) -> | |
token = "client:#{org}:project:#{id}" | |
if id == 'list' | |
rdscl.smembers token, (e,c) -> | |
rdscl.mget c, (e2,c2) -> | |
for i in [0..c2.length - 1] | |
c2[i] = JSON.parse c2[i] | |
cb c2 | |
else | |
rdscl.get token, (e,c) -> | |
cb JSON.parse c | |
exports.create = (newproject,cb) -> | |
org = newproject.client | |
id = newproject.id | |
key = "client:#{org}:project:#{id}" | |
rdscl.set key, JSON.stringify newproject | |
rdscl.sadd "client:#{org}:project:list", key | |
rdscl.get key,(e,c) -> | |
cb JSON.parse c |
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
redis = require "redis" | |
rdscl = redis.createClient() | |
exports.get = (id,cb) -> | |
token = "token:#{id}" | |
rdscl.get token, (e,c) -> | |
ret = JSON.parse c | |
if !ret then ret = "" | |
cb ret | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment