Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala: how to embed a small web server into a scala app?

For a little educational project intended as a community resource to help people learn Scala I am looking for a simple way to serve and process web pages in a background thread.

Minimal background: Scalatron is a multi-player game in which players pit bot programs (written in Scala) against each other. It is accompanied by a step-by-step Scala tutorial. Currently players need to use an IDE on their local machines to compile bots, which are then published into the game by copying them into a shared network directory. This is cumbersome. For the next version it would be nice to offer browser-based bot editing and publishing to maximally simplify the setup for both organizers and players.

I already got a background thread working that will compile Scala source code arriving on the server on the fly (obviating the need for a full IDE). Next I have to run a tiny web server to provide an access point for players and to handle bot uploads (this gets rid of the network share).

The requirements are very basic: initially I envisage serving a single page with an edit box and a "Go" button (= upload to server, compile & publish into game); I expect no more than 20 concurrent users with not more than one bot upload across all users every 5 or 10 seconds; I need to hold minimal state for each users (just a name) and I need to return compiler error messages to the user. Note that game screen updates would NOT be displayed in the browser but on a projector attached to the server machine. And primarily to allow for the simplest possible setup (double click on the game server .jar) I think it would be nice to run the web server in a background thread within the existing game server.

What is the most appropriate way to do this? Should I use some existing framework, like Play or Lift? Is there existing code for doing something very similar to this? Is it even reasonable to plan to run a web server in a background thread like this? Any advice is appreciated.

like image 342
Scalatron Botwar Avatar asked Mar 29 '12 20:03

Scalatron Botwar


2 Answers

Embedded web servers is exactly what Unfiltered was made for. The basic philosophy for Unfiltered is that the web server is just a library you call from your code, instead of your code being something the web framework calls.

like image 143
Daniel C. Sobral Avatar answered Oct 19 '22 10:10

Daniel C. Sobral


Embedding Jetty is fairly easy, being one of the primary goals for the Jetty project. If you needs are simple, this is the quickest route. I suspect you will quickly need a more robust solution (models, routing, templates, etc) so using a framework like Lift or Play Framework would be a better idea.

like image 25
mguymon Avatar answered Oct 19 '22 11:10

mguymon