Skip to content

Instantly share code, notes, and snippets.

@hamidreza-s
Created January 8, 2014 17:57
Show Gist options
  • Save hamidreza-s/8321228 to your computer and use it in GitHub Desktop.
Save hamidreza-s/8321228 to your computer and use it in GitHub Desktop.
Just a proposal for Lebtus framework to add use middleware.
-module(rq_handler).
-compile({parse_transform, leptus_pt}).
%% leptus callbacks
-export([init/3]).
-export([get/3]).
-export([use/3]).
-export([terminate/3]).
init(_Route, _Req, State) ->
{ok, State}.
%% general middlewares
use(Route, Req, State) ->
%% logger middleware
Logger = spawn(loggerModule, handlerFunction, []),
Logger ! {self(), Route, Req, State},
%% site counter middleware
Counter = spawn(counterModule, handlerFunction, []),
Counter ! {self(), Route, Req, State},
%% etc ...
ok.
get("/", _Req, State) ->
{<<"Hello, leptus!">>, State};
get("/hi/:name", Req, State) ->
Status = 200,
Name = leptus_req:param(name, Req),
Body = [{<<"say">>, <<"Hi">>}, {<<"to">>, Name}],
{Status, {json, Body}, State}.
terminate(_Reason, _Req, _State) ->
ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment