All Projects → kbrw → Ewebmachine

kbrw / Ewebmachine

Licence: mit
The HTTP decision tree as a plug (full elixir rewriting of basho/webmachine with improvements)

Labels

Projects that are alternatives of or similar to Ewebmachine

plug rails cookie session store
Rails compatible Plug session store
Stars: ✭ 93 (-2.11%)
Mutual labels:  plug
Vim Pizza
My initial attempt at trying to order pizza from within vim.
Stars: ✭ 414 (+335.79%)
Mutual labels:  plug
Liberator
An Elixir library for building RESTful applications.
Stars: ✭ 28 (-70.53%)
Mutual labels:  plug
Guardian
Elixir Authentication
Stars: ✭ 3,150 (+3215.79%)
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 (+309.47%)
Mutual labels:  plug
Elixir Boilerplate
⚗ The stable base upon which we build our Elixir projects at Mirego.
Stars: ✭ 627 (+560%)
Mutual labels:  plug
sheriff
Build simple and robust authorization systems with just Elixir and Plug
Stars: ✭ 39 (-58.95%)
Mutual labels:  plug
Multiverse
Elixir package that allows to add compatibility layers via API gateways.
Stars: ✭ 87 (-8.42%)
Mutual labels:  plug
Eslint Plugin Flowtype Errors
Run Flow as an ESLint plugin
Stars: ✭ 404 (+325.26%)
Mutual labels:  plug
Plug and play
Set up an Elixir Plug application with less boilerplate.
Stars: ✭ 13 (-86.32%)
Mutual labels:  plug
Corsica
Elixir library for dealing with CORS requests. 🏖
Stars: ✭ 373 (+292.63%)
Mutual labels:  plug
Trot
An Elixir web micro-framework.
Stars: ✭ 383 (+303.16%)
Mutual labels:  plug
Dot vim
🐉 The Vim Configuration of Champions. Uses Plug to manage roughly four thousand plugins. The dragon symbolizes complexity.
Stars: ✭ 660 (+594.74%)
Mutual labels:  plug
getx template
Used to generate the template code of GetX framework | Flutter GetX模板代码生成(一个有用的IDEA插件)
Stars: ✭ 181 (+90.53%)
Mutual labels:  plug
Authex
Authex is an opinionated JWT authentication and authorization library for Elixir.
Stars: ✭ 73 (-23.16%)
Mutual labels:  plug
accent
Dynamically convert the case of your JSON API keys
Stars: ✭ 27 (-71.58%)
Mutual labels:  plug
Canary
🐣 Elixir authorization and resource-loading library for Plug applications.
Stars: ✭ 450 (+373.68%)
Mutual labels:  plug
Flutter weather bg
A rich and cool weather dynamic background plug-in
Stars: ✭ 89 (-6.32%)
Mutual labels:  plug
Plugapi
A generic API for creating plug.dj bots
Stars: ✭ 83 (-12.63%)
Mutual labels:  plug
Dotfiles
Configurations for the tools I use every day
Stars: ✭ 898 (+845.26%)
Mutual labels:  plug

Ewebmachine is a full rewrite with clean DSL and plug integration based on Webmachine from basho. This version is not backward compatible with the previous one that was only a thin wrapper around webmachine, use the branch 1.0-legacy to use the old one.

Build Status

See the generated documentation for more detailed explanations.

The principle is to go through the HTTP decision tree and make decisions according to response of some callbacks called "handlers".

To do that, the library gives you 5 plugs and 2 plug pipeline builders :

  • Ewebmachine.Plug.Run go through the HTTP decision tree and fill the conn response according to it
  • Ewebmachine.Plug.Send is used to send a conn set with Ewebmachine.Plug.Run
  • Ewebmachine.Plug.Debug gives you a debugging web UI to see the HTTP decision path taken by each request.
  • Ewebmachine.Plug.ErrorAsException take a conn with a response set but not send, and throw an exception is the status code is an exception
  • Ewebmachine.Plug.ErrorAsForward take a conn with a response set but not send, and forward it changing the request to GET /error/pattern/:status
  • Ewebmachine.Builder.Handlers gives you helpers macros and a :add_handler plug to add handlers as defined in Ewebmachine.Handlers to your conn, and set the initial user state.
  • Ewebmachine.Builder.Resources gives you a resource macro to define at the same time an Ewebmachine.Builder.Handlers and the matching spec to use it, and a plug :resource_match to do the match and execute the associated plug. The macro resources_plugs helps you to define commong plug pipeling

Example usage

defmodule MyJSONApi do 
  use Ewebmachine.Builder.Handlers
  plug :cors
  plug :add_handlers, init: %{}

  content_types_provided do: ["application/json": :to_json]
  defh to_json, do: Poison.encode!(state[:json_obj])

  defp cors(conn,_), do: 
    put_resp_header(conn,"Access-Control-Allow-Origin","*")
end


defmodule ErrorRoutes do
  use Ewebmachine.Builder.Resources ; resources_plugs
  resource "/error/:status" do %{s: elem(Integer.parse(status),0)} after 
    content_types_provided do: ['text/html': :to_html, 'application/json': :to_json]
    defh to_html, do: "<h1> Error ! : '#{Ewebmachine.Core.Utils.http_label(state.s)}'</h1>"
    defh to_json, do: ~s/{"error": #{state.s}, "label": "#{Ewebmachine.Core.Utils.http_label(state.s)}"}/
    finish_request do: {:halt,state.s}
  end
end

defmodule FullApi do
  use Ewebmachine.Builder.Resources
  if Mix.env == :dev, do: plug Ewebmachine.Plug.Debug
  resources_plugs error_forwarding: "/error/:status", nomatch_404: true
  plug ErrorRoutes

  resource "/hello/:name" do %{name: name} after 
    content_types_provided do: ['application/xml': :to_xml]
    defh to_xml, do: "<Person><name>#{state.name}</name>"
  end

  resource "/hello/json/:name" do %{name: name} after 
    plug MyJSONApi #this is also a plug pipeline
    allowed_methods do: ["GET","DELETE"]
    resource_exists do: pass((user=DB.get(state.name)) !== nil, json_obj: user)
    delete_resource do: DB.delete(state.name)
  end

  resource "/static/*path" do %{path: Enum.join(path,"/")} after
    resource_exists do:
      File.regular?(path state.path)
    content_types_provided do:
      [{state.path|>Plug.MIME.path|>default_plain,:to_content}]
    defh to_content, do:
      File.stream!(path(state.path),[],300_000_000)
    defp path(relative), do: "#{:code.priv_dir :ewebmachine_example}/web/#{relative}"
    defp default_plain("application/octet-stream"), do: "text/plain"
    defp default_plain(type), do: type
  end
end

Debug UI

Go to /wm_debug to see precedent requests and debug there HTTP decision path. The debug UI can be updated automatically on the requests.

Debug UI example

Use Cowboy to serve the plug

Create a simple supervision tree with only the Cowboy server adapter spec.

defmodule MyApp do
  use Application
  def start(_type, _args), do:
    Supervisor.start_link([
        Plug.Adapters.Cowboy.child_spec(:http,FullApi,[], port: 4000)
      ], strategy: :one_for_one)
end

And add it as your application entry point in your mix.exs

def application do
  [applications: [:logger,:ewebmachine,:cowboy], mod: {MyApp,[]}]
end
defp deps, do:
  [{:ewebmachine, "2.0.0"}, {:cowboy, "~> 1.0"}]
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].