在Scala中引导Web服务器

Mat*_*hew 9 python scala

使用Python可以实现以下功能:

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

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()
$ python hello.py
Run Code Online (Sandbox Code Playgroud)

用于运行Web服务器的4个命令和7行代码确实非常令人印象深刻.

什么是Scala等价物?

小智 11

这使用了JDK6中内置的HttpServer类.随意提出改进建议,我是Scala的新手.

package org.test.simplehttpserver

import java.net.InetSocketAddress
import com.sun.net.httpserver.{HttpExchange, HttpHandler, HttpServer}
import collection.mutable.HashMap

abstract class SimpleHttpServerBase(val socketAddress: String = "127.0.0.1",
                                    val port: Int = 8080,
                                    val backlog: Int = 0) extends HttpHandler {
  private val address = new InetSocketAddress(socketAddress, port)
  private val server = HttpServer.create(address, backlog)
  server.createContext("/", this)

  def redirect(url: String) =
    <html>
      <head>
          <meta http-equiv="Refresh" content={"0," + url}/>
      </head>
      <body>
        You are being redirected to:
        <a href={url}>
          {url}
        </a>
      </body>
    </html>

  def respond(exchange: HttpExchange, code: Int = 200, body: String = "") {
    val bytes = body.getBytes
    exchange.sendResponseHeaders(code, bytes.size)
    exchange.getResponseBody.write(bytes)
    exchange.getResponseBody.write("\r\n\r\n".getBytes)
    exchange.getResponseBody.close()
    exchange.close()
  }

  def start() = server.start()

  def stop(delay: Int = 1) = server.stop(delay)
}

abstract class SimpleHttpServer extends SimpleHttpServerBase {
  private val mappings = new HashMap[String, () => Any]

  def get(path: String)(action: => Any) = mappings += path -> (() => action)

  def handle(exchange: HttpExchange) = mappings.get(exchange.getRequestURI.getPath) match {
    case None => respond(exchange, 404)
    case Some(action) => try {
      respond(exchange, 200, action().toString)
    } catch {
      case ex: Exception => respond(exchange, 500, ex.toString)
    }
  }
}

class HelloApp extends SimpleHttpServer {
  var count = 0

  get("/") {
    "There's nothing here"
  }

  get("/hello") {
    "Hello, world!"
  }

  get("/markup") {
    <html>
      <head>
        <title>Test Title</title>
      </head>
      <body>
        Test Body
      </body>
    </html>
  }

  def countPage = <html>
    <head>
      <title>Test Title</title>
    </head>
    <body>
      Count:
      {count}<a href="/increaseCount">++</a>
      <a href="/decreaseCount">--</a>
      <a href="/resetCount">Reset</a>
    </body>
  </html>

  get("/count") {
    countPage
  }

  get("/resetCount") {
    count = 0
    redirect("/count")
  }

  get("/increaseCount") {
    count = count + 1
    redirect("/count")
  }

  get("/decreaseCount") {
    count = count - 1
    redirect("/count")
  }

  get("/error") {
    throw new RuntimeException("Bad bad error occurred")
  }
}

object Main {

  def main(args: Array[String]) {
    val server = new HelloApp()
    server.start()
  }
}
Run Code Online (Sandbox Code Playgroud)


Dav*_*low 9

您可能会发现Unfiltered值得一看.


Dan*_*ral 9

我知道Max alread 提到了它,但我无法抗拒指出Scalatra的 6行hello world:

import org.scalatra._

class ScalatraExample extends ScalatraServlet {
  get("/") {
    <h1>Hello, world!</h1>
  }
}
Run Code Online (Sandbox Code Playgroud)

无论如何,看看可用的Scala Web框架.

编辑

有一些关于如何轻松准备工具的讨论,尤其是关于Lift的问题.所以,这是一个关于Ubuntu的会议.我的大部分时间花在试图弄清楚Sun的Java在包管理器中的位置.无论如何,一旦安装了Java,这就是它的方式,所有消息都被省略了,所以我可以看到我实际需要输入的内容:

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
Run Code Online (Sandbox Code Playgroud)

那里,web服务器正在运行.当然,你必须事先了解SBT和Lifty,甚至知道你用它们来运行Scala Lift程序,但是,另一方面,我从未听说过Flask,所以我肯定会花钱更多的时间试图弄清楚如何使用Python获取Web服务器应用程序而不是获取Lift一个.

我也没有在第一次尝试时做到正确 - 我尝试使用Scala 2.8.1(上面使用默认的2.7.7版本,虽然2.8.0也可以使用),但却发现没有Lift版本目前可用于该版本的Scala.另一方面,我已经安装了lifty,并且只是为了显示安装它的命令而卸载它.

我希望SBT有一个Debian/Ubuntu软件包 - 毕竟它只是一个小的shell脚本和一个jar文件,它负责下载Scala,Lift等,以及你需要的任何版本.

它是一个与Python和Ruby不同的模型,其中语言带有一个处理大多数事物的包管理器.


Max*_* A. 3

那么,Scalatra的目标是在功能和易用性方面与 Ruby 的 Sinatra 类似。