Friday, July 17, 2015

Spring Boot

I recently did a presentation on Spring Boot - what it is and how to use it. And for my presentation, I made a simple REST server sample project[1]. The project consists of a few REST endpoints, a couple of data entity classes, and an in-memory H2 database.

Feel free to check out the documentation[2] if you're interested in setting up a very simple REST server with Spring Boot.


  • [1] - https://github.com/DWiechert/spring-boot
  • [2] - https://github.com/DWiechert/spring-boot/blob/master/README.md

Thursday, May 28, 2015

Tv Show Tracker - On Heroku

I recently decided that I should start testing out my tv show tracker website out in "the real web" instead of just local development. So to help me along I chose to use Heroku, which is a PaaS - platform as a service. The advantage of using a PaaS is that they manage your actual box, upgrades, security, etc. for you. All you have to do is build and deploy your site according to their guidelines.

After going through their documentation of how to deploy a Scala application, I have successfully deployed tv-show-tracker on Heroku. However, this was not as straight-forward as the documentation made it out to be - especially when I'm using spray instead of just a quick HTTP server like their example uses. So I thought I'd go through some of the things I had to do to get my site up and running on their platform.

First, lucky for me, I already had my site fully utilizing sbt so I had most of the config and dependencies working as they want you to. However, I did have to add an sbt plugin:
 addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "0.8.0")  
After that was added, I had to add the following to my build.sbt file:
 import NativePackagerKeys._  
 packageArchetype.java_application  
Heroku utilizes this native packager and foreman to run Scala applications which is not something I was doing when I would run it locally.

Secondly, I had issues connecting to my PostgreSQL database. They went through some of the steps, however I felt some things were lacking. In the end, my database connection looks like:
 val dbUri = new URI(System.getenv("DATABASE_URL"))  
 val username = dbUri.getUserInfo.split(":")(0)  
 val password = dbUri.getUserInfo.split(":")(1)  
 val db = Database.forURL(s"jdbc:postgresql://${dbUri.getHost}:${dbUri.getPort}${dbUri.getPath}", driver = "org.postgresql.Driver", user = username, password = password)  
This connection setup uses slick, where their example just had a regular database connection.

Next, I had issues seeing the data in my database. I was lucky enough to come across this stack overflow question which helped me solve my problem - look at my data on my local install of pgadmin. Now, the accepted answer is what I used to get it working, however not all steps are properly documented in that answer.

To get all of the information needed to fill out the pgadmin server, you first need to run:
 heroku config  
If you have the database add-on installed, it will spit out a database url that we need. We just need to know how to decipher this long url and break it into the individual parts:
 DATABASE_URL: postgres://user:password@server:port/database  
Once we know how the url is broken down (example above), you just plug each part into pgadmin (like the SO answer says) and you can see your data on your local install of pgadmin.

Lastly, I had issues with my site properly binding to the host and port and being able to hit the url from my computer.

For the port, I tried putting in 8080 and 5000 (from the example) but neither seemed to work. I eventually found this documentation which explains that I need to get the port from a system variable - PORT.

I also had issues with the hostname I was supposed to connect to. I tried "localhost" and my site's url but neither of those worked. Then I came across this SO answer which showed I needed to use "0.0.0.0". This allows the app to bind to the localhost and accept incoming traffic from my sites Heroku url.
So, in the end my connection looks like:
 val myPort = Properties.envOrElse("PORT", "8080").toInt // for Heroku compatibility  
 IO(Http) ? Http.Bind(service, interface = "0.0.0.0", port = myPort)  
This allows my app to get the proper port from Heroku or use a default of 8080 if I'm running locally.

All in all, this turned out pretty well seeing that I was able to go from my local setup to running on Heroku in just a few hours. There were a few hiccups along the way, but it seems like other people have ran into the issues I experienced before and I was able to use the answers given to them.

Saturday, May 16, 2015

Tv Show Tracker - Basic Pages

As I noted on my previous post, I am currently working on a site to track the tv shows I watch. I have been working quite a bit, have a few main pages working, and thought I should write an update.

URLs

  • /
  • /index 
  • /shows
  • /searchshow
  • /show
    • Request parameters:
      • showName
  • /searchseason
  • /season
    • Request parameters:
      • showName
      • number
  • /notfound
/
This is the base url. It just re-directs to /index.

/index
The main landing page of the site. On this page the user can choose to list all shows, search a specific show, or search a specific season of a show

/shows
Lists all shows recorded in the database. Each show is a link to the individual show's page which displays extra information about the show itself.

