Bootstrapping a web server in Scala

后端 未结 8 1703
走了就别回头了
走了就别回头了 2021-02-01 10:32

The following is possible using Python:

$ apt-get install python
$ easy_install Flask
$ cat > hello.py
from flask import Flask
app = Flask(__name__)

@app.rou         


        
8条回答
  •  走了就别回头了
    2021-02-01 10:54

    I know Max alread mentioned it, but I couldn't resist pointing out Scalatra's 6 lines hello world:

    import org.scalatra._
    
    class ScalatraExample extends ScalatraServlet {
      get("/") {
        

    Hello, world!

    } }

    Anyway, take a look at available Scala web frameworks.

    EDIT

    There's some discussion about how easy is to get the tooling ready, particularly with regards to Lift. So, here's a session on Ubuntu. Most of my time was spent trying to figure out where did Sun's Java go in the package manager. Anyway, once Java was installed, this is how it went, with all messages elided, so one can see what I actually had to type:

    dcs@dcs-desktop:~$ wget -q -O bin/sbt-launch.jar http://simple-build-tool.googlecode.com/files/sbt-launch-0.7.4.jar
    dcs@dcs-desktop:~$ echo 'java -Xmx512M -jar `dirname $0`/sbt-launch.jar "$@"' > bin/sbt
    dcs@dcs-desktop:~$ chmod u+x bin/sbt
    dcs@dcs-desktop:~$ mkdir app
    dcs@dcs-desktop:~$ cd app
    dcs@dcs-desktop:~/app$ sbt
    Project does not exist, create new project? (y/N/s) s
    > *lifty is org.lifty lifty 1.4
    > lifty create project-blank sample 2.1
    > reload
    > update
    > jetty-run
    

    There, web server running. Of course, you have to know about SBT and Lifty beforehand, to even know you'd use them to get a Scala Lift program running, but, on the other hand, I had never heard about Flask, so I'd certainly spend way more time trying to figure out how to get a web server application going in Python than I did getting a Lift one.

    I also did not get it right on the first try -- I tried going for Scala 2.8.1 (the above uses a default 2.7.7 version, though 2.8.0 will work too), only to find out that there's no Lift version available for that version of Scala as yet. On the other hand, I had lifty installed already, and de-installed it just to show the command that installs it.

    I do wish there was a Debian/Ubuntu package for SBT -- it's just a tiny shell script and a jar file, after all, and it takes care of downloading Scala, Lift, etc, and at whatever version you need.

    It is a different model than Python and Ruby, where the language comes with a package manager which handles most things.

提交回复
热议问题