<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="133431" data-post-id="133431">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="LostKobrakai" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/LostKobrakai/120/3072_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  LostKobrakai
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="svarlet" data-post="11" data-topic="23367">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/svarlet/48/10907_2.png" class="avatar"> svarlet:</div>
<blockquote>
<p>That means I could effectively start them in application.ex and configure them there.</p>
</blockquote>
</aside>
<p>There’s <code>Phoenix.Endpoint.init/1</code>, which is called when your endpoint starts up. Not per controller, but still.</p>
<aside class="quote no-group" data-username="svarlet" data-post="11" data-topic="23367">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/svarlet/48/10907_2.png" class="avatar"> svarlet:</div>
<blockquote>
<p>Regarding tests, since they just are implementation of a behaviour, like gen_servers, they can be tested safe and fast.</p>
</blockquote>
</aside>
<p>Controllers in phoenix also implement a interface (even if not explicitly via a elixir behaviour). They’re plugs. <code>MyController.call(conn, action)</code> and you don’t need to start any infrastructure around it.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="133431" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/modules-dont-compose-well/23367/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-133431" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="133431"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="133432" data-post-id="133432">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="svarlet" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/svarlet/120/10907_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  svarlet
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="LostKobrakai" data-post="12" data-topic="23367">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lostkobrakai/48/3072_2.png" class="avatar"> LostKobrakai:</div>
<blockquote>
<p>Controllers in phoenix also implement a interface (even if not explicitly via a elixir behaviour). They’re plugs. <code>MyController.call(conn, action)</code> and you don’t need to start any infrastructure around it.</p>
</blockquote>
</aside>
<p>Yes but testing them without the endpoint and router is a real pain. First it’s going to complain about the session, then the flash, then something else, bwah.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="133432" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/modules-dont-compose-well/23367/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-133432" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="133432"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="133433" data-post-id="133433">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="michalmuskala" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/michalmuskala/120/20288_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  michalmuskala
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="svarlet" data-post="4" data-topic="23367">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/svarlet/48/10907_2.png" class="avatar"> svarlet:</div>
<blockquote>
<p>I believe that the application’s entrypoint should make all the plumbing/wiring decisions (what X to use in a Y and what Y to use in a Z). In some languages, it’s the “main” file. In Elixir, that would be <code>application.ex</code> .</p>
</blockquote>
</aside>
<p>I agree with this sooo much. I think the problem lies exactly in the fact that we can’t inject state into the Plug connections from the regular supervision structure - for any other processes we have <code>start_link</code> and the <code>init</code> callback or other mechanisms, where we can set-up the state for the process - including dependencies, but we entirely lack that possibility with plug. For some reason Plug tries to enforce completely stateless processes.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="133433" data-batch-url="/posts/batch_likers">
                        4
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/modules-dont-compose-well/23367/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-133433" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="133433"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="133435" data-post-id="133435">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="LostKobrakai" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/LostKobrakai/120/3072_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  LostKobrakai
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thats what <code>bypass_through(conn, MyAppWeb.Router, [:browser])</code> is meant to be used for.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="133435" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/modules-dont-compose-well/23367/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-133435" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="133435"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="133438" data-post-id="133438">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="svarlet" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/svarlet/120/10907_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  svarlet
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="michalmuskala" data-post="14" data-topic="23367">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/michalmuskala/48/20288_2.png" class="avatar"> michalmuskala:</div>
<blockquote>
<p>I agree with this sooo much. I think the problem lies exactly in the fact that we can’t inject state into the Plug connections from the regular supervision structure - for any other processes we have <code>start_link</code> and the <code>init</code> callback or other mechanisms, where we can set-up the state for the process - including dependencies, but we entirely lack that possibility with plug. For some reason Plug tries to enforce completely stateless processes.</p>
</blockquote>
</aside>
<p>And it seems Ace/Raxx got that one right <a class="mention" href="/u/michalmuskala" rel="nofollow">@michalmuskala</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="133438" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/modules-dont-compose-well/23367/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-133438" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="133438"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #15"></div>
  </section>
</div>
    <div class="postbit" id="133442" data-post-id="133442">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="svarlet" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/svarlet/120/10907_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  svarlet
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Is it? I never understood the docs for that one.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="133442" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/modules-dont-compose-well/23367/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-133442" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="133442"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #16"></div>
  </section>
</div>
    <div class="postbit" id="133444" data-post-id="133444">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="LostKobrakai" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/LostKobrakai/120/3072_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  LostKobrakai
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I somehow forgot <code>Phoenix.Endpoint.init/1</code> was only for configuration, but can’t a plug at the start of the endpoint do something similar.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule MyApp.Application do
  def start(_, _) do
    children = [
      {MyApp.AceController, config()}
    ]
    …
  end

  def config() do
    …
  end