/searchshow
This page allows the user to search for a specific show. If a show is found, the user is directed the show's page. If the show is not found, a not found page is displayed.

/show
Displays information about a specific show. The show name is passed in via a request parameter.

/searchseason
This page allows the user to search for a specific season in a show. If the season is found, the user is directed to the season's page. If the season is not found, a not found page is displayed.

/season
Displays information about a specific season of a shot. The show name and number are passed in via request parameters.

/notfound
Indicates to the user that the information requested was not found.

Next Steps

  • Episodes. These need to be added to individual seasons so that they can be tracked as watched.
  • Watched indicator. There needs to be an indicator on each episode, season, and show to let the user know if they've watched it already.
  • Currently watching. There needs to be a way for the user to indicate which show they are currently watching. This way they can easily check when they've watched a particular episode and see which episode is next to watch.
  • Data. The site needs to add the ability to insert episodes, season, and shows. Currently the few shows and seasons are hard-coded. The user needs to be able to add an episode, season, and show from the site. If it is already in the database, the site will indicate that to the user.

Monday, April 27, 2015

Tv Show Tracker

I watch a lot of tv shows and keeping track of which shows/seasons/episodes I have watched can be a daunting task if I don't stay on top of it. At any one time I am watching more than 1 show, but sometimes as much as 5 different shows in a given week. To help myself keep track of these things, I have been using a Google Doc Excel sheet which tracks my shows, seasons, and episodes I have been watching.

This is obviously not ideal because I have to do a lot of manual work and it's very archaic. So, for this reason, I have started tv-show-tracker. The idea behind this project is to make a more robust solution than all of my manual labor to keep my items in order. Right now this is in the very early stages, however the end goal could help me out quite a bit. I would eventually like to be able to:

  • Enter a show I would like to watch
  • Have it import all of the seasons and episodes
  • Keep track of which shows I'm currently watching and what episode I'm on
  • Allow multiple users to use this based on a login
Seeing that this site is an "easier" project (database layer, a couple of endpoints to hit, and a presentation layer) I decided to use a full Scala stack. Not only will this help me learn Scala better, but also get used to the open source projects Scala has because I'm choosing to use true Scala projects - not Java ones. The projects I've chosen to use on this site are:

As noted above, this is in very early stages of development, however that doesn't mean I am not making good progress. So far I have successfully wired up all of these projects and have the proof of concept working:
  • URL endpoints with request parameters - e.g."Shows"
  • Parse the request parameters and search the database
  • Return the "Show" and its "Seasons"
  • Render the results to the user
There's obviously a lot of work still to go, but now that I have a proof of concept working the rest should start to fall into place quicker - I just need to nail down the database schema and then work up the stack.

Friday, April 24, 2015

Scala Shell

I've recently been working on a new side project - scala-shell. As one might guess, this is a shell written in Scala. The idea behind this side project is to help further my Scala knowledge and dive into some more advanced topics such as:

  • Creating a GUI
  • Files and directories
  • Parsing user input
  • Processes
  • Packaging using sbt
I haven't had a lot of time to work on scala-shell, however I currently have the following items completed:
  • Creating a GUI
  • Packaging using sbt
  • Parsing user input
  • A few basic commands
    • exit
    • pwd
    • ls
  • Printing an error on invalid input

I realize that there's still a long ways to go to make this a "usable" shell, however I feel that it's coming along quite nicely and am happy with my current progress.

Friday, February 6, 2015

Pi-Temperature

Raspberry Pi's are a great little tool for learning. Software people can learn a little hardware and hardware people can learn a little software. I recently got a Raspberry Pi B+ kit and have been messing around with it lately to learn a little hardware.

My goal was to attach temperature sensors and have the Pi report back the temperature to me.

So, I went out and bought the Vktech DS18b20 sensors and attached them to a GPIO port. Once, the sensors have been connected, you need to login and run the following 2 commands to have Linux setup and probe them:

 sudo modprobe w1-gpio  
 sudo modprobe w1-therm  

Now, once those commands are ran, the temperatures will show up in following files:

 /sys/bus/w1/devices/w1_bus_master1/<serial id>/w1_slave  

where <serial id> is the serial id of the sensor itself. An example of one of these files is:

 4d 01 4b 46 7f ff 03 10 d8 : crc=d8 YES  
 4d 01 4b 46 7f ff 03 10 d8 t=20812  

Now, in that file, the Celsius temperature is actually the last little bit, 20812 - it just needs to be divided by 1000 and you have your Celsius temperature of 20.8.

