Erlang 有 Sinatra 风格的 Web 框架吗?

发布于 2024-08-17 20:03:48 字数 136 浏览 6 评论 0原文

我用 Ruby 和 Rails 编程了很长一段时间,然后我爱上了 Sinatra 框架的简单性,它允许我构建一页 Web 应用程序。

Erlang 有像 Sinatra 这样的 Web 框架吗?我尝试了 Erlyweb,但它似乎太重量级了。

I programmed in Ruby and Rails for quite a long time, and then I fell in love with the simplicity of the Sinatra framework which allowed me to build one page web applications.

Is there a web framework like Sinatra available for Erlang? I tried Erlyweb but it seems far too heavyweight.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

淑女气质 2024-08-24 20:03:48

您可以使用 mochiweb 实现最小的目标:

start() ->
  mochiweb_http:start([{'ip', "127.0.0.1"}, {port, 6500},
                       {'loop', fun ?MODULE:loop/1}]).
                           % mochiweb will call loop function for each request

loop(Req) ->
  RawPath = Req:get(raw_path),
  {Path, _, _} = mochiweb_util:urlsplit_path(RawPath),   % get request path

  case Path of                                           % respond based on path
    "/"  -> respond(Req, <<"<p>Hello World!</p>">>);
    "/a" -> respond(Req, <<"<p>Page a</p>">>);
    ...
    _    -> respond(Req, <<"<p>Page not found!</p>">>)
  end.

respond(Req, Content) ->
  Req:respond({200, [{<<"Content-Type">>, <<"text/html">>}], Content}).

如果您需要高级路由,则必须使用正则表达式而不是简单的 case 语句。

You could achieve something minimal with mochiweb:

start() ->
  mochiweb_http:start([{'ip', "127.0.0.1"}, {port, 6500},
                       {'loop', fun ?MODULE:loop/1}]).
                           % mochiweb will call loop function for each request

loop(Req) ->
  RawPath = Req:get(raw_path),
  {Path, _, _} = mochiweb_util:urlsplit_path(RawPath),   % get request path

  case Path of                                           % respond based on path
    "/"  -> respond(Req, <<"<p>Hello World!</p>">>);
    "/a" -> respond(Req, <<"<p>Page a</p>">>);
    ...
    _    -> respond(Req, <<"<p>Page not found!</p>">>)
  end.

respond(Req, Content) ->
  Req:respond({200, [{<<"Content-Type">>, <<"text/html">>}], Content}).

If you need advanced routing, you will have to use regex's instead of a simple case statement.

倾城花音 2024-08-24 20:03:48

看看 webmachine。它有一个非常简单但功能强大的调度机制。您只需编写一个资源模块,将您的 URI 指向它,您的服务就会自动兼容 HTTP。

Have a look at webmachine. It has a very simple but powerful dispatch mechanism. You simply have to write a resource module, point your URIs to it and your service is automatically HTTP compliant.

樱花细雨 2024-08-24 20:03:48

您可能想看看 Axiom (声明:这是我自己的项目)。它很大程度上受到 Sinatra 的启发,构建在 Cowboy 之上,并提供了许多 Sinatra 的功能。

一个简单的示例:

-module(my_app).
-export([start/0, handle/3]).

start() ->
    axiom:start(?MODULE).

handle('GET', [<<"hi">>], _Request) ->
    <<"Hello world!">>.

它处理 GET /hi 并返回 Hello World!

查看自述文件以获取其功能的文档。

You may want to take a look at Axiom (disclosure: it's my own project). It is largely inspired by Sinatra, built on top of Cowboy and offers a lot of the features, Sinatra does.

A simple example:

-module(my_app).
-export([start/0, handle/3]).

start() ->
    axiom:start(?MODULE).

handle('GET', [<<"hi">>], _Request) ->
    <<"Hello world!">>.

This handles GET /hi and returns Hello World!.

Take a look at the README for a documentation of it's features.

海风掠过北极光 2024-08-24 20:03:48

您可能对 Rusty Klophaus 的 nitrogen 框架感兴趣。它非常轻量级,非常适合真正动态的单页网站。

You might be interested in Rusty Klophaus' nitrogen framework. It's really lightweight and is ideal for really dynamic single page sites.

天涯离梦残月幽梦 2024-08-24 20:03:48

可能是这个使用misultin的示例(请参阅REST SUPPORT),看起来像sinatra:

May be this example (see REST SUPPORT) using misultin, looks like sinatra :

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文