end
</code></pre>
<p>vs.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule MyApp.Application do
  def start(_, _) do
    children = [
      MyApp.Endpoint
    ]
    …
  end

  def config() do
    …
  end
end

defmodule MyApp.Endpoint do
  plug :prime_with_config

  def prime_with_config(conn, _), 
    do: put_in(conn.private.my_app_config, MyApp.Application.config())
end
</code></pre>
<p>In both options the context of execution does get the config. I’m sure the first option would be cleaner, though.</p>
<aside class="quote no-group" data-username="svarlet" data-post="17" data-topic="23367" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/svarlet/48/10907_2.png" class="avatar"> svarlet:</div>
<blockquote>
<p>Is it? I never understood the docs for that one.</p>
</blockquote>
</aside>
<p>It basically takes a conn, applies all the plugs of the endpoint and any router pipes if any and returns the  updated conn. Which should solve the issue of sessions not being available and such.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="133444" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/modules-dont-compose-well/23367/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-133444" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="133444"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #17"></div>
  </section>
</div>
    <div class="postbit" id="133467" data-post-id="133467">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="josevalim" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/120/1787_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  josevalim
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Elixir</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I am not going to discuss the overall architecture ideas here but I would like to talk about the “Modules don’t compose well” bit, in particular this one:</p>
<aside class="quote no-group" data-username="svarlet" data-post="4" data-topic="23367">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/svarlet/48/10907_2.png" class="avatar"> svarlet:</div>
<blockquote>
<p>Functions compose naturally: <code>f() o g()</code> works if the output of g is an acceptable input for f. With modules, you can’t compose M1 with M2 (at least in Elixir), it doesn’t return a new module or anything else that would make sense.</p>
</blockquote>
</aside>
<p>Modules in Elixir provide the same expressive power as functions. From the perspective of functional programming, they are the same, modeled by the same principles, and the same power for composition. Maybe one is more verbose than the other, and that may be what “compose well” means in this context, but they are not different in the <em>ability</em> to compose.</p>
<p>If you have two functions, how can you compose them? We can write the <code>compose</code> function like this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Foo do
  def add_2(x), do: x + 2
  def mult_2(x), do: x * 2
  def compose(fun1, fun2), do: fn x -&gt; fun2.(fun1.(x)) end
end
</code></pre>
<p>And now:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex&gt; Foo.compose(&amp;Foo.add_2/1, &amp;Foo.mult_2/1).(13)
30
</code></pre>
<p>How can we compose modules? In similar way. Let’s assume each module has a contract, it has to implement <code>call</code>:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Add2 do
  def call(x), do: x + 2
end

defmodule Mult2 do
  def call(x), do: x * 2
end

defmodule Compose do
  def compose(mod1, mod2) do
    name = Module.concat(mod1, mod2)

    defmodule name do
      @mod1 mod1
      @mod2 mod2

      def call(x) do
        @mod2.call(@mod1.call(x))
      end
    end

    name
  end
end
</code></pre>
<p>And now:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex&gt; Compose.compose(Add2, Mult2).call(13)
30
</code></pre>
<p>The difference between the approaches is in their cost: defining the modules are much more expensive. But this is a runtime property. Plus, it can also be made cheaper by having each “module call” be represented by a tuple <code>{Mod, arg}</code>, and then you can compose without defining modules dynamically. Namely, the benefit of functions is that you can close over the current environment (closure) and by using a tuple you can emulate the same with modules.</p>
<p>in fact, one can say the advantage of a module is that you can define multiple functions and compose over multiple functions at once, but this is also possible with anonymous functions. You just need to add a new argument to the function signifying the operation you want to do. From a functional programming perspective, there isn’t much difference between those two:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Calculator do
  def add(a, b), do: a + b
  def mult(a, b), do: a * b
end

calculator = fn
  :add, a, b -&gt; a + b
  :mult, a, b -&gt; a * b
end
</code></pre>
<p>Protocols also provide the same sort of composition as anonymous functions, with the benefit they are open and can be defined/implemented at any time. In fact, a protocol may be a nice solution for your problem.</p>
<p>Anyway, the reason why I am saying this is not to say “you are wrong”, but maybe it can help you put a finger on what you don’t like about the current code.</p>
<aside class="quote no-group" data-username="michalmuskala" data-post="14" data-topic="23367">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/michalmuskala/48/20288_2.png" class="avatar"> michalmuskala:</div>
<blockquote>
<p>I agree with this sooo much. I think the problem lies exactly in the fact that we can’t inject state into the Plug connections from the regular supervision structure - for any other processes we have <code>start_link</code> and the <code>init</code> callback or other mechanisms, where we can set-up the state for the process - including dependencies, but we entirely lack that possibility with plug. For some reason Plug tries to enforce completely stateless processes.</p>
</blockquote>
</aside>
<p>I am not sure I follow the point here. Plug pipelines can be defined completely dynamically. For example:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  [
    {Plug1, opts},
    {Plug2, opts},
    {Plug3, opts},
  ]
