ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - henrik/plug_and_play: Set up an Elixir Plug application with less boilerplate.
Set up an Elixir Plug application with less boilerplate. - henrik/plug_and_play
Visit Site

GitHub - henrik/plug_and_play: Set up an Elixir Plug application with less boilerplate.

GitHub - henrik/plug_and_play: Set up an Elixir Plug application with less boilerplate.

PlugAndPlay 🔌►

Build Status Elixir Forum

Set up a Plug application with less boilerplate.

PlugAndPlay is not a web framework – it's a small scaffold. You use Plug as you would normally, only sooner.

Later, if you need more control, you can easily replace PlugAndPlay piece by piece or wholesale.

Setting up a Plug app, the easy way

Generate a new project with --sup, e.g.

mix new hello_world --sup

Open mix.exs and add plug_and_play to your list of dependencies:

def deps do
  [
    {:plug_and_play, "~> 0.7.0"},
  ]
end

(This makes PlugAndPlay conveniences available and saves you from manually adding Plug and the Cowboy web server to the deps list.)

Make your root module (e.g. lib/hello_world.ex) look something like:

defmodule HelloWorld do
  defmodule Router do
    use PlugAndPlay.Router

    get "/" do
      send_resp conn, 200, "Hello world!"
    end

    match _ do
      send_resp conn, 404, "404!"
    end
  end
end

(This saves you from manually including some Plug.Router boilerplate.)

Make your application module (e.g. lib/hello_world/application.ex) look something like:

defmodule HelloWorld.Application do
  use PlugAndPlay.Application, router: HelloWorld.Router
end

(This saves you from manually setting up a Supervisor to run your app in the Cowboy web server on the right port.)

Now you should be able to start the app in a terminal with:

mix deps.get
mix server

(This saves you from typing mix run --no-halt.)

It outputs the URL at which the server runs - usually http://0.0.0.0:8080. Go there and marvel!

Custom port number

The default port is 8080.

If the environment variable PORT is set, that port number will be used. This is the convention on e.g. Heroku and with Dokku, meaning things will Just Work™ if you deploy there.

Or you can assign a port explicitly, e.g. from application config.

Assuming you have config like this in config/config.exs:

config :hello_world, port: 1234

You could do this in lib/hello_world/application.ex:

defmodule HelloWorld.Application do
  use PlugAndPlay.Application,
    router: HelloWorld.Router,
    port: Application.get_env(:hello_world, :port)
end

If you set up your own supervision tree, you can also specify the port there.

Custom plug pipeline

If you need additional plugs, skip use PlugAndPlay.Router and simply write out the code instead:

use Plug.Router

plug :match
plug :my_custom_plug
plug :dispatch

You can still use the rest of the PlugAndPlay conveniences, of course, even if you skip PlugAndPlay.Router.

Custom supervision

By default, PlugAndPlay defines a supervision tree for you so you don't have to. If that's all you need, ignore this section.

If you want more control, you can define your own supervision tree with PlugAndPlay.Supervisor as one of its children.

Make your main application (e.g. lib/hello_world/application.ex) look something like:

defmodule HelloWorld.Application do
  use Application

  def start(_type, _args) do
    children = [
      PlugAndPlay.Supervisor.child_spec(HelloWorld.Router),
    ]

    Supervisor.start_link(children, strategy: :one_for_one)
  end
end

You can specify the desired port as a second argument to child_spec.

You can specify multiple instances as long as they have different routers and ports:

children = [
  PlugAndPlay.Supervisor.child_spec(HelloWorld.RouterOne, 1111),
  PlugAndPlay.Supervisor.child_spec(HelloWorld.RouterTwo, 2222),
]

Credits and license

By Henrik Nyh 2017-02-25 under the MIT License.

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory