# PlugCodeReloader

Library that allows you to reload your code on every request in development mode, without the need to restart your server.

## Installation

The package can be installed
by adding `plug_code_reloader` to your list of dependencies in `mix.exs`:

```elixir
def deps do
  [
    {:plug_code_reloader, "~> 0.1.0", only: :dev}
  ]
end
```

Documentation can be found at <https://hexdocs.pm/plug_code_reloader>.

## Usage

On your routes file

```elixir
  if Code.ensure_loaded?(Mix) && Mix.env() == :dev do
    plug(PlugCodeReloader)
  end
```

On your `application.ex`

```elixir
    children = [
        ...
    ]

    children = add_code_reloader(children)
```

```elixir
defp add_code_reloader(children) do
    if Code.ensure_loaded?(Mix) && Mix.env() == :dev do
        [PlugCodeReloader.Server | children]
    else
        children
    end
end
```

## Limitations

If you have a big route file that includes all implementations of the API resources, the code is only gonna be reloaded the next request, not the first, but if you forward your request to another plug and it will work

```elixir
defmodule MyApp.Router do
  use Plug.Router

  if Mix.env() == :dev do
    plug(PlugCodeReloader)
    plug(Plug.Logger)
  end

  plug(:match)
  plug(:dispatch)

  # will reload on the first request
  forward("/foo", to: MyApp.Foo)
  match("/bar", to: MyApp.Bar)

  # will reload on the second request
  get("/foobar") do
    send_resp(conn, 200, "")
  end

end
```

## TODO

1. see if there is a way to fix the request problem that makes the API be called twice for the same effect
2. improve it with an IO proxy
3. backup files when errors occour
