Wednesday, July 2, 2014

How to create angular app and host on heroku using cosmos frameowk in 5 simple steps

Angular seed project is configured to use npm to do basic tasks like download required packages. You should have node installed on your machine. You won't need it after you download the required package. Cosmos will host the application using tornado in both development and production environment. In production use a reverse proxy like nginx to serve the static files. Now follow the steps to create and run the angular-seed application.

Create angular project using cosmosadmin

If you do not have mongodb or virtualenv installed install those first. For ubuntu its like:

sudo apt-get install mongodb
sudo apt-get install python-virtualenv

First create a virtualenv and activate it:

virtualenv --no-site-packages testenv
source testenv/bin/activate
Now install cosmos inside it:
pip install cosmos

Run new-project command with angular from console from any directory to create new project:

cosmosadmin new-project angular

This will create basic cosmos application and download angular-seed project from github. Here is sample output.

new-project
-----------Cloning angular seed project--------------

Cloning into 'angular-seed'...
remote: Counting objects: 2475, done.
remote: Compressing objects: 100% (1237/1237), done.
remote: Total 2475 (delta 1071), reused 2475 (delta 1071)
Receiving objects: 100% (2475/2475), 13.80 MiB | 2.19 MiB/s, done.
Resolving deltas: 100% (1071/1071), done.
Checking connectivity... done.
----------- You should run "npm install" from angular-seed directory now -------------

You should now open settings.py and local_settings.py files to adjust database and other settings.

Now change directory to angular-seed and run npm install:

cd angular-seed
npm install

This will download all required packages for angular-seed application.

Now go back to project directory and run:

cd ..
python cosmosmain.py

You may now browse to http://localhost:8080/ to view your newly created application.

Host the app on heroku

If you want to host your application on heroku you need Procfile and requirements.txt files. You may create these files using following command:

python cosmosmain.py add-herokusettings

Now run the application using foreman (you should have heroku toolbelt installed on the machine):

foreman start

You may now browse to http://localhost:5000/ to view your application. Now commit and push the application to heroku for deployment.