Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- config = require './config.js'
- # Filesystem
- fs = require 'fs'
- # Crypto
- crypto = require 'crypto'
- # Request
- request = require 'request'
- # Express
- express = require 'express'
- application = express()
- # Express add bodyparser middleware
- application.use \
- express.bodyParser \
- uploadDir: 'tmp/'
- # Application logic here
- # Compute the signature for vuforia
- compute_signature = (data, verb, request_path, today)->
- md5_hash = crypto.createHash 'md5'
- md5_hash.update(JSON.stringify data)
- string_to_sign =
- # Add the verb
- verb + "\n" +
- # Content-MD5
- md5_hash.digest('hex') + "\n" +
- # Content-type
- "application/json" + "\n" +
- # Date in RFC 2616 format / rfc1123-date
- today.toUTCString() + "\n" +
- # Request path
- request_path
- console.log 'String to sign: --------------------'
- console.log string_to_sign
- console.log '------------------------------------'
- hmac_sha1 = crypto.createHmac('sha1', config.server_secret_key).update(string_to_sign).digest('hex')
- signature = new Buffer(hmac_sha1).toString('base64')
- return signature
- # Main function to post to vuforia
- vuforia_request_completed = (error, response, body)->
- console.log 'Vuforia completed: ------------------------------------'
- console.log "Error: #{error}"
- #console.log "Response: "
- #console.log response
- console.log "Body: "
- console.log body
- console.log "-------------------------------------------------------"
- vuforia_send = (req)->
- # Read the image into a base64 encoded string
- req.body.image_base64 = fs.readFileSync \
- req.files.image.path
- , encoding: 'base64'
- # Create the data to be posted to vuforia
- req.send_data =
- name: req.body.name
- width: req.body.width
- image: req.body.image_base64
- active_flag: true
- application_metadata: req.body.application_metadata
- today = new Date()
- signature = compute_signature req.send_data, 'POST', '/targets', today
- request_options =
- url: config.urls.add_target
- method: 'POST'
- body: JSON.stringify(req.send_data)
- headers:
- 'Authorization': "VWS #{config.server_access_key}:#{signature}"
- 'Content-type': 'application/json'
- request \
- request_options
- , vuforia_request_completed
- console.log "Vuforia request sent!"
- true
- marker_set = (req, res)->
- # http://jsfiddle.net/gg9tM/ - example post form
- res.response_data =
- status: 'ok'
- message: 'request unprocessed'
- vuforia_send req
- res.send(JSON.stringify res.response_data)
- # do the vuforia post
- true
- # Set the routes
- ## Setting the markers
- application.post \
- '/marker/set'
- , marker_set
- ## Checking markers status
- # todo: ...
- application.listen config.port
- console.log "Application listening on port #{config.port}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement