Erlang 服务器接口的工作示例

发布于 2024-10-04 12:24:59 字数 609 浏览 12 评论 0原文

我正在尝试使 Erlang 的 Inets mode_esi 工作并运行一些函数。到目前为止,我还没有成功。有人可以发布一个关于如何运行 Inets 的最小示例,以便像 http:localhost:8099/esi/my_mod:foo 这样的 url 将调用方法 my_mod:foo/3

另外:我已经使用这些命令启动了 inets:

inets:start(),
inets:start(httpd, [{port, 8099}, {server_name, "localhost"}, {document_root, "."}, 
  {server_root, "."}, {erl_script_alias, {"/esi", [my_esi, io]}}]).

该模块是:

-module(my_esi).
-export([foo/3]).
foo(Sid, Env, In) -> mod_esi:deliver(Sid, ["bla"]).

但是浏览到 http://localhost:8099/esi/my_esi:foo 会导致错误消息。

I am trying to make Erlang's Inets mode_esi work and run some function. So, far, I did not succeed. Can someone post a minimal example of how to run Inets so that a url like http:localhost:8099/esi/my_mod:foo will invoke the method my_mod:foo/3?

Addition: I have started inets with these commands:

inets:start(),
inets:start(httpd, [{port, 8099}, {server_name, "localhost"}, {document_root, "."}, 
  {server_root, "."}, {erl_script_alias, {"/esi", [my_esi, io]}}]).

The module is:

-module(my_esi).
-export([foo/3]).
foo(Sid, Env, In) -> mod_esi:deliver(Sid, ["bla"]).

But browsing to http://localhost:8099/esi/my_esi:foo result in an error message.

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

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

发布评论

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

评论(3

聆听风音 2024-10-11 12:24:59

为了澄清已经说过的内容,我将给出一个工作示例(从头开始)。这是为了帮助那些从 google 来到这里的人,主要是因为我就是这样找到它的;)

首先,创建模块:

# filename: my_esi.erl
-module(my_esi).
-export([foo/3,bar/3]).
foo(Sid, Env, In) -> mod_esi:deliver(Sid, ["foo"]).
bar(Sid, Env, In) -> mod_esi:deliver(Sid, ["bar"]).

保存它,然后编译它:

erlc my_esi.erl

您现在将拥有 my_esi.beam

运行 shell

erl

然后从 bash在 shell 内 输入以下内容:

inets:start().
{Httpd_State,Httpd_Pid} = inets:start(httpd, [{port, 8099}, {server_name, "localhost"}, {document_root, "."}, {modules,[mod_esi]},{server_root, "."}, {erl_script_alias, {"/esi", [my_esi, io]}}]).

这里重要的部分是:

{modules,[mod_esi]}

如果您需要其他模块,请通过将它们附加到列表来加载它们。

为了澄清如何/为什么:

我首先承认它一开始确实看起来有点奇怪,而且文档没有我希望的那么有帮助。然而,我意识到:

modules()       -> [atom()]

变成:

{modules, [mod_access, mod_esi, ..., etc.]}

希望它能帮助别人:)

To clarify what has already been said, I'll give a working example (from the ground up). This is to help those who came here from google, mostly because it's how I found it ;)

First, create the module:

# filename: my_esi.erl
-module(my_esi).
-export([foo/3,bar/3]).
foo(Sid, Env, In) -> mod_esi:deliver(Sid, ["foo"]).
bar(Sid, Env, In) -> mod_esi:deliver(Sid, ["bar"]).

Save it, then compile it:

erlc my_esi.erl

You'll now have my_esi.beam

Then run the shell from bash

erl

Within the shell type the following:

inets:start().
{Httpd_State,Httpd_Pid} = inets:start(httpd, [{port, 8099}, {server_name, "localhost"}, {document_root, "."}, {modules,[mod_esi]},{server_root, "."}, {erl_script_alias, {"/esi", [my_esi, io]}}]).

The important part here is:

{modules,[mod_esi]}

If you need other modules, load them by appending them to the list.

To clarify how/why:

I will first admit that it does seems a bit strange at first, and that the documentation was less helpful than I had hoped. However, I realised that:

modules()       -> [atom()]

becomes:

{modules, [mod_access, mod_esi, ..., etc.]}

Hope it helps someone :)

初心未许 2024-10-11 12:24:59

我自己已经解决了。由于某种原因,超出了我的理解,当我使用配置文件(inets/examples/server_root/conf/8080.conf 的修改版本)调用 Inets 时,完全相同的代码起作用了。我认为示例文件包含 Inets 所需的属性列表,比我在 inets:start(httpd, [...]) 中列出的属性更完整。如果将来有人遇到 Inets 问题,我的建议是从示例配置开始。

I've solved it myself. For some reason, beyond my understanding, the exact same code worked when I invoked Inets with a configuration file (a modified version of inets/examples/server_root/conf/8080.conf). I suppose that the example file includes a more complete list of properties that Inets required than what I listed in inets:start(httpd, [...]). If anyone encounters Inets problem in the future, my advice is to start with the example configuration.

月野兔 2024-10-11 12:24:59

好的,这里是:

  1. inets Web 服务器设置 {erl_script_alias, {"/esi", [my_mod]}},以便模块被接受。
  2. 写入my_mod。像这样的东西:

    <前><代码>-模块(my_mod)。
    -导出([foo/3])。

    foo(SessID,_Env,_Input)->
    mod_esi:deliver(SessID, ["数据", <<"as">>, "一个iolist"])。

  3. 确保 mod_esi 是名为 module 的 inets 部分的一部分:{modules, [.., mod_esi, ..}}
  4. 这一步非常重要:呼吸3次,背诵圣言“我祈祷你,inets,这次你会工作”。交叉手指。
  5. 喝一杯咖啡。茶不行。一定是咖啡。
  6. 享受您的网络服务器。

Ok, here goes:

  1. set {erl_script_alias, {"/esi", [my_mod]}} for the inets web server, so the module is accepted.
  2. Write my_mod. Something like:

    -module(my_mod).
    -export([foo/3]).
    
    
    foo(SessID, _Env, _Input) ->
      mod_esi:deliver(SessID, ["Data ", <<"as">>, " an iolist"]).
    
  3. Make sure mod_esi is part of inets section called module: {modules, [.., mod_esi, ..}}
  4. This step is very important: Breathe 3 times, recite the holy words "I pray to thee, inets, that you will work this time". Cross your fingers.
  5. Drink a cup of coffee. Tea will not do. It has to be coffee.
  6. Enjoy your web server.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文