UP | HOME

Leptus Documentation

Table of Contents

Quick Overview

-module(hello).
-compile({parse_transform, leptus_pt}).

%% leptus callbacks
-export([init/3]).
-export([get/3]).
-export([terminate/4]).

init(_Route, _Req, State) ->
    {ok, State}.

get("/", _Req, State) ->
    {<<"Hello, leptus!">>, State};
get("/hi/:name", Req, State) ->
    Status = ok,
    Name = leptus_req:param(Req, name),
    Body = #{<<"say">> => <<"Hi">>, <<"to">> => Name},
    {Status, Body, State}.

terminate(_Reason, _Route, _Req, _State) ->
    ok.

Note the use of the following in your request/resource handler module.

-compile({parse_transform, leptus_pt}).

Let's compile, and run the example above.

$ erl -pa ebin deps/*/ebin
> c(hello).
> leptus:start_listener(http, [{'_', [{hello, undefined_state}]}]).
Leptus started on http://127.0.0.1:8080
$ curl localhost:8080/hi/Leptus
{"say":"Hi","to":"Leptus"}

Author: Sina Samavati

Created: 2018-09-19