HTTP/1.1 400 Bad Request
connection: close
server: Cowboy
date: Mon, 22 Jul 2013 06:09:17 GMT
content-length: 0
{ok, _} = cowboy:start_http(
http, 100,
[{port, HttpPort}],
[{env, [{dispatch, DispatchRules}]}]),
-module(sg_rest_handler).
-define(CT_JSON, {<<"content-type">>, <<"application/json">>}).
-define(bta(Data), binary_to_atom(Data, utf8)).
-export([init/3, handle/2, terminate/3]).
-export([test/3]).
init({tcp, http}, Req, _Opts) -> {ok, Req, undefined_state}.
terminate(_Reason, _Req, _State) -> ok.
handle(Req, State) -> {ok, dispatch(Req), State}.
test('POST', [_Data] , _Req) -> ok;
test('GET', [_Data] , _Req) -> ok.
dispatch(Req) ->
{Method, _} = cowboy_req:method(Req),
{[Fn|Data], _} = cowboy_req:path_info(Req),
Result = apply(?MODULE, ?bta(Fn), [?bta(Method), Data, Req]),
json(Result, Req).
json(Data, Req) ->
{ok, Reply} = cowboy_req:reply(200, [?CT_JSON], json_encode(Data), Req),
Reply.
json_encode(Data) -> lists:flatten(mochijson2:encode(Data)).
О©╫О©╫О©╫О©╫О©╫О©╫.О©╫
О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫, О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ ajax О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ (О©╫О©╫О©╫О©╫О©╫ 20 О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫)О©╫О©╫О©╫ get О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫ О©╫О©╫, О©╫О©╫О©╫ post - О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫:
HTTP/1.1 400 Bad Request
connection: close
server: Cowboy
date: Mon, 22 Jul 2013 06:09:17 GMT
content-length: 0
О©╫О©╫О©╫ О©╫О©╫О©╫О©╫ О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫
О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫ О©╫ О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫. О©╫О©╫О©╫О©╫ О©╫О©╫ О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫ О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫ О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫, О©╫ О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫
О©╫ О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫?
О©╫О©╫О©╫О©╫О©╫О©╫ - {git, "https://github.com/extend/cowboy.git", {branch, "master"}},
О©╫ О©╫ {ok, _} = cowboy:start_http(
О©╫ О©╫ О©╫ О©╫ О©╫ О©╫ О©╫ О©╫ О©╫ О©╫ О©╫ О©╫ http, 100,
О©╫ О©╫ О©╫ О©╫ О©╫ О©╫ О©╫ О©╫ О©╫ О©╫ О©╫ О©╫ [{port, HttpPort}],
О©╫ О©╫ О©╫ О©╫ О©╫ О©╫ О©╫ О©╫ О©╫ О©╫ О©╫ О©╫ [{env, [{dispatch, DispatchRules}]}]),
О©╫О©╫О©╫О©╫О©╫О©╫О©╫
-module(sg_rest_handler).
-define(CT_JSON, {<<"content-type">>, <<"application/json">>}).
-define(bta(Data), binary_to_atom(Data, utf8)).
-export([init/3, handle/2, terminate/3]).
-export([test/3]).
init({tcp, http}, Req, _Opts) О©╫ О©╫-> {ok, Req, undefined_state}.
terminate(_Reason, _Req, _State) -> ok.
handle(Req, State) О©╫ О©╫ О©╫ О©╫ О©╫ О©╫ О©╫ -> {ok, dispatch(Req), State}.
test('POST', [_Data] , _Req) -> ok;
--
test('GET', О©╫[_Data] , _Req) -> ok.
dispatch(Req) ->
О©╫ О©╫ {Method, _} О©╫ О©╫= cowboy_req:method(Req),
О©╫ О©╫ {[Fn|Data], _} = cowboy_req:path_info(Req),
О©╫ О©╫ Result = apply(?MODULE, ?bta(Fn), [?bta(Method), Data, Req]),
О©╫ О©╫ json(Result, Req).
О©╫ О©╫
json(Data, Req) ->
О©╫ О©╫ {ok, Reply} = cowboy_req:reply(200, [?CT_JSON], json_encode(Data), Req),
О©╫ О©╫ Reply.
json_encode(Data) О©╫-> lists:flatten(mochijson2:encode(Data)).
--
О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫: http://groups.google.com/group/erlang-russian
О©╫О©╫О©╫О©╫О©╫О©╫О©╫: http://erlanger.ru
О©╫О©╫О©╫: xmpp://erl...@conference.jabber.ru
О©╫О©╫О©╫ О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫: xmpp://erlang...@conference.jabber.ru
О©╫О©╫О©╫О©╫О©╫О©╫О©╫, О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫ О©╫О©╫О©╫О©╫ О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫: http://erlanger.ru/ru/erlang-at-conference-jabber-ru
О©╫
О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫: erlang-...@googlegroups.com
О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫: erlang-russia...@googlegroups.com
---
О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫, О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫ Erlang О©╫О©╫-О©╫О©╫О©╫О©╫О©╫О©╫.
О©╫
О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫ О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫ О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫ О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫, О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫ О©╫О©╫О©╫О©╫О©╫ erlang-russia...@googlegroups.com.
О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫: https://groups.google.com/groups/opt_out.
О©╫
О©╫
О©╫О©╫О©╫: xmpp://erlang@conference.jabber.ru
О©╫О©╫О©╫ О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫: xmpp://erlang-talks@conference.jabber.ru
О©╫О©╫О©╫: xmpp://erlang@conference.jabber.ru
О©╫О©╫О©╫ О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫: xmpp://erlang-talks@conference.jabber.ru
--
--
Страница рассылки: http://groups.google.com/group/erlang-russian
Новости: http://erlanger.ru
Чат: xmpp://erl...@conference.jabber.ru
Чат для оффтопа: xmpp://erlang...@conference.jabber.ru
Правила, действующие в чате и рассылке: http://erlanger.ru/ru/erlang-at-conference-jabber-ru
Написать письмо: erlang-...@googlegroups.com
Отписаться: erlang-russia...@googlegroups.com
---
Вы получили это сообщение, поскольку подписаны на группу Erlang по-русски.
Чтобы отказаться от подписки на эту группу и перестать получать из нее сообщения, отправьте электронное письмо на адрес erlang-russia...@googlegroups.com.
Настройки подписки и доставки писем: https://groups.google.com/groups/opt_out.
json_encode(Data) -> lists:flatten(mochijson2:encode(Data)).
какие вообще есть best practices для диспатчинга? мне в голову только словарь <путь>:<обработчик> приходит (например, в виде генсервера с апи получения и связывания обработчиков)