SquidWeb.Router (squid v0.1.2)

Router helper to forward request to tentacle.

configurations

Configurations

# tentacle/config/config.exs

config :squid,
  scopes:
    admin:
      prefix: "/admin"
    prefixed_tentacle:
      prefix: "/{{tentacle_name}}/api"

{{tentacle_name}} is a reserved keyword that will be replace with your tentacle name (also know as the otp app). The app's name will be formated to kebab case.

Using the previous example, an app my_app with a prefixed_tentacle squid's scope will generate a phoenix scope prefiexed by /my-app/api.

You could also add specified configuration by env

# tentacles/config/prod.exs
config :squid,
  scopes:
    dev:
      disable: true

examples

Examples

defmodule MyTentacleWeb.Router do
  use SquidWeb.Router, otp_app: :my_tentacle

  squid_scope "/resources" do
    get "/action", MyTentacleWeb.MyResourceController, :index
  end

  squid_scope "/resources", as: :admin do
    get "/action", MyTentacleWeb.MyResourceAdminController, :index
  end

  squid_scope "/resources/dev", as: :dev do
    get "/action", MyTentacleWeb.MyResourceController, :debug
  end
end

With the configuration define in the previous chapter, this will generate followings path:

  • /resources/action
  • /admin/resources/action
  • /resources/dev/action

Those path could be generate by your router's helper (as phoenix does)

iex> MyTentacleWeb.Router.Helpers.custom_path(conn, :index)
"/resources/action"

iex> MyTentacleWeb.Router.Helpers.admin_custom_path(conn, :index)
"/admin/resources/action"

Link to this section Summary

Functions

Helper to create a tentacle router.

Helper to create a tentacle pipeline.

Helper to create a tentacle router.

Link to this section Types

@type ast() :: any()
Link to this type

tentacle_app()

@type tentacle_app() :: atom()

Link to this section Functions

Link to this macro

__using__(opts)

(macro)

Helper to create a tentacle router.

example

Example

use SquidWeb.Router, otp_app: :my_tentacle

squid_scope "/my-tentacle-prefix" do
  get "/page", MyTentacleWeb.PageController, :index
end

See squid_scope/3 for more informations.

Link to this function

create_dynamic_router(tentacles)

@spec create_dynamic_router([tentacle_app()]) :: ast()
Link to this macro

squid_pipeline(plug, list)

(macro)

Helper to create a tentacle pipeline.

examples

Examples

use SquidWeb.Router, otp_app: :my_tentacle

squid_scope "/my-tentacle" do
  pipe_through :my_tentacle_api

  # your actions, phoenix scopes ...
end

squid_pipeline :my_tentacle_api do
  plug :your_function
end

def your_function(conn, _opts) do
  # Do what ever you want

  conn
end

limitations

Limitations

Currently we don't fully support plug pipe_through. If you want to pipe_through a function, you should write an explicit function name as you can see in the next example. Otherwise, if multiples tentacles router defined the same function name, you'll have a compiled error.

use SquidWeb.Router, otp_app: :my_tentacle

squid_scope "/my-tentacle" do
  scope "/" do
    pipe_through :my_tentacle_function_name
    # your actions, phoenix scopes ...
  end

  def my_tentacle_function_name(conn, _opts) do
    # Do what ever you want
    conn
  end
end
Link to this macro

squid_scope(path, opts \\ [], do_block)

(macro)

Helper to create a tentacle router.

examples

Examples

use SquidWeb.Router, otp_app: :my_tentacle

squid_scope "/my-tentacle-prefix" do
  get "/page", MyTentacleWeb.PageController, :index
end

This will register following helpers:

  • SquidWeb.HeadRouter.Helpers.tentacle_app_page_path/2 and
  • MyTentacleWeb.Router.Helpers.page_path/2

You could also create routes under a scope such as admin.

squid_scope "/my-tentacle-prefix", scope: :admin do
  get "/users", MyTentacleWeb.UserController, :index
end

options

Options

  • scope define the scope of the router such as api, web, admin. (default: :default)