I published a post last week showing how to use Grails to create a website on the Raspberry Pi. After some feedback and conversations about whether this was "overkill" for a simple Raspberry Pi website I decided to revisit the topic and see how I could simplify things a bit without sacrificing the power of Groovy and Pi4J. I've done a bit of digging and discovered the Spark Java framework.
So what is Spark Java? From the Spark Java site:
Spark Framework is a simple and lightweight Java web framework built for rapid development. Spark was originally inspired by the web framework Sinatra, but it's intention isn't to compete with Sinatra, or other similar web frameworks in different languages. Sparks intention is to provide a pure Java alternative for developers that want to (or are required to), develop their web application in Java. Spark is built around Java 8's lambda philosophy, which makes a typical Spark application a lot less verbose than most application written in other Java web frameworks.
Spark focuses on being as simple and straight-forward as possible, without the need for cumbersome (XML) configuration, to enable very fast web application development in pure Java with minimal effort. It’s a totally different paradigm when compared to the overuse of annotations for accomplishing pretty trivial stuff seen in other web frameworks.
Sounds like a perfect framework for the Pi. Simple, self-contained, lightweight and convention based. No need to install a web server like Tomcat or Apache, no need for a full blown framework like Grails. And since Groovy closures can be used as Lambda expressions there is nothing stopping us from using Groovy instead of Java with Spark Java. So, let's do that.
Create a new Gradle project in IntelliJ. Once you've got your project created, open up your build.gradle
script and paste the following:
We declare three dependencies here:
runServer
task is what we'll use to launch the server via Gradle. Next we need a simple Bootstrap script which is what we'll need to declare our routes within the Spark Java application (basically - one route for each verb, if needed, for each path within the application).src/main/groovy
or Gradle won't find them!Now populate your src/main/groovy/Bootstrap.groovy
class as such:
You're now ready to launch the app. In an SSH
session navigate to where the build.gradle
script is and run gradle runServer
. The first time you run it will take a bit longer than subsequent runs, since the dependencies need to be downloaded. Eventually you'll see the following in your console:
runServer[Thread-1] INFO org.eclipse.jetty.util.log - Logging initialized @1898ms
[Thread-1] INFO spark.webserver.JettySparkServer - == Spark has ignited ...
[Thread-1] INFO spark.webserver.JettySparkServer - >> Listening on 0.0.0.0:4567
[Thread-1] INFO org.eclipse.jetty.server.Server - jetty-9.3.2.v20150730
[Thread-1] INFO org.eclipse.jetty.server.ServerConnector - Started ServerConnector@2ad3bd{HTTP/1.1,[http/1.1]}{0.0.0.0:4567}
[Thread-1] INFO org.eclipse.jetty.server.Server - Started @2319ms
And that's it! Your site is now up and running. Try it out by hitting http://[Raspberry Pi or localhost IP]:4567/hello
and you should see the message 'Hello World' in your browser.
Image by cocoparisienne from Pixabay
Last week I had a bit of free time and decided to see how difficult it would be to write a Texas Hold'em poker simulation in Groovy. My goal wasn't...
I've long been an AWS user, but recently one of my projects at work has expressed some interest in getting our platform running on multiple cloud service...
I've heard plenty about Twilio over the past few years and had always wanted to learn more about their APIs. They have a ton of different products...