This is the first part of what I plan to be a series of setting up an app built on PostGIS, Node.js, Leaflet.js, and D3.js to visualize the routes a climber has done (ticks) and those that they want to do (todos). Specifically the idea is to show where those routes are on the earth and some interactivity to help better understand details about a climbers past ticks and future todos. For this post we will ignore the map and just focus on getting Node and Postgis on OpenShift.
To make sure everyone is up to speed I figure I’ll go over some questions that might come up on this topic.
1. What is Node.js?
2. What isPostGIS?
Answer: PostGISis the amazingspatial bindings to PostgreSQLthat, when used together, make the best damn spatial database that exists today.
3. What are Leaflet.js and D3.js?
4. What is OpenShift?
Answer: OpenShiftis Red Hat’s P latform A s A S ervice (PAAS) solution for hosting web applications. It makes lifereally easy because OpenShift manages all the infrastructure around your app while also incorporating a bunch of really handy tools. It’s also really cheap for a simple non-resource needy app. OpenShift is also damn awesome.
5. Why am I writing about this topic?
Lets cover both methods just for fun.Method #1 – OpenShift’s Handy Instant App
1. After setting up an account with OpenShift create a new Node.js app following the basic instructions. There are lots of resources for doing this online by using either OpenShift’s command line tools, the OpenShift web interface, or both. Hereis an official doc from OpenShift on the topic.
NOTE: Leave the“Source Code” field blank if using the web interface. See Method #2 below for more details.
2. Now that you have an app created you can add a PostgreSQL database to it… This is amazingly easy. Simply go to your apps main page on the OpenShift website and clickthe “Add PostreSQL x.x” button in the databases section below your apps cartage section. This will ask if you want to add the PostgreSQL cartage to which you will click “yes”.Take note of your PostgreSQL’s credentials. Those are important.
3. To get PostGIS installed is easier than it used to be but you still have to SSH into your app. You can find the SSH command on your apps home page of the OpenShift website. This is what my SSH command looks like:
Shellssh [email protected]ud.com [email protected]cloud.com
Once you are SSH’d into your app you have access to PSQL which is the primary terminal based language for interacting with PostgreSQL. Type ‘psql’ in the terminal. The prompt should change a bit to something like this ‘APPNAME=#’. That’s good. That means your terminal is in psql mode. Now simply type:CREATE EXTENSION postgis; CREATE EXTENSIONpostgis;
The terminal should stall for a couple seconds and then output ‘CREATE EXTENSION’.Congratulations! You just setup a new Node.js app with a PostGIS database!
4. Now that you’vecreated your first OpenShift Nodeapp you can clone the Git repo that OpenShift requires you to use as a code repository. This is what your clone command looks like if typed in the terminal.git clone ssh://[email protected]/~/git/THENAMEOFYOURAPP.git/ git clonessh://[email protected]/~/git/THENAMEOFYOURAPP.git/
Why clone a repo you might ask?
Answer: OpenShift relies on the wildly popular Git as a version control system for your applications source code. They do some amazing things to make your life easier like managing your apps infrastructure, allowing simple integration of build tools (like Jenkins), and simply allowing you to redeploy an app with a ‘git push’ to your remote OpenShift repo. I love that I rarely have to SSH into my app to pull new source or other menial tasks.
This repo is fully configured to run on openshift. All you have to do now is make any desired code changes and push the changes to OpenShift via Git. This means deploying your changes to your app is as simple as:git push origin master git pushoriginmaster When running this command you should see all kinds of messages indicating fun messageslike your app and database cartridges are restarting. Method #2 – Adding an existing Node.js app to OpenShift
1. Step one for this method is very similar to method #1 except that you must use the option to add an existing Git repo to the application during the setup process. I havn’t done this via terminal but on the web interface you enter a URL to your Git repository in the “Source Code” field. You can optionally specify a branch.
2. Step 2, 3, and 4 are the same as in method #1 so…Follow those steps to add a PostgreSQL database, add the PostGIS extension, and clone the OpenShift Git repo.
3. Now, in my experience there was really only a couple changes to make to get my Node.js app to work using this method. They primarily have to do with the fact that OpenShift will not modify existing files or create certain configuration files for you. There is some documentationon this but I will outline my steps here for the sake of clarity.Make sure there is a package.json file in the root of the project. From what I can tellitis standard Node to have this so this step is basically a gimme. However, my default node configuration was different so I changed the ‘scripts.start’ and ‘main’ options to reference the correct files.You can see my whole package.json file below. Add some OpenShift specific settings to the top of your main app configuration file (mine is called server.js). This is needed because OpenShift uses environment variables in your app environment to set the port and domain for your app. The example code below will use port 8080 and a localhost domain (127.0.0.1) if the OpenShift environment variables don’t exist. This is useful for not having to change code between your dev and production environments. You can see my example below.
server.js OpenShift specific configuration example
That’s all folks! I think next I will cover the basics of using your new database but this is enough for now. I hope this helped some of you stumble a little less than I did.