yaws - 使用 Yaws 的 REST API 实现

标签 yaws erlang-shell

我正在尝试使用 Yaws 网络服务器构建 REST API,但我无法让 Yaws 将请求分派(dispatch)到我的模块。

-module(rest).
-include_lib("stdlib/include/qlc.hrl").
-include_lib("yaws/include/yaws_api.hrl").
-export([out/1, addAirport/4, handle/2]).
-compile(export_all).

-define(RECORD_TYPE, airport).
-define(RECORD_KEY_FIELD, code).

-record(airport,
        {code, city, country, name }).

start() ->
    application:start(mnesia).

do_this_once() ->
    mnesia:create_table(airport, [{attributes,record_info(fields,airport)},{index, [country]}]),
    mnesia:stop().

out(Arg) ->
    Method = method(Arg) ,
    io:format("~p:~p ~p Request ~n", [?MODULE, ?LINE, Method]),
    handle(Method, Arg).

method(Arg) ->
    Rec = Arg#arg.req,
    Rec#http_request.method.

convert_to_json(Lines) ->
    Data = [{obj, [{airport, Line#?RECORD_TYPE.code},
                   {city, Line#?RECORD_TYPE.city},
                   {country, Line#?RECORD_TYPE.country},
                   {name, Line#?RECORD_TYPE.name}]} || Line <- Lines],
    JsonData = {obj, [{data, Data}]},
    rfc4627:encode(JsonData).

addAirport(Code, City, Country, Name) ->
    NewRec = #?RECORD_TYPE{?RECORD_KEY_FIELD = Code,
                           city = City,
                           country = Country,
                           name = Name},
    io:format("~p:~p Adding Airport ~p~n",
              [?MODULE,?LINE, NewRec]),
    Add = fun() ->
                  mnesia:write(NewRec)
          end,
    {atomic, _Rec} = mnesia:transaction(Add),
    NewRec.

handle('GET', _Arg) ->
    io:format("~n ~p:~p GET Request ~n", [?MODULE, ?LINE]),
    Records = do(qlc:q([X || X <- mnesia:table(?RECORD_TYPE)])),
    Json = convert_to_json( Records),
    io:format("~n ~p:~p GET Request Response ~p ~n", [?MODULE, ?LINE,
                                                      Json]),
    {html, Json};
handle('POST', Arg) ->
    {ok, Json, _} = rfc4627:decode(Arg#arg.clidata),
    io:format("~n~p:~p POST request ~p~n",
              [?MODULE, ?LINE, Json]),
    Airport = rfc4627:get_field(Json, "airport", <<>>),
    City = rfc4627:get_field(Json, "city", <<>>),
    Country = rfc4627:get_field(Json, "country", <<>>),
    Name= rfc4627:get_field(Json, "name", <<>>),
    _Status = addAirport(Airport, City, Country, Name),
    [{status, 201},
     {html, Arg#arg.clidata}];
handle('PUT', Arg) ->
    [IndexValue,_] = string:tokens(Arg#arg.pathinfo),
    {ok, Json, _} = rfc4627:decode(Arg#arg.clidata),
    io:format("~p:~p PUT request ~p ~p~n",
              [?MODULE, ?LINE, IndexValue, Json]),
    Airport = rfc4627:get_field(Json, "airport", <<>>),
    City = rfc4627:get_field(Json, "city", <<>>),
    Country = rfc4627:get_field(Json, "country", <<>>),
    Name= rfc4627:get_field(Json, "name", <<>>),
    NewRec = #?RECORD_TYPE{
                 ?RECORD_KEY_FIELD = Airport,
                 city = City,
                 country = Country,
                 name = Name},
    io:format("~p:~p Renaming ~p",
              [?MODULE, ?LINE, NewRec]),
    ChangeName = fun() ->
                         mnesia:delete(
                           {?RECORD_KEY_FIELD, IndexValue}),
                         mnesia:write(NewRec)
                 end,
    {atomic, _Rec} = mnesia:transaction(ChangeName),
    [{status, 200},
     {html, IndexValue}];

handle('DELETE', Arg) ->
    [IndexValue, _ ] = string:tokens(Arg#arg.pathinfo),
    io:format("~p:~p DELETE request ~p",
              [?MODULE, ?LINE, IndexValue]),
    Delete = fun() ->
                     mnesia:delete(
                       {?RECORD_KEY_FIELD, IndexValue})
             end,
    Resp = mnesia:transaction(Delete),
    case Resp of
        {atomic, ok} ->
            [{status, 204}];
        {_, Error} ->
            io:format("~p:~p Error ~p ",
                      [?MODULE, ?LINE, Error]),
            [{status, 400},
             {html, Error}]
    end;
handle(Method,_) ->
    [{error, "Unknown method " ++ Method},
     {status, 405},
     {header, "Allow: GET, HEAD, POST, PUT, DELETE"}
    ].

do(Q)->
    F = fun() ->
                qlc:e(Q)
        end,
    {atomic, Value} = mnesia:transaction(F),
    Value.

最佳答案

您需要在 yaws.conf 文件的服务器配置部分将您的模块注册为 Yaws appmod。请参阅Yaws appmod documentation获取必要的详细信息。

关于yaws - 使用 Yaws 的 REST API 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23290224/

相关文章:

windows - Windows 中的 Erlang 和 YAWS 设置

ssl - 雅司 SSL : Decode Error

erlang - 雅司病路线

erlang - 在 erlang 中采用 gen_server 架构有困难

concurrency - 文件服务器 erlang 响应

erlang - 我可以在另一个应用程序或模块中使用现有的 OTP 应用程序吗?

Erlang 在我的一个安装上映射语法错误

erlang - 如何更改 Yaws appmods 文件?

erlang - 运行 Yaws 应用程序

erlang - 如何在erlang的文件中存储元组及其大小?