Lightning v0.1.8 Lightning View Source

Lightning is a framework for making simple REST API endpoints based on Plug

WARNING: API subject to change in future 0.x versions.
check changelog for breaking API changes: CHANGELOG

How to get started:

Create a new file (App.ex)

defmodule App do

# Use the Lightning library
use Lightning.HTTP
import Lightning

# New Route: GET "/helloworld/"
def route("GET", ["helloworld"], conn) do

    # Set additional response information (based on Plug responses):
    conn 
    |> put_resp_header("Header", "Here")
    |> put_resp_content_type("application/json")
    |> put_resp_cookie("abc", "def")
    |> put_status(200)

    # Send an JSON response with a statuscode of 200:
    json(conn, 200, %{"hello" => "world"})
end

-----------------------------------------
Start up a server using the iex command:
    iex -S mix
    iex> {:ok, _} = Lightning.start(5000, App, :dev)

# Navigating to localhost:5000/helloworld will output JSON response:
# {"hello":"world"}

Link to this section Summary

Link to this section Functions

Link to this function eex(conn, status, template, vars \\ []) View Source

EEx file response.

return an EEx response

EXAMPLE:

# Route: GET "/user/:user_id/"
def route("GET", ["user", user_id], conn) do

    # Set additional response information
    conn 
    |> put_resp_header("Header", "Here")
    |> put_resp_content_type("text/html")

    # Example using Ecto, returning different responses based on condition:
    case App.Repo.get(User, user_id) do
        nil 
            -> text(conn, 404, "User with ID " <> user_id <> " not found")
        user 
            -> eex(conn, 200, Path.expand("./lib/templates/show_user.eex"), [user: user, user_id: user_id])
    end
end


---------------------------------------------
Alternatively you can pre-compile EEx files (faster).
# place at top of App.ex file:

defmodule App do
    require EEx
    EEx.function_from_file(:def, :template_show_user, Path.expand("./lib/templates/show_user.eex"), [:user, :user_id])

# and now use its pre-compiled version passing in the user and user_id as arguments:
eex(conn, 200, template_show_user(user, user_id)) #
Link to this function json(conn, status, body) View Source

JSON Response.

return a JSON response

EXAMPLE:

# Route: GET "/json/"
def route("GET", ["json"], conn) do

    # Set additional response information:
    conn 
    |> put_resp_header("Header", "Here")
    |> put_resp_content_type("application/json")

    # Send an JSON response with a statuscode of 200:
    json(conn, 200, %{"age" => 26, "name" => "My_name"})
end
Link to this function parse_body(conn, opts \\ []) View Source

Parse the incoming request body.

Parse incoming request body

EXAMPLE:

# Route: POST "/parse?name=casper&age=26&sex=male
def route("POST", ["parse"], conn) do

    # patternmatch the request body and get the values of first two keys and ignoring last
    [name, age, _] = parse_body(conn)

    # Return a text response with status code 200
    text(conn, 200, "Hello, name: " <> name <> " age: " <> age)
end
Link to this function parse_json(conn, status, body) View Source
Link to this function parse_key(conn, key, opts \\ []) View Source

Parse the incoming request key.

Parse incoming key

EXAMPLE:

# Route: POST "/parse?name=casper
def route("POST", ["parse"], conn) do

    # Request the value of key: name
    name = parse_key(conn, :name)

    # Return a text response with status code 200
    text(conn, 200, "Hello, name: " <> name)
end
Link to this function redirect(conn, path_name) View Source
Link to this function response(conn, status, body) View Source
Link to this function response(conn, status, template, vars \\ []) View Source
Link to this function start(port, module, environment) View Source

Start the server.

Start up the server

EXAMPLE:

Lightning.start(<port>, <module_name>, <environment>)

iex -S mix
iex> {:ok, _} = Lightning.start(5000, App, :dev)

# Locate to localhost:5000/ in the browser
Link to this function text(conn, status, body) View Source

Text Response.

return a text response

EXAMPLE:

# Route: GET "/hello/:firstname/:lastname/"
 def route("GET", ["hello", firstname, lastname], conn) do

    # Set additional response information:
    conn 
    |> put_resp_header("Header", "Here")
    |> put_resp_content_type("text/html")

    # Send an text response with a statuscode of 200:
    text(conn, 200, "Hello from text response " <> firstname <> lastname)
end