elixir-maru / maru

Elixir RESTful Framework
https://maru.readme.io
BSD 3-Clause "New" or "Revised" License
1.32k stars 85 forks source link

use a server module #106

Closed falood closed 6 years ago

falood commented 6 years ago

something like Ecto and other apps, use a standalone module to run server, mount server to it's own supervisor tree.

defmodule MyServer do
  # config :my_api, MyServer,
  #   adapter: Cowboy2,
  #   plug: MyAPI,
  #   port: 8080,
  #   scheme: :http,
  #   bind_addrsss: 0.0.0.0
  use Maru.Server, otp_app: :my_api

  def init(_type, _opts) do
    {
      :ok,
      [
        adapter: Cowboy2,
        plug: MyAPI,
        port: 8080,
        scheme: :http,
        bind_addrsss: 0.0.0.0
      ]
    }
  end
end

defmodule MyAPI do
  use MyServer

  ...
end

and then add worker(MyServer, []) to supervisor tree.

jalcine commented 6 years ago

What are the advantages/disadvantages of doing this?

Allowing for composite servers could allow developers to scale individual workers for servers a lot more efficient (when leveraging the supervisor/worker model).

falood commented 6 years ago

Advantages

I use maru on my own production, we have 4 http servers run on the same node, most advantages are for this use case.

  1. different servers can be started by different application. For now, we start all servers by Application.start :maru, it can't resolve depends issue, we may need to start by custom order.
  2. It decouple different servers, for example we can let Maru.Type bind to service, then we don't need to define Maru.Type.UserID for all services, we may need to define MyService.Types.UserID and import_type MyServices.Types in the server module.
  3. it's also designed for plugin, we may use different plugin in different services.
  4. there's root_plug in current version, it's a bad design, I think everything belongs to root_plug can be moved to server module.

No Disadvantages, I think everything is better.

jalcine commented 6 years ago

Okay. I took some time to think over this. This would make Maru more portable and flexible to process control. I'm down for this.

What are some roadblocks that are visible right now? Outside of tests and sample code to make sure it works, that is.

falood commented 6 years ago

Nothing, I'm still thinging about how to migrate smooth.

falood commented 6 years ago

I have had https://github.com/elixir-maru/maru/pull/108 for this issue. for now, we still need make_plug: true as options of use Maru.Router, will keep working on building plug in server module.