</code></pre>
<p>And then to execute it:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">plugs
|&gt; Enum.reduce_while(conn, fn {plug, init}, conn -&gt;
  case plug.call(conn, plug.init(init)) do
    %{halted: true} = conn -&gt; {:halt, conn}
    %{} = conn -&gt; {:cont, conn}
  end
end
|&gt; elem(1)
</code></pre>
<p>This can also be meta-programmed and compiled into a module when the app starts for performance. But in a nutshell, you can always move the Plug to the runtime. However, I would avoid doing this, because understanding what my pipeline is actually doing becomes much harder. I would indeed prefer to keep them static and read the state from elsewhere.</p>
<p>And this is somewhat my concerns with the proposed code in the thread: the code may be less coupled but we lose a lot in clarity. This could be beneficial if the controller ultimately become a dumb layer (imagine that multiple gateways and business rules are served by the same controller) but I personally wouldn’t do it for individual cases.</p>
<p>This is not something Ace solves either because if you want to make decisions based on the user, which you would most likely do, by the time you know exactly which user you have, you are already too deep into the request life-cycle (and therefore inside both Ace and Plug requests). Futhermore, both Ace and Plug are module based contracts and both allow you pass arguments when building the supervision tree.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="133467" data-batch-url="/posts/batch_likers">
                        16
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/modules-dont-compose-well/23367/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-133467" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="133467"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #18"></div>
  </section>
</div>
    <div class="postbit" id="133673" data-post-id="133673">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="michalmuskala" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/michalmuskala/120/20288_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  michalmuskala
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I don’t mean plugs at all in here. I’m talking about processes - the process that is started for the request happens somewhere completely in the internals of the library and you have no access to that. The regular way of setting up state for services through <code>start_link</code> and <code>init</code> is not available.</p>
<p>What I’d like to see would be to have something like:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir"># in application.ex
children = [..., {MyApp.Endpoint, some_initial_options}, ...]

# in MyApp.Endpoint
def init_conn(conn, those_options) do
  # set up initial state for the Conn struct that is later used by this request
  # basically - the same as init for a gen_server
end
</code></pre>
<p>This would allow handling plug request processes as any other processes in the system - right now they are somewhat special and separated from the supervision tree in that there’s no way to pass state into them. The whole state has to be either provided statically (in some module somewhere) or through mutable memory (an ets table or application env).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="133673" data-batch-url="/posts/batch_likers">
                        6
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/modules-dont-compose-well/23367/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-133673" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="133673"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #19"></div>
  </section>
</div>
    <div class="postbit" id="133682" data-post-id="133682">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="josevalim" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/120/1787_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  josevalim
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Elixir</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="michalmuskala" data-post="20" data-topic="23367">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/michalmuskala/48/20288_2.png" class="avatar"> michalmuskala:</div>
<blockquote>
<p>I’m talking about processes - the process that is started for the request happens somewhere completely in the internals of the library and you have no access to that. The regular way of setting up state for services through <code>start_link</code> and <code>init</code> is not available.</p>
</blockquote>
</aside>
<p>Sorry, but I am still not seeing how that is different to this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">children = [
  {Plug.Cowboy, scheme: :http, plug: MyRouter, plug_opts: some_initial_options, options: [port: 4001])
]
</code></pre>
<p>While <code>init</code> is called at compile-time, <code>call</code> will be invoked with the custom options. But if you ignore the names and think they are <code>init_compile</code> and <code>init_runtime</code>, the result is the same (basically, for a process that runs sequentially, like a task, there is no difference between a <code>init+run</code> and just having <code>init</code> or just <code>run</code>).</p>
<p>Maybe the complaint is that Phoenix.Endpoint does not allow you do the same (which can be fixed in like 10LOC) but again, I am not sure which problem it solves. You have to build the supervision tree inside the Application callback. And how do you pass information to the application callback? Usually with the application environment. Then we are back to square one.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="133682" data-batch-url="/posts/batch_likers">
                        4
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/modules-dont-compose-well/23367/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-133682" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="133682"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #21"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/23367/load_more?page=3">Load more posts (1 remaining)</a>
</div></template></turbo-stream>