All Projects → henrik → Plug_and_play

henrik / Plug_and_play

Set up an Elixir Plug application with less boilerplate.

Programming Languages

elixir
2628 projects

Labels

Projects that are alternatives of or similar to Plug and play

vue-plug-in
create plug-in for vue
Stars: ✭ 25 (+92.31%)
Mutual labels:  plug
Guardian
Elixir Authentication
Stars: ✭ 3,150 (+24130.77%)
Mutual labels:  plug
Vim Pizza
My initial attempt at trying to order pizza from within vim.
Stars: ✭ 414 (+3084.62%)
Mutual labels:  plug
plug heartbeat
A plug for responding to heartbeat requests.
Stars: ✭ 32 (+146.15%)
Mutual labels:  plug
plug rails cookie session store
Rails compatible Plug session store
Stars: ✭ 93 (+615.38%)
Mutual labels:  plug
Terraform
A simple plug for incrementally transforming an API into Phoenix. Check out the blog post:
Stars: ✭ 379 (+2815.38%)
Mutual labels:  plug
guardian trackable
A Guardian hook to track user sign ins.
Stars: ✭ 25 (+92.31%)
Mutual labels:  plug
Dot vim
🐉 The Vim Configuration of Champions. Uses Plug to manage roughly four thousand plugins. The dragon symbolizes complexity.
Stars: ✭ 660 (+4976.92%)
Mutual labels:  plug
getx template
Used to generate the template code of GetX framework | Flutter GetX模板代码生成(一个有用的IDEA插件)
Stars: ✭ 181 (+1292.31%)
Mutual labels:  plug
Eslint Plugin Flowtype Errors
Run Flow as an ESLint plugin
Stars: ✭ 404 (+3007.69%)
Mutual labels:  plug
dictator
Dictates what your users see. Plug-based authorization.
Stars: ✭ 77 (+492.31%)
Mutual labels:  plug
accent
Dynamically convert the case of your JSON API keys
Stars: ✭ 27 (+107.69%)
Mutual labels:  plug
Trot
An Elixir web micro-framework.
Stars: ✭ 383 (+2846.15%)
Mutual labels:  plug
etag plug
A simple to use shallow ETag plug
Stars: ✭ 18 (+38.46%)
Mutual labels:  plug
Canary
🐣 Elixir authorization and resource-loading library for Plug applications.
Stars: ✭ 450 (+3361.54%)
Mutual labels:  plug
epg
一个处理电信机顶盒焦点的JavaScript插件。
Stars: ✭ 12 (-7.69%)
Mutual labels:  plug
Corsica
Elixir library for dealing with CORS requests. 🏖
Stars: ✭ 373 (+2769.23%)
Mutual labels:  plug
Dotfiles
Configurations for the tools I use every day
Stars: ✭ 898 (+6807.69%)
Mutual labels:  plug
Elixir Boilerplate
⚗ The stable base upon which we build our Elixir projects at Mirego.
Stars: ✭ 627 (+4723.08%)
Mutual labels:  plug
Als Community
Replicated and optimized community version of Advanced Locomotion System V4 for Unreal Engine 4.26 with additional bug fixes.
Stars: ✭ 389 (+2892.31%)
Mutual labels:  plug

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].