Where Am I Again?
Spending some of my 20’s traveling was always a dream of mine since I first traveled abroad to visit my sister in Barcelona when I was 14, so towards the end of 2016 I quit my job managing, started my own software freelance biz, and booked a one-way flight abroad. While preparing for this trip and searching for my first client, I developed a couple little apps that allowed me to track my location as I hop from country to country. This automates the mundane process of me having to manually type into my website, “This week I’m in Rishikesh, India” and it also gives me a database that shows I went from Thailand, to Malaysia, to Turkey, and so on. You can see the result of all of this in action on my website where I share my ‘Current Location’.
I liked building this project and I thought it would be cool to break down the technical bits of how I did it for folks who might be interested. Read on if you’re one of those folks!
Grabbing my Location
To properly track my current location in the world, I needed a device that came with me everywhere. My MacBook-Pro fit the bill for this so I decided to write a small OSX app that did the job. I ended up going with a Mac Menubar application because it made sense to build something with not much of a UI. I aptly termed that project WhereAmIMenuBar and it’s dead simple. It’s a small Swift app that grabs the current IP address of my machine, fetches the location of that IP from http://ip-api.com, and then POSTs it as a JSON blob to a small web service that we’ll get into later. Here is the small bit of code that’ll give you a gist of what I’m talking about:
This runs every hour, but really only POSTs my location if my IP address changes. It also includes a small MenuBar UI (who would have guessed?) to allow me to update manually. I set this up so that it is one of the startup items on my MacBook and now I broadcast my location to my web service whenever I open up my machine somewhere new. Pretty cool, huh?
Storing my Location
Okay, so I’ve got an app that broadcasts my location, but where does that end up? Well it ends up on the web of course. Another small app that I’ve dubbed whereami.com serves as an API for my location data. At the time of writing this I was interested in learning more about the Elixir programming language so I wrote this app in Elixir and used Plug as my web framework. Plug is the equivalent of Ruby’s Sinatra/Rack in the Elixir world, which is perfect for super small APIs like this. Here is the 5 lines of code which accomplish accepting that location data:
I’m accepting the incoming location JSON data from the MenuBar application, throw it into a Redis List to be retrieved later, and then serve up a 200. Simple, amirite?
Fetching my Location
My location is now stored in Redis, but how do I get it out? Well, the web service that can write the data can obviously read the data. This is where my
/last_location endpoint comes in. Here it is:
Here I’m pulling the last location JSON blob (surprise, surprise) that was inserted into the Redis List, setting some headers, and then responding with that JSON. This is pretty straightforward and now I can pull the info for my last location for my website like so:
And Viola, I’ve got myself some auto-updating ‘Current Location’ text on my website!
What’s great about the above is that it has some cool potential. With using the Redis List type instead of a single key that gets overwritten, I have a running tally of where I’ve been. Let’s say tomorrow I want to show a Google Map view on my website with markers for each City I visited? I can pull the full Redis list, dedupe locations by filtering down on a particular Lat/Lon diameter, and then create a marker for each unique location. Boom, bam, pow I’ve got myself a map of where I’ve been. Sweet, right?
This project was fun to build and writing all of this down has given me some ideas… so expect to see more from this project soon!
Have an improvement or noticed something I missed? Let me know in the comments or ping me at firstname.lastname@example.org!