Lightning v0.1.2 Lightning View Source

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

How to get started:

Create a new file (App.ex)

defmodule App do

# Use the Lightning library
use Lightning

# Create a new route endpoint:
# Route: GET "/json/"
def route("GET", ["json"], conn, res) do

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

    # Send an JSON response with a statuscode of 200:
    Lightning.send_json(conn, res, 200, %{"age" => 26, "name" => "Casper"})
end


-----------------------------------------
Start up a server using the iex command:
    iex -S mix
    iex> {:ok, _} = Plug.Adapters.Cowboy.http App, []

# Navigating to localhost:4000/json will output JSON response:
# {"name":"Casper","age":26}

Link to this section Summary

Link to this section Functions

Link to this macro __using__(opts) View Source (macro)

Start the server.

Start up the server

iex -S mix
iex> {:ok, _} = Plug.Adapters.Cowboy.http <Name_of_module>, []

# Locate to localhost:4000/ in the browser
Link to this function parse_body(conn, opts \\ []) View Source

Parse the incoming request body.

Parse incoming requests

EXAMPLE:

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

    # Set additional response information:
    conn 
    |> res.put_resp_content_type("text/html")
    |> res.put_resp_cookie("abc", "def")

    # Parse the body and get value of key 'name'
    name = Lightning.parse_body(conn).params["name"]

    # Return a text response with status code 200, and "Hello <name>" as response
    Lightning.send_text(conn, res, 200, "Hello " <> name)
end
Link to this function parse_json(conn, res, status, body) View Source
Link to this function send_eex(conn, res, 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, res) do

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

    # Example using Ecto, returning different responses based on condition:
    case App.Repo.get(User, user_id) do
        nil 
            -> Lightning.send_text(conn, res, 404, "User with ID " <> user_id <> " not found")
        user 
            -> Lightning.send_eex(conn, res, 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:
Lightning.send_eex(conn, res, 200, template_show_user(user, user_id)) #
Link to this function send_json(conn, res, status, body) View Source

JSON Response.

return a JSON response

EXAMPLE:

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

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

    # Send an JSON response with a statuscode of 200:
    Lightning.send_json(conn, res, 200, %{"age" => 26, "name" => "Casper"})
end
Link to this function send_text(conn, res, status, body) View Source

Text Response.

return a text response

EXAMPLE:

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

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

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