Playing with Meteor Again

It’s been awhile. I’m looking at Meteor again after my first look about 2 or 3 years ago. It seems to have matured into a rather cool web framework. It’s USP is real time updates between server and clients. Anything entered into the server database, is reflected (almost) immediately on each of the clients. If a client makes changes, those are reflected immediately on the local, and are background updated to the server, and then out to the other clients. It’s really slick.

It uses (by default) a mongodb as backend, and minimongos on each client, which live in the browser memory and have a subset of the full database cached locally. These listen for updates from the server and push updates to the server form teh minimongo. The client only reads and acts on the minimongo.

I decided to pay proper attention this time instead of skimming, so I’ve bought the Discover Meteor Book by Tom Coleman and Sacha Greif.

I’ve also done some digging into the architecture of Meteor because I have always wondered about how much control and scalability I might be sacrificing for the “magic”. This page from The Meteor Cookbook helped a lot (thanks). I now understand that I can remove the mongodb from the meteor server itself and deploy and admin as I wish in production. I am still a little hazy on scaling the meteor app itself, but that is coming in later chapters!

All in all, I’m enjoying myself. The long winter evenings will just fly.

Serving HTTPS out of Bottle using CherryPy

I wanted to bash a couple of ideas together and see how quickly I could get something serving data from mongodb over https. I’d just started messing with python and the pymongo driver and I was (and still am) loving the quick hit python coding gives. Adding bottle as a framework made it all webby and useful feeling into the bargain :-). What is not not to like?

What I really wanted was a sweet single point solution that bottle already gave me, but with https. Past self takes over at this point….

After a bunch of digging around and reading lots of blurb, the upshot is a lightweight https server called CherryPy using bottle querying a mongodb using a regex and outputting whatever is in the first result to the screen, with a list of some of the other results, allowing the user to refine their search. The app is mickey mouse, and you do have a post where I’d prefer a redirect and using a cookie (so that browser refreshes work nicely), but the amount of data you can potentially return using the re would blow out the cookie size pretty quickly. There’s a probably a better way(tm) 😉

To do a test run you will need:

  • mongod running
  • If you want it not to throw a wobbler (I haven’t put much exception handling in it), you’ll need at least one row in testbottle.trades collection with at least one attribute of ‘Trade_Id’.
    So create a test db and record or two to play with. Start mongo shell and add some:

    use testbottle
    
    //bung one in with an easy one match re to remember
    db.trades.insert(
    	{
    		"Trade_Id" : "000457390G-1",
    		"Version_Number" : 1,
    		"Notes" : "",
    		"Settlement_Consideration" : 1721.72
    	}
    )
    
    //let us bung a small shedload in starting with x
    for (var i = 0; i < 1000; i+=1) { 
    	db.trades.insert(
    		{
    		"Trade_Id" : "x" + i, 
    		Version_Number : i, 
    		Settlement_Consideration : Math.random() * i * 100
    		}
    	)
    };
    			
    		
  • python installed
  • pip install pymongo – OR if on windows – http://pypi.python.org/pypi/pymongo use the windows msi
  • pip install bottle
  • pip install cherrypy
  • pip install pyOpenSSL – OR if on windows – http://pypi.python.org/pypi/pyOpenSSL use the windows msi
  • Create a top level folder for this project. I used pyMongoBottlePoc
  • create your own ssl cert –
    openssl req -new -x509 -keyout testserver.pem -out testserver.pem -days 365 -nodes

    Put this in your project top level folder.

  • The find_trade.py code is here. Put this in your project top level directory
  • The hello page and show found pages are here. Put these in “views” subdirectory
  • Run it by typing at the command line
    python find_trade.py
  • Use it by pointing your web browser at
    https://localhost:8082/

find_trade.py is a webserver on your localhost port 8082 which initially shows a welcome page hello_get_value.tpl asking you to input a trade id. When you click submit, find_trades.py does a regex search in the mongo database returns a cursor (trades) and the first result (trade). Bottle renders them using show_trade.tpl.

I leaned really heavily on this post from Diego XA and on the 10Gen Dev course youtube videos week 1 which have an excellent intro to python course in them. Thanks guys.

MongoDb: Drop a Collection With runCommand

Stupidly I managed to create a collection with a space in the name. In the interactive shell various escape chars and quotes using the “db.CollectionName.drop()” solution failed to work. Since I created the blasted thing with a runCommand, I can fix it with a runCommand:

> db.runCommand( { drop : "my output" } );
{
"nIndexesWas" : 1,
"msg" : "indexes dropped for collection",
"ns" : "preNormalisation.my output",
"ok" : 1
}