Created
June 1, 2012 23:50
-
-
Save archaelus/2855839 to your computer and use it in GitHub Desktop.
Cowboy rest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% @copyright Geoff Cant | |
%% @author Geoff Cant <[email protected]> | |
%% @version {@vsn}, {@date} {@time} | |
%% @doc Default HTTP handler for Logmachine. | |
%% @end | |
-module(lm_logs_rest). | |
-export([init/3 | |
,rest_init/2 | |
,known_methods/2 | |
,allowed_methods/2 | |
,content_types_accepted/2 | |
,from_logplex/2 | |
,content_types_provided/2 | |
,to_response/2 | |
]). | |
init(_Transport, _Req, _Opts) -> | |
{upgrade, protocol, cowboy_http_rest}. | |
rest_init(Req, _Opts) -> | |
{ok, Req, undefined}. | |
allowed_methods(Req, State) -> | |
{['PUT'], Req, State}. | |
known_methods(Req, State) -> | |
{['PUT'], Req, State}. | |
content_types_accepted(Req, State) -> | |
{[{{<<"application">>, <<"x-logplex">>, []}, from_logplex}], | |
Req, State}. | |
from_logplex(Req, State) -> | |
case cowboy_http_req:body(Req) of | |
{ok, Body, Req1} -> | |
io:format(standard_io, "Got a valid logplex body.~n", []), | |
{true, Req1, Body}; | |
{error, Why} -> | |
io:format(standard_io, "Invalid logplex body: ~p.~n", [Why]), | |
{false, Req, State} | |
end. | |
content_types_provided(Req, State) -> | |
{[{{<<"text">>, <<"plain">>, []}, to_response}], | |
Req, State}. | |
to_response(Req, undefined) -> | |
{"UNDEFINED", Req}; | |
to_response(Req, _Body) -> | |
{"DEFINED", Req}. |
Author
archaelus
commented
Jun 1, 2012
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment