Skip to content

Instantly share code, notes, and snippets.

@leefsmp
Created July 20, 2017 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leefsmp/9b0396241961565be147f3709c9428d4 to your computer and use it in GitHub Desktop.
Save leefsmp/9b0396241961565be147f3709c9428d4 to your computer and use it in GitHub Desktop.
Node.js AWS Lambda Server demo for Forge Viewer
'use strict'
///////////////////////////////////////////////////////////
// Node imports, must be installed with npm install and
// packaged along the lambda code zip
//
///////////////////////////////////////////////////////////
const Promise = require('bluebird')
const Forge = require('forge-apis')
const config = require('./config')
const path = require('path')
const fs = require('mz/fs')
const ejs = require('ejs')
///////////////////////////////////////////////////////////
// Lambda Handler, this is the method that gets invoked
// when the lambda server is triggered
//
///////////////////////////////////////////////////////////
const lambdaHandler = (event, context, callback) => {
// set the PATH
process.env['PATH'] =
process.env['PATH'] + ':' +
process.env['LAMBDA_TASK_ROOT']
// gets oauth token from Forge API
const oAuth2TwoLegged = new Forge.AuthClientTwoLegged(
process.env.FORGE_CLIENT_ID,
process.env.FORGE_CLIENT_SECRET,
['viewables:read'])
// packs the various tasks required to generate html code
const tasks = [
fs.readFile(path.resolve(__dirname, 'index.ejs'), 'utf8'),
fs.readFile(path.resolve(__dirname, 'app.ejs'), 'utf8'),
oAuth2TwoLegged.authenticate()
]
// wait for completion of each tasks
Promise.all(tasks).then((res) => {
const htmlEjs = res[0]
const appEjs = res[1]
const token = res[2]
// formats client code with token
const app = ejs.render(appEjs, {
accessToken: token.access_token,
urn: process.env.URN
})
// renders html code
const html = ejs.render(htmlEjs, {
viewer3D: config.viewer.viewer3D,
threeJS: config.viewer.threeJS,
style: config.viewer.style,
app: app
})
// returns response to caller
callback (null, html.replace(/"/g, '"'))
})
}
exports.handler = lambdaHandler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment