在 Scala 中引导 Web 服务器
使用 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
4 个命令和 7 行代码即可运行 Web 服务器,这确实令人印象深刻。
Scala 的等价物是什么?
The following is possible using 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
4 commands and 7 lines of code to get a web server running is very impressive indeed.
What's the Scala equivalent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这使用了 JDK6 中内置的 HttpServer 类。请随意提出改进建议,我是 Scala 新手。
This uses the HttpServer class that is built-in in JDK6. Feel free to suggest improvements, I'm new to Scala.
我知道Max已经提到它,但我忍不住指出Scalatra 的 6行hello world:
无论如何,看看可用的 Scala Web 框架。
编辑
有一些关于准备工具有多容易的讨论,特别是在 Lift 方面。这里有一个关于 Ubuntu 的会议。我的大部分时间都花在试图弄清楚 Sun 的 Java 在包管理器中去了哪里。不管怎样,一旦安装了 Java,事情就是这样的,所有消息都被删除了,所以人们可以看到我实际上必须输入的内容:
There, web server running。当然,你必须事先了解 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 不同的模型,Python 和 Ruby 语言带有一个包管理器来处理大多数事情。
I know Max alread mentioned it, but I couldn't resist pointing out Scalatra's 6 lines 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:
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.
您可能会发现 Unfiltered 值得一看。
You might find Unfiltered worth a look.
嗯,有 Scalatra,它的目标是在功能和易用性方面类似于 Ruby 的 Sinatra。
Well, there's Scalatra, which aims to be analogous to Ruby's Sinatra in terms of functionality and ease of use.
此解决方案使用 JAX-WS 端点:
您可以将其复制到文件 WebServer.scala 并只需键入以下内容即可运行它:
This solution uses a JAX-WS Endpoint:
You can copy it to a file WebServer.scala and run it simply by typing:
您可以使用嵌入式 Jetty 服务器:
如果您的目标是进行 LOC 比较,则可以使用嵌入 Sun JDK 的 HTTP 服务器。另一种解决方案可能是使用 javax.xml.ws.Endpoint 和 Provider API。
You could use an embedded Jetty Server:
In case you target for a LOC comparison, You use the HTTP server embedded with the Sun JDK. Another solution could be to use javax.xml.ws.Endpoint and the Provider API.
Scala 等效项有 6 个命令:
使用 Play、
Scala equivalent is in 6 commands:
Using Play,
正如 David Winslow 提到的,Unfiltered 使用代码片段
信息:Apache Spark 字数统计示例的简单 API 功能使用 Unfiltered 用 Scala 编写。
完整示例位于此处
As David Winslow mentioned, Unfiltered usage code snippet
INFO: Simple API capabilities for Apache Spark word count example written in Scala using Unfiltered.
Complete example is here