PlugCodeReloader
View SourceLibrary 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:
def deps do
[
{:plug_code_reloader, "~> 0.1.0", only: :dev}
]
endDocumentation can be found at https://hexdocs.pm/plug_code_reloader.
Usage
On your routes file
if Code.ensure_loaded?(Mix) && Mix.env() == :dev do
plug(PlugCodeReloader)
endOn your application.ex
children = [
...
]
children = add_code_reloader(children)defp add_code_reloader(children) do
if Code.ensure_loaded?(Mix) && Mix.env() == :dev do
[PlugCodeReloader.Server | children]
else
children
end
endLimitations
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
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
endTODO
- see if there is a way to fix the request problem that makes the API be called twice for the same effect
- improve it with an IO proxy
- backup files when errors occour