How to add phoenix to existing project and start server

I have a riak_core application in production. I need to add phoenix to serve some pages. I have added all needed parts except the Supervisor part. My app has an Application module and separate Supervisor Module. How can I start phoenix server? Where is Endpoint starting command?

Phoenix is just a library, you can add it like any other Elixir library.

First put it in your dependencies.
Then make an Endpoint module:

defmodule CCCServer.Web.Endpoint do
  use Phoenix.Endpoint, otp_app: :my_blah

  def load_from_system_env(config) do
    port = System.get_env("PORT") || raise "expected the PORT environment variable to be set"
    {:ok, Keyword.put(config, :http, [:inet6, port: port])}
  end

Then add that Endpoint module to your supervision tree:

supervisor(MyBlah.Web.Endpoint, []),

Now you can do stuff, like if you want static file serving then add a static file plug to your endpoint:

  plug Plug.Static,
    at: "/", from: :ccc_server, gzip: true,
    only: ~w(css fonts images js favicon.ico robots.txt)

Or add whatever other plugs you want or sockets or add a router:

  plug MyBlah.Web.Router

Along with making the router module too.

Everything in phoenix is pluggable, you can pick and choose what you want. :slight_smile:

6 Likes