Great, it's now working. Linux is probing the sensor and reporting it to a file which I can read and see. It works, but not that practical. At this point I wanted to see if I could automate this process so I didn't have to manually read the file each time I want to know the temperature.

Meet pi-temperature, a Java library I wrote to solve this exact problem. It probes all attached sensors for me at a given interval (defaults to 1 min) and can report the temperatures of each sensor back to the user. It utilizes spring boot to startup a local Tomcat instance for a web server and has several REST urls attached:

 /sensors/list  
 /alerts/list  
 /alerts/setOn/<name>  
 /alerts/setOff/<name>  
 /alerts/update/<name>  

The /sensors/list is the primary endpoint which reads all of the sensors and reports back the temperatures in both Celsius and Fahrenheit. The /alerts endpoints are currently a work in progress and another post will be coming soon with their features.

Specific instructions on how to build, run, and more thorough instructions can be found on the pi-temperature project's README.

Saturday, October 11, 2014

Scala Simple Build Tool

So, recently as I was working through my scala book, we got to the first chapter where we used 3rd party dependencies - the testing chapter. Now, for the simple examples in the chapter I just downloaded the jars needed and put them on my classpath in Eclipse and got everything working just fine. However, I was interested in looking for a dependency manager for scala and that's when I stumbled up the scala sbt project. With this finding, I decided to mimic the tests done in the testing chapter, but this time use scala sbt to manage my dependencies and put them on the classpath for me (I've pushed this small project to my github account).

Setting Up First Project In Eclipse


After I installed sbt, geting my first project up and running was not trivial so I wanted to walk through the steps I did to get a project from initial commit in github to fully working.
Note: I copied a lot of steps from a SO answer, however it was not exactly the same so I wanted to type up the steps I did.
  • Create your new repo on Github and clone it somewhere locally
  • Add the sbt eclipse plugin to your list of global sbt plugins
    • Now this step was a little confusing for me because the instructions were for *NIX style setups. They say to just add the line:
    •  addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.5.0")  
      To the file:
    •  ~/.sbt/0.13/plugins/plugins.sbt  
      
    • However, I'm working on Windows so obviously this path doesn't exist. Though, eventually I figured it out and needed to add it to the local user's file. So, in my case the file was located at:
    •  C:\Users\Dan\.sbt\0.13\plugins\plugins.sbt  
      
  • Once you add this plugin, open a command prompt to where you cloned your repo and type the following:
  •  sbt eclipse  
    
    • This will generate all of the eclipse project, classpath, and other various files needed to work properly in Eclipse
  • Import the project into Eclipse and start coding:
    • File --> Import --> General --> Existing Projects into Workspace
And that's it, now you can start working on your scala project inside of eclipse and get all of the helpfulness from the scala Eclipse plugin.

build.sbt


Once you have your project initialized and ready to go in Eclipse, it's time to make our build.sbt file. This file serves the same purpose as the file "pom.xml" for maven projects - it defines your jar name, version, dependencies, etc etc. Now, my build.sbt is very simple, however there a lot of different things you can do in this file and they are explained on the sbt site.

Adding Dependencies


Now, adding dependencies to your project is clearly defined on the sbt site, however there was one issue I ran into that I wanted to write down. By default, Eclipse will not reload the dependencies when you add them to your build.sbt file. Instead, you need to reload the Eclipse definition with the sbt plugin we added earlier. This is done by just typing a couple of commands into a command prompt and refreshing the Eclipse project - the steps to do this are listed on this SO answer.

Change SBT Download Location


By default, sbt will download jars to folder in the user's directory, in my case it was:
 C:\Users\Dan\.ivy2\cache  
However, I did not want my downloaded jars to reside there because that meant each user would have their own copies of all of the scala jars used. So, I found yet another SO answer that showed me how to change the download location. I simply had to edit my sbtconfig.txt file, which was located at - C:\Program Files (x86)\sbt\conf\sbtconfig.txt - and had to add the following 2 lines:
 -Dsbt.ivy.home=S:\SBTRepo  
 -Divy.home=S:\SBTRepo  
So now all jars will download to the folder - S:\SBTRepo - for all users and not to each user's individual local files.

Now, all of these steps might seem like a lot, but it really didn't take that long - there was definitely more investigate than implementation. It's just that this was my first time building a dependency-managed scala project so inevitably took the longest to get properly setup. And once all of these steps were done, my scala project was up and running perfectly in Eclipse and I was able to test everything properly on the command line.