|
1 | 1 | # HttpRouter
|
| 2 | +[](https://travis-ci.org/slogsdon/elixir-http-router) |
| 3 | +[](https://coveralls.io/r/slogsdon/elixir-http-router) |
| 4 | +[](https://hex.pm/packages/http_router) |
2 | 5 |
|
3 |
| -HTTP Router with various macros to assist in |
4 |
| -developing your application and organizing |
5 |
| -your code |
| 6 | +> HTTP Router with various macros to assist in |
| 7 | +> developing your application and organizing |
| 8 | +> your code |
| 9 | +
|
| 10 | +## Installation |
| 11 | + |
| 12 | +Add the following line to your dependency list |
| 13 | +in your `mix.exs` file, and run `mix deps.get`: |
| 14 | + |
| 15 | +```elixir |
| 16 | +{:http_router, "~> 0.0.1"} |
| 17 | +``` |
| 18 | + |
| 19 | +Also, be sure to add `:http_router` to the list |
| 20 | +of applications on which your web application |
| 21 | +depends (the default looks something like |
| 22 | +`applications: [:logger]`) in your `mix.exs` |
| 23 | +file. |
| 24 | + |
| 25 | +Be sure to have [Plug][plug] in your dependency |
| 26 | +list as well as this is essentially a |
| 27 | +reimagination of the `Plug.Router` module, and |
| 28 | +as such, it still make use of a large portion |
| 29 | +of the Plug library. |
| 30 | + |
| 31 | +## Usage |
| 32 | + |
| 33 | +To get the benefits that this package has to |
| 34 | +offer, it is necessary to use the `HttpRouter` |
| 35 | +module in one of your modules that you wish to |
| 36 | +act as the router for your web application. |
| 37 | +Similar to `Plug.Router`, we can start with a |
| 38 | +simple module: |
| 39 | + |
| 40 | +```elixir |
| 41 | +defmodule MyRouter do |
| 42 | + use HttpRouter |
| 43 | +end |
| 44 | +``` |
| 45 | + |
| 46 | +That was easy, huh? Now, this module still needs a |
| 47 | +few calls to the below macros, but this depends on |
| 48 | +how your application needs to be structured. |
| 49 | + |
| 50 | +### The Macros |
| 51 | + |
| 52 | +Check out this convoluted example: |
| 53 | + |
| 54 | +```elixir |
| 55 | +defmodule MyRouter do |
| 56 | + use HttpRouter |
| 57 | + |
| 58 | + # Define one of the versions of the API |
| 59 | + # with a simple version number "1" |
| 60 | + # or following semver "1.0.0" |
| 61 | + # or date of release "2014-09-06" |
| 62 | + version "1" do |
| 63 | + # Define your routes here |
| 64 | + get "/", Handlers.V1.Pages, :index |
| 65 | + get "/pages", Handlers.V1.Pages, :create |
| 66 | + post "/pages", Handlers.V1.Pages, :create |
| 67 | + put "/pages/:page_id" when id == 1, |
| 68 | + Handlers.V1.Pages, :update_only_one |
| 69 | + get "/pages/:page_id", Handlers.V1.Pages, :show |
| 70 | + |
| 71 | + # Auto-create a full set of routes for resources |
| 72 | + # |
| 73 | + resource :users, Handlers.V1.User, arg: :user_id |
| 74 | + # |
| 75 | + # Generates: |
| 76 | + # |
| 77 | + # get "/users", Handlers.V1.User, :index |
| 78 | + # post "/users", Handlers.V1.User, :create |
| 79 | + # get "/users/:user_id", Handlers.V1.User, :show |
| 80 | + # put "/users/:user_id", Handlers.V1.User, :update |
| 81 | + # patch "/users/:user_id", Handlers.V1.User, :patch |
| 82 | + # delete "/users/:user_id", Handlers.V1.User, :delete |
| 83 | + # |
| 84 | + # options "/users", "HEAD,GET,POST" |
| 85 | + # options "/users/:_user_id", "HEAD,GET,PUT,PATCH,DELETE" |
| 86 | + end |
| 87 | + |
| 88 | + # An updated version of the AP |
| 89 | + version "2" do |
| 90 | + get "/", Handlers.V2.Pages, :index |
| 91 | + post "/pages", Handlers.V2.Pages, :create |
| 92 | + get "/pages/:page_id", Handlers.V2.Pages, :show |
| 93 | + put "/pages/:page_id", Handlers.V2.Pages, :update |
| 94 | + |
| 95 | + raw :trace, "/trace", Handlers.V2.Tracer, :trace |
| 96 | + |
| 97 | + resource :users, Handlers.V2.User |
| 98 | + resource :groups, Handlers.V2.Group |
| 99 | + end |
| 100 | +end |
| 101 | +``` |
| 102 | + |
| 103 | +`get/3`, `post/3`, `put/3`, `patch/3`, `delete/3`, |
| 104 | +`options/2`, and `any/3` are already built-in as |
| 105 | +described. `resource` exists but will need |
| 106 | +modifications to create everything as noted. |
| 107 | + |
| 108 | +`raw/4` allows for using custom HTTP methods, allowing |
| 109 | +your application to be HTTP spec compliant. |
| 110 | + |
| 111 | +`version/2` will need to be created outright. Will |
| 112 | +allow requests to contained endpoints when version |
| 113 | +exists in either `Accepts` header or URL (which ever |
| 114 | +is defined in app config). |
| 115 | + |
| 116 | +Extra routes will need to be added for `*.json`, |
| 117 | +`*.xml`, etc. requests for optionally specifying |
| 118 | +desired content type without the use of the |
| 119 | +`Accepts` header. These should match |
| 120 | +parsing/rendering abilities of your application. |
| 121 | + |
| 122 | +## Configuration |
| 123 | + |
| 124 | +TBD. |
| 125 | + |
| 126 | +## License |
| 127 | + |
| 128 | +HttpRouter is released under the MIT License. |
| 129 | + |
| 130 | +See [LICENSE](license) for details. |
| 131 | + |
| 132 | +[plug]: https://github.com/elixir-lang/plug |
| 133 | +[license]: https://github.com/slogsdon/elixir-http-router/blob/master/LICENSE |
0 commit comments