Simple HTTP/HTTPS server on Scala/Akka

Let me present a simple Scala script to serve local files over HTTP or HTTPS. I've used Scala/Akka to get the good performance and to play around with the programming language I really like.

Toolchain


We need Scala, SBT (Scala interactive build tool), Conscript (a distribution mechanism for Scala apps) and Scalas (the script runner for Scala). Conscript is needed only to install Scalas here, so you may want to install the runner manually (see the Scalas link above) if you don't want to install Conscript.

For Mac OS X we're going to use Homebrew to install Scala and SBT:

 $ brew install scala sbt

Next step is Conscript (please follow the installation instructions on the official site):

 $ wget https://dl.bintray.com/foundweekends/maven-releases/org/foundweekends/conscript/conscript_2.11/0.5.1/conscript_2.11-0.5.1-proguard.jar
 $ java -jar conscript_2.11-0.5.1-proguard.jar

Next step is installing Scalas (please follow the installation instructions on the official site):

 $ cs sbt/sbt --branch 0.13.13

It may show some errors, please dismiss those.

HTTPS self-signed certificate


Java has the handy tool to generate the certificate, just call it with the required parameters (in the example it creates a RSA certificate valid for 365 days):

 $ keytool -genkey -keyalg RSA -validity 365 -keystore /path/to/file -storetype PKCS12

where /path/to/file is the keystore file you can use with the HTTPS server.

Please note that the generated certificate is not trusted by browsers. If you have a legit public website, you can generate a certificate for free using Let's Encrypt service.

The script


Please note that SBT will download the dependencies, therefore the first start may take a while. After that the using of the script is straightforward (please use "--help" command if you're lost).

Sample output:

 $ /serve.scala --keystore=server.p12 --password=89WSTfO --port=9000

[INFO] [12/07/2016 22:02:59.682] [run-main-0] [http(akka://sys)] scala version 2.12.0
[INFO] [12/07/2016 22:02:59.891] [run-main-0] [http(akka://sys)] start: https://192.168.1.26:9000/
[INFO] [12/07/2016 22:03:33.832] [sys-akka.actor.default-dispatcher-9] [http(akka://sys)] GET /
[INFO] [12/07/2016 22:03:34.121] [sys-akka.actor.default-dispatcher-2] [http(akka://sys)] GET /favicon.ico

The code:



Comments

Popular posts from this blog

Web application framework comparison by memory consumption

Trac Ticket Workflow

Shellcode detection using libemu