By Daniel Du
I have been evaluating different cloud enviroment to host our viewer samples, in this post I will share my experience with Heroku, which is a Platform as a Service(PaaS) provider. The advantage of PaaS like Heroku is simplicity, we do not need to care about the infrastructure underneath, and we do not need to care about the load balance, scaling .etc. On the contrary, it gives us limited control with the infrastructure, and it only provides pre-defined hosting environments. Just in case something bad happened on the stack which our application is running, we have nothing to do it, what we can do is waiting for Heroku to fix it. Nevertheless, it is a good solution for a quick deployment and verification, and it is good enough for a simple sample application like what I did.
I am using the workflow-node.js-view.and.data.api sample which is a forked repo and I did some minor changes.
Develop and test on local machine
For view and Data API, we need a consumer key and secret key pair to do the authentication, protecting your data from being peeked by unauthorized hackers. We do not want to expose our secret keys to public right? We need to keep the keys on server side. And I am not comfortable to save the keys in source code, as I am pushing the source code to public repo of github all the time, sometime I may push the keys to public accidentally and it is not easy to clear the history, so I would prefer to set the keys at run time. In node.js, I can use process.env.variablename and pass the values to it when running the node server.
router.get('/token', function (req, res) {
var params = {
client_id: process.env.ClientId ,
client_secret: process.env.ClientSecret,
grant_type: 'client_credentials'
}
request.post(
process.env.BaseUrl + '/authentication/v1/authenticate',
{ form: params },
function (error, response, body) {
if (!error && response.statusCode == 200) {
res.send(body);
}
});
});
For local testing, I can pass the parameters like "key1=value1 key2=value2 node server.js" when starting the node server. Of cause it is too long to input it all them time when running, so I put it into a shell script, name it as run.sh. It can be bat file for windows.
ClientId=replace_with_your_consumer_key \
ClientSecret=replace_with_your_secret_key \
BaseUrl=https://developer.api.autodesk.com \
node server.js
On Mac/Linux, I need to make it execuable by :
chmod +x run.sh
When I test my node server at local side, I can simplily run the script:
./run.sh
With that I can working on my node.js viewer sample and test it on my local machine.
Deploy to heroku
With that, I am OK to deploy my web site to Heroku. As we said, heroku is pretty easy to use to host a website, and it provides up to 5 free apps.
Firstly, sign up on heroku.com for a free account, and then download and install the Heroku Toolbelt , you can learn more about the Heroku Command Line Interface if you want to know detailed instruction of the usage.
Next, log in to your Heroku account and follow the prompts to create a new SSH public key.
$ heroku login
Enter your Heroku credentials.
Email: <your heroku account here>
Password (typing will be hidden):
Authentication successful.
Create a new Heroku app through command line, it will create an app with random name if you do not give one in command line, it also add a git remote to Heroku so that you can deploy your code by git push.
$ heroku create
Creating quiet-shore-6917... done, stack is cedar-14
https://quiet-shore-6917.herokuapp.com/ | https://git.heroku.com/quiet-shore-6917.git
Git remote heroku added
Deploy your website to Heroku using Git once your are ready. Heroku will detect your app and setup the corresponding hosting environment, and then host it for you.
git push heroku master
Similarly, Heroku lets you externalise configuration - storing data such as encryption keys or external resource addresses in config vars. At runtime, config vars are exposed as environment variables to the application. Heroku will automatically setup the environment based on the contents of the .env file in your local directory, so you can create a .env
with the key pair:
ClientId=replace_with_your_consumer_key
ClientSecret=replace_with_your_secret_key
BaseUrl=https://developer.api.autodesk.com
Or set the config vars manualy by command:
heroku config:set ClientId=replace_with_your_consumer_key
heroku config:set ClientSecret=replace_with_your_secret_key
heroku config:set BaseUrl=https://developer.api.autodesk.com
Ensure that at least one instance of the app is running(do this only once):
$ heroku ps:scale web=1
Once the deployment is done, you can open the website by following command line, it launches your website in your default web browser. You can note down the URL if you'd like to switch to another browser. "heroku open" uses HTTPS by default, you need to use HTTP instead for now due to some known issue of viewer, for example, browse to http://quiet-shore-6917.heroku.com instead of https://quiet-shore-6917.heroku.com
heroku open
If everything works fine, your website is hosted on cloud now. You can keep working on the project on your local machine, making some changes, testing on local machine and commit when the test is passed, and deploy your changes again to heroku by git push.
git push heroku master
Ok, seems easy and simple, right?
Comments