Hyperscala: Getting Started

Matt Hicks continues his blog series about Hyperscala with a very interesting and simple article about how to create your first pages with Hyperscala.

Last week I did an introduction to Hyperscala and briefly outlined some really cool things it can do. This week I want to slow down a bit and take you through the basics of getting your first application up and running with Hyperscala.

Requirements: Since there is a broad number of IDEs / editors used for working in Scala I won’t make any presumptions in this tutorial regarding an IDE. However, I will quickly say that I use IntelliJ and absolutely love it for Scala development. That being said, the only requirements necessary to begin this tutorial is SBT and your editor of choice.

First we need to create our directory structure:

      hello-hyperscala/

        src/

          main/

            scala/

              hello/

                HelloPage.scala

 

              HelloSite.scala

      build.sbt

 

Now lets create our build.sbt file in the hello-hyperscala folder with the following contents:

1
2
3
4
5
6
7
8
9
name := "hello-hyperscala"
version := "1.0"
scalaVersion := "2.9.2"
resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"
libraryDependencies += "org.hyperscala" %% "hyperscala-web" % "0.6"

This is a fairly simplistic build.sbt file with just a few specifics to note. First, we need the Typesafe Repository because one of Hyperscala’s dependencies uses Akka Actors. The external repo will not be required once we upgrade to 2.10 but because of some known bugs in 2.10 that undertaking is currently on hold. The second thing to notice is the hyperscala-web dependency. The web sub-project of Hyperscala provides the functionality to create a Website and Webpages. There are several sub-projects in Hyperscala (core, html, javascript, svg, web, ui, examples, and site) but web is the highest level we need and depends on the rest of the functionality we want to use right now.

Next we need to create our actual webpage:

1
2
3
4
5
6
7
8
9
10
package hello
import org.hyperscala.html._
import org.hyperscala.web.site.Webpage
class HelloPage extends Webpage {
  title := "Hello World Page"
  body.contents += "Hello World!"
}

You’ll name this HelloPage.scala and put in the hello directory as reflected in the structure above. It’s fairly simple what we’re doing here. We’re extending from Webpage that defines the basic HTML structure (html, head, body) and we can then add content on to it. We set the page title and add “Hello World!” to the body of the page.

Next we need to create a Website. The website is responsible for routing URLs to pages, managing access to the session, and much more. So here’s our very basic website:

1
2
3
4
5
6
7
8
9
10
11
package hello
import org.hyperscala.web._
import org.hyperscala.web.site._
import org.hyperscala.web.session._
object HelloSite extends Website[MapSession] {
  val page = WebpageResource("/hello.html", new HelloPage, Scope.Request)
  protected def createSession() = new MapSession
}

You’ll name this HelloSite.scala and put it in the hello directory with HelloPage.scala. We simply define the val page that represents the URI /hello.html to point to a new HelloPage. The second argument is a function, so every request creates a new instance of HelloPage. We talked about Scope in the previous article but for the purposes of our do-nothing page we just have it create the page, render it, and then die when a request comes in. By default a WebpageResource will automatically add itself to the Website so there’s no need to explicitly add it. Lastly, the createSession is responsible for creating a new Session that is used throughout the website across all pages.

Now that we’ve got all of our code written we simply need to run it. Issue the following command:

1
sbt run

You should end up with some output like the following:

1
2
3
[info] Set current project to hello-hyperscala (in build file:/home/mhicks/projects/hello-hyperscala/)
[info] Running hello.HelloSite
2013.01.15 14:16:53.449 [run-main] INFO  hello.HelloSite$ - HelloSite$ bound to null:8080

Notice the “bound to null:8080″ signifies that it is wildcard bound to all IP addresses and on the port of 8080. Now just open up your browser and hit http://localhost:8080 and you should be greeted with “Hello World!”

Re-published article by MattHicks.com.
Original article can be found herehttp://www.matthicks.com/2013/01/hyperscala-introduction.html

Tagged with: , ,
Posted in Scala
Pinterest
WP Socializer Aakash Web