Permalink
Browse files

- initial import of php-pecl clients - add shapeways-node 116455c33f2…

…bd4c3a34ef2723d0a81be778dc889 as nodejs
  • Loading branch information...
d33psky committed Dec 5, 2012
1 parent d4ee960 commit d4d751160ba133fd0cc8f5e3fbe93a077986edac
Binary file not shown.
Binary file not shown.
View
@@ -0,0 +1,2 @@
+node_modules/
+*.log
View
@@ -0,0 +1,64 @@
+shapeways-node
+==============
+
+Sample application for the new Shapeways API in NodeJS
+
+
+## Installation
+
+```
+1) Download NodeJS - http://nodejs.org/
+2) Download npm - http://nodejs.org/download/
+3) Run `npm install` from the base directory to install necessary modules
+```
+
+## Configuration
+
+This sample app allows you to set configuration variables in one of two ways. The variables made available can be see in `cfg/config.cofee` which will compile to `cfg/config.js`.
+
+**Environment Variables (preferred)** - Node can easily read variables from your local environment, which allows you to securely set configuration variables like Consumer Key, etc locally rather than in the code.
+
+In your *nix console:
+```
+Run export CALLBACK_URL = 'http://localhost:3000/callback'
+```
+
+**Configuration File** - The file `cfg/config.coffee` contains a number of variables that by default pull from your environment but also fall back to defaults specified in the file.
+
+_Note: If you modify the configuration file you will need to recompile the coffee-script files into javascript by running `coffee -c .` in the base directory._
+
+
+**Variables**
+```
+# Shapeways OAuth URLs
+exports.REQUEST_TOKEN_URL = process.env.REQUEST_TOKEN_URL || 'http://api.test102.ehv.shapeways.net/oauth1/request_token/v1'
+exports.ACCESS_TOKEN_URL = process.env.ACCESS_TOKEN_URL || 'http://api.test102.ehv.shapeways.net/oauth1/access_token/v1'
+
+# Tokens from 'My Apps' on Shapeways
+exports.CUSTOMER_KEY = process.env.CUSTOMER_KEY || '0c4d90e3d93a690d1287086d066eb64c4c15914f'
+exports.CUSTOMER_SECRET = process.env.CUSTOMER_SECRET || '647939e8935d986413a83c6e428fd64fb7f668d5'
+
+# Your App's Callback URL
+exports.CALLBACK_URL = process.env.CALLBACK_URL || 'http://localhost:3000/callback'
+```
+
+
+## Usage
+
+```
+1) Run `node app.js`
+2) Load up http://localhost:3000/ in your browser to access the app (Note: must be connected to SW VPN)
+```
+
+
+
+## TODO
+* ~~Auth a user via OAuth~~
+* ~~Move hardcoded consumer keys into app's session variables~~
+* ~~Move auth logic into its own function~~
+* ~~View a list of the user's modules~~
+* ~~Upload a model~~
+* Edit a model's information
+* View a list of Materials
+* Add links to documentation
+* Create npm module
View
@@ -0,0 +1,114 @@
+express = require 'express'
+Auth = (require './lib/auth.js').Auth
+Model = (require './lib/model.js').Model
+cfg = require './cfg/config.js'
+
+app = express()
+app.use express.bodyParser()
+app.use express.cookieParser()
+app.use express.session
+ secret: 'blahblahblah' # Random hash for session store
+app.set 'views', __dirname + '/views'
+app.engine 'jade', require('jade').__express
+
+
+### Controllers ###
+
+auth = new Auth
+model = new Model
+
+
+### Routes ###
+
+app.get '/', (req, res) ->
+ if !isLoggedIn(req.session)
+ res.redirect '/login'
+ else
+ # User is already auth'd in and should be taken to their models
+ res.redirect '/model'
+
+
+app.get '/login', (req, res) ->
+ # Allow user to login using Shapeways and collect request token
+ auth.login (callback) ->
+ # Store oauth_token + secret in session
+ req.session.oauth_token = callback.oauth_token
+ req.session.oauth_token_secret = callback.oauth_token_secret
+ res.redirect callback.url
+
+app.get '/callback', (req, res) ->
+ # Grab the OAuth access token and store it in session
+ auth.handleCallback req.query.oauth_token, req.session.oauth_token_secret, req.query.oauth_verifier, (callback) ->
+ # Store oauth_access_token + secret in session
+ req.session.oauth_access_token = callback.oauth_access_token
+ req.session.oauth_access_token_secret = callback.oauth_access_token_secret
+ res.redirect '/' # Send the access token to the browser
+
+app.get '/upload', (req, res) ->
+ if !isLoggedIn(req.session)
+ res.redirect '/login'
+ else
+ # Display file upload dialog
+ res.render 'model/upload.jade'
+
+app.post '/model/upload', (req, res) ->
+ # Process model upload
+ if !isLoggedIn(req.session)
+ res.redirect '/login'
+ else
+ # Upload a model
+ model.putModel req.files.modelUpload, req.session.oauth_access_token, req.session.oauth_access_token_secret, (callback) ->
+ res.render 'model/upload_success.jade', { "callback": JSON.parse callback }
+
+app.get '/model/:id', (req, res) ->
+ if !isLoggedIn(req.session)
+ res.redirect '/login'
+ else
+ # Display a list of user's models
+
+ model.getModel req.params.id, req.session.oauth_access_token, req.session.oauth_access_token_secret, (callback) ->
+ if isJson req.url
+ res.send JSON.parse callback
+ else
+ res.render 'model/id.jade', { "callback": JSON.parse callback }
+
+app.get '/model*', (req, res) ->
+ if !isLoggedIn(req.session)
+ res.redirect '/login'
+ else
+ # Display a list of user's models
+
+ model.getModels req.session.oauth_access_token, req.session.oauth_access_token_secret, (callback) ->
+ if isJson req.url
+ res.send JSON.parse callback
+ else
+ res.render 'model/index.jade', { "callback": JSON.parse callback }
+
+
+
+
+app.get '/logout', (req, res) ->
+ # Allow the user to logout (clear local cookies)
+ console.log '--- LOGOUT ---'
+ console.log req.session
+ console.log '--- LOGOUT ---'
+ req.session.destroy()
+ res.redirect '/'
+
+### Start the App ###
+
+app.listen '3000'
+
+
+isLoggedIn = (session) ->
+ if !session.oauth_access_token
+ # User doesn't have any session data and therefore is not already logged in
+ return false
+ return true
+
+isJson = (url) ->
+ json = false
+ if (url.substring url.length-5) == ".json"
+ json = true
+ return json
+
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -0,0 +1,16 @@
+### Configuration file - Set your Shapeways/App specific variables here ###
+
+# Shapways API Version and server
+exports.API_VERSION = process.env.API_VERSION || 'v1'
+exports.API_SERVER = process.env.API_SERVER || 'api.shapeways.com'
+
+# Shapeways OAuth URLs
+exports.REQUEST_TOKEN_URL = process.env.REQUEST_TOKEN_URL || "http://#{@API_SERVER}/oauth1/request_token/#{@API_VERSION}"
+exports.ACCESS_TOKEN_URL = process.env.ACCESS_TOKEN_URL || "http://#{@API_SERVER}/oauth1/access_token/#{@API_VERSION}"
+
+# Tokens from 'My Apps' on Shapeways
+exports.CUSTOMER_KEY = process.env.CUSTOMER_KEY || '2c74e97709ac200da02222036df4ec3c5997ba51'
+exports.CUSTOMER_SECRET = process.env.CUSTOMER_SECRET || 'dc9bce571e5953db857356240b5e9778c641bd3e'
+
+# Your App's Callback URL
+exports.CALLBACK_URL = process.env.CALLBACK_URL || 'http://localhost:3000/callback'
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit d4d7511

Please sign in to comment.