# Two apps on one port, each by its name

> Two apps on port 80 of one server, each answering its own name: templates make each app, its load balancer and the proxy they share.

<style>
  .appwin ul.tut-list { margin: 0; padding: 10px 14px 12px 32px; font-size: 14px; list-style: disc; }
  .appwin ul.tut-list li { margin: 4px 0; }
  .tut-form td:first-child { width: 34%; color: var(--muted); }
  .tut-on { color: var(--brand); font-weight: 600; }
  .appwin td { vertical-align: top; }
</style>

<p>You have a server and two small web apps. You want <code>hello.example.com</code> and <code>shop.example.com</code> to reach them, both on port 80 of that one server, each its own app, deployed without touching the other.</p>

<p>In this tutorial you make both apps from a template, a form each. For each app plinto makes its code, its <a class="term" data-term="version-group" href="../concepts/version-groups.html">version group</a>, its <a class="term" data-term="environment" href="../concepts/environments.html">environment</a> and its <a class="term" data-term="pipeline" href="../concepts/pipelines.html">pipeline</a>, deploys it, and puts a load balancer in front that answers by the app's name. The first app also makes the proxy both of them share.</p>

<figure class="fig">
  <svg viewBox="0 0 640 300" role="img" aria-label="hello.example.com and shop.example.com reach one Caddy on port 80 of host group web. It has a site for each name, sending to Hello at port 8080 and Shop at port 8081; any other name gets a 404.">
    <defs><marker id="aht" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="s-arrhead" d="M0,0 L10,5 L0,10 z"/></marker></defs>
    <rect class="s-box" x="8" y="46" width="160" height="44" rx="8"/>
    <text class="s-t" x="20" y="65">hello.example.com</text>
    <text class="s-mono" x="20" y="82">GET /hello</text>
    <rect class="s-box" x="8" y="128" width="160" height="44" rx="8"/>
    <text class="s-t" x="20" y="147">shop.example.com</text>
    <text class="s-mono" x="20" y="164">GET /hello</text>
    <rect class="s-dash" x="8" y="210" width="160" height="44" rx="8"/>
    <text class="s-t2" x="20" y="229">any other name</text>
    <text class="s-mono" x="20" y="246">web-1.example.net</text>

    <rect class="s-dash" x="190" y="8" width="442" height="284" rx="12"/>
    <text class="s-head" x="206" y="30">host group web · web-1</text>

    <rect class="s-brand" x="206" y="44" width="210" height="232" rx="10"/>
    <text class="s-t" x="222" y="66">WebSharedProxy</text>
    <text class="s-t2" x="222" y="84">Caddy on port 80</text>
    <rect class="s-box2" x="220" y="100" width="182" height="40" rx="6"/>
    <text class="s-mono" x="230" y="116">http://hello.example.com</text>
    <text class="s-t3" x="230" y="132">load balancer hello</text>
    <rect class="s-box2" x="220" y="154" width="182" height="40" rx="6"/>
    <text class="s-mono" x="230" y="170">http://shop.example.com</text>
    <text class="s-t3" x="230" y="186">load balancer shop</text>
    <rect class="s-box2" x="220" y="208" width="182" height="40" rx="6"/>
    <text class="s-mono" x="230" y="224">:80, anything else</text>
    <text class="s-t3" x="230" y="240">404 No app answers to … here</text>

    <rect class="s-box" x="460" y="92" width="160" height="56" rx="8"/>
    <text class="s-t" x="476" y="114">Hello</text>
    <text class="s-mono" x="476" y="134">port 8080</text>
    <rect class="s-box" x="460" y="168" width="160" height="56" rx="8"/>
    <text class="s-t" x="476" y="190">Shop</text>
    <text class="s-mono" x="476" y="210">port 8081</text>
    <text class="s-t3" x="460" y="250">each its own app,</text>
    <text class="s-t3" x="460" y="265">deployed by its pipeline</text>

    <path class="s-line" d="M168 68 C 194 68, 196 120, 218 120" marker-end="url(#aht)"/>
    <path class="s-line" d="M168 150 C 194 150, 196 174, 218 174" marker-end="url(#aht)"/>
    <path class="s-line" d="M168 232 C 194 232, 196 228, 218 228" marker-end="url(#aht)"/>
    <path class="s-line" d="M402 120 H458" marker-end="url(#aht)"/>
    <path class="s-line" d="M402 174 C 430 174, 432 196, 458 196" marker-end="url(#aht)"/>
  </svg>
  <figcaption class="cap">What you'll have: one Caddy on port 80 of the group's hosts, with a site for each name, each sending to its own app. A name it has no site for gets a 404.</figcaption>
</figure>

<h2 id="before">Before you start</h2>

<ul>
  <li><b>A host group with a ready host.</b> Here it's <code>web</code>, with one host, <code>web-1</code>, at <code>web-1.example.net</code>. <a href="../concepts/hosts.html#add">Add a host</a> says how.</li>
  <li><b>System services on that group.</b> Port 80 is below 1024, so the shared proxy runs as a system service, and a group takes those only with <b>System services</b> on, on its page in the Hosts app.</li>
  <li><b>Port 80 open</b> on those hosts, to anyone: the run's last step asks the app through it, as anyone on the internet would.</li>
  <li><b>Nothing to declare.</b> <code>GoWebServiceTemplate</code>, the app, and <code>CaddyEdgeProxyTemplate</code>, the proxy the apps share, are the catalog's: every organization's Templates app lists them, marked <b>catalog</b>. One of your own of either name would be listed beside it, marked yours.</li>
</ul>

<div class="callout tip"><p><b>No domain yet?</b> You don't need one to start. Each app can be tried by its name with <code>curl</code> before your DNS knows it, as <a href="#try">below</a>.</p></div>

<h2 id="first">Make the first app</h2>

<ol class="steps">
  <li class="step" id="new-app" data-toc="New app"><h3>New app</h3>
    <p>In the Templates app, <b>New app</b>. Pick <b>GoWebServiceTemplate</b>, the one marked <b>catalog</b>, and name the app <code>Hello</code>. The template names everything after it: the package <code>Hello</code>, the version group <code>hello</code>, and the environments <code>Hello</code> and <code>Hello/Prod</code>.</p>
    <p>The app is a small Go service: <code>/hello</code> says which host answered, which shows the load balancer's turns once the group has more hosts, and <code>/health</code> answers <code>ok</code> while it serves.</p>
  </li>
  <li class="step" id="where" data-toc="Where it runs"><h3>Where it runs</h3>
    <p><b>Runs on</b>: <code>web</code>. Leave <b>Port</b> empty: the app takes the first port free on the group's hosts from 8080, here 8080 itself.</p>
  </li>
  <li class="step" id="part" data-toc="Its load balancer"><h3>Its load balancer</h3>
    <p>Under <b>Parts</b>, tick <b>+ Load balancer</b> and pick <b>By name</b>: on the group's shared proxy, with other apps. <b>Its own port</b> would give Hello a proxy of its own instead, on a port nobody else uses.</p>
    <div class="appwin"><div class="chrome"><i></i><i></i><i></i><span>New app · Parts</span></div>
      <table class="tut-form">
        <tr><td>+ Load balancer</td><td><span class="tut-on">✓</span> caddy, asking each server <code>/health</code></td></tr>
        <tr><td>How it answers</td><td><span class="tut-on">● By name</span> · on the group's shared proxy, with other apps</td></tr>
        <tr><td>Name</td><td class="mono">hello.example.com</td></tr>
        <tr><td>On</td><td class="mono">web</td></tr>
        <tr><td>Port</td><td><span class="mono">80</span>, the shared proxy's, left empty</td></tr>
      </table>
    </div>
    <p>The name is what the app answers to, plain HTTP. <b>On</b> is the group whose shared proxy answers it: the app's own will do.</p>
  </li>
  <li class="step" id="check" data-toc="Read It will make"><h3>Read <em>It will make</em></h3>
    <p>As you type, plinto checks the app and lists what it would make, each name checked free. With a load balancer by name, the last part reads:</p>
    <div class="appwin"><div class="chrome"><i></i><i></i><i></i><span>New app · It will make · 5 Load balancer</span></div>
      <ul class="tut-list">
        <li>First web's shared proxy, <b>WebSharedProxy</b>, made by the catalog's CaddyEdgeProxyTemplate: caddy on <b>web</b> at port <b>80</b>, for every app there with a name</li>
        <li>Then hello, answering <b>hello.example.com</b>, sending to web at 8080 and asking each /health</li>
        <li>Answers at <code>http://hello.example.com</code>, plain HTTP, once hello.example.com points at web-1.example.net in your DNS</li>
      </ul>
    </div>
    <p><b>First</b>: Hello is the first app on <code>web</code> by name, so it makes the group's shared proxy, <code>WebSharedProxy</code>. Every app after it uses that one.</p>
  </li>
  <li class="step" id="make" data-toc="Make it"><h3>Make it</h3>
    <p><b>Make Hello</b> starts the run, and its page follows each step. The first ones make the app as any template does, up to its first deployment. The three after are the load balancer's:</p>
    <div class="appwin"><div class="chrome"><i></i><i></i><i></i><span>Run · Hello</span></div>
      <div class="scrollx"><table>
        <tr><th>Step</th><th>What it says once done</th></tr>
        <tr><td>Names checked … First deployment</td><td>The app, its group, environments and pipeline, built and deployed to <code>Hello/Prod</code></td></tr>
        <tr><td>Proxy</td><td>Run 2 made WebSharedProxy and deployed it to web, at port 80</td></tr>
        <tr><td>Load balancer</td><td>Made hello, of kind caddy, on web through WebSharedProxy/Prod, sending to web at 8080</td></tr>
        <tr><td>Answers</td><td>http://web-1.example.net:80/health answers HTTP 200 for hello.example.com, through hello: Hello is at http://hello.example.com once hello.example.com points at web-1.example.net</td></tr>
      </table></div>
    </div>
    <p>The proxy is an app too, with a run of its own: Hello's run starts it and waits, and <code>WebSharedProxy</code> gets its own version group, environment and pipeline. A step that fails stops the run and says why; what it made stays, and <b>Retry</b> carries on from there.</p>
  </li>
</ol>

<h2 id="try">Try it before DNS</h2>

<p>Caddy picks the site by the name the request asks for, so <code>curl</code> can ask for <code>hello.example.com</code> at the host itself:</p>

<pre class="cmd"><code><span class="p">$ </span><span class="in">curl -H 'Host: hello.example.com' http://web-1.example.net/hello</span>
<span class="o">Hello, world, from web-1</span></code></pre>

<p>A name the proxy has no site for, here the host's own, gets a 404:</p>

<pre class="cmd"><code><span class="p">$ </span><span class="in">curl http://web-1.example.net/hello</span>
<span class="o">No app answers to web-1.example.net here</span></code></pre>

<h2 id="second">A second app on the same port</h2>

<p>Again <b>New app</b>, with <b>GoWebServiceTemplate</b>: name it <code>Shop</code>, <b>Runs on</b> <code>web</code>, <b>+ Load balancer</b>, <b>By name</b>, and <b>Name</b> <code>shop.example.com</code>. Port 8080 is Hello's now, so Shop takes 8081.</p>

<p>This time <em>It will make</em> finds the proxy there:</p>

<div class="appwin"><div class="chrome"><i></i><i></i><i></i><span>New app · It will make · 5 Load balancer</span></div>
  <ul class="tut-list">
    <li>On web's shared proxy, <b>WebSharedProxy</b>, there already at port <b>80</b></li>
    <li>Then shop, answering <b>shop.example.com</b>, sending to web at 8081 and asking each /health</li>
  </ul>
</div>

<p>and the run's <b>Proxy</b> step says <em>Uses WebSharedProxy, web's shared proxy, at port 80</em>. Once it's made, the same port answers both names:</p>

<pre class="cmd"><code><span class="p">$ </span><span class="in">curl -H 'Host: shop.example.com' http://web-1.example.net/hello</span>
<span class="o">Hello, world, from web-1</span></code></pre>

<h2 id="change">Make the shop say something else</h2>

<p>Both apps say the same, as the template wrote them. Shop's code is yours now, a package like any other, so a change goes as any change does. Get it into a <a class="term" data-term="workspace" href="../concepts/workspaces.html">workspace</a> on Shop's version group:</p>

<pre class="cmd"><code><span class="p">$ </span><span class="in">plinto workspace create shop --version-group shop</span>
<span class="p">$ </span><span class="in">cd shop</span>
<span class="p">$ </span><span class="in">plinto workspace add Shop</span></code></pre>

<p>Change what <code>/hello</code> says:</p>

<div class="file">src/Shop/src/main.go</div>
<pre><code>mux.HandleFunc("GET /hello", func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Welcome to the shop, from %s\n", host)
})</code></pre>

<p>and what its test wants of it, in <code>src/Shop/src/main_test.go</code>, so the build passes:</p>

<pre><code>"/hello": "Welcome to the shop, from web-1\n",</code></pre>

<p>Commit it in <code>src/Shop</code>, and send it as a <a class="term" data-term="review" href="../concepts/reviews.html">review</a>:</p>

<pre class="cmd"><code><span class="p">$ </span><span class="in">plinto review -m "Welcome to the shop"</span></code></pre>

<p>Once someone approves it and it's merged, Shop's pipeline builds it and deploys it to <code>Shop/Prod</code>. The deployment takes each host out of <code>shop</code> before it restarts the app there, and puts it back once it answers. With two hosts or more, nobody asking <code>shop.example.com</code> meanwhile gets an error; with <code>web-1</code> alone, Shop is away for the seconds its restart takes. Hello isn't touched either way:</p>

<pre class="cmd"><code><span class="p">$ </span><span class="in">curl -H 'Host: shop.example.com' http://web-1.example.net/hello</span>
<span class="o">Welcome to the shop, from web-1</span>
<span class="p">$ </span><span class="in">curl -H 'Host: hello.example.com' http://web-1.example.net/hello</span>
<span class="o">Hello, world, from web-1</span></code></pre>

<h2 id="dns">Point the names</h2>

<p>In your DNS, point <code>hello.example.com</code> and <code>shop.example.com</code> at the group's hosts: a record for each host's address, since each runs the shared proxy. Then anyone can ask by name:</p>

<pre class="cmd"><code><span class="p">$ </span><span class="in">curl http://shop.example.com/hello</span>
<span class="o">Welcome to the shop, from web-1</span></code></pre>

<h2 id="how">How it works</h2>

<ul>
  <li><b>WebSharedProxy</b> is Caddy, deployed to every host of <code>web</code> by its own pipeline, with plinto's agent for it, CaddyAgent, beside it.</li>
  <li><b>Each load balancer with a name</b> is a site in that Caddy, <code>http://hello.example.com</code>, sending to the app's hosts at its port and asking each its health path. The agent writes the site when the load balancer is made or a host goes in or out, and Caddy reads it without a restart.</li>
  <li><b>A deployment</b> of Hello takes each host out of <code>hello</code> before it restarts there, and back in once it answers. With two hosts or more, one always serves; a group's only host goes out anyway.</li>
  <li><b>Any other name</b> reaches the catch-all site, which answers 404. The proxy's own health is <code>/.plinto/health</code>.</li>
  <li><b>The next app</b> made by name on <code>web</code> joins <code>WebSharedProxy</code>, and nothing else changes. The first one by name on another group makes that group's shared proxy.</li>
</ul>

<h2 id="move">Move a load balancer you already have</h2>

<p>A load balancer of kind caddy you made before, behind a proxy of its own, moves onto the shared proxy in the Load balancers app: open it, <b>Edit…</b>, and set</p>

<div class="tablebox"><table>
  <tr><th>Setting</th><th>To</th></tr>
  <tr><td><code>environment</code></td><td><code>WebSharedProxy/Prod</code></td></tr>
  <tr><td><code>on</code></td><td><code>web</code>, the shared proxy's group</td></tr>
  <tr><td><code>host</code></td><td>its name, such as <code>orders.example.com</code></td></tr>
</table></div>

<p>Its old proxy keeps running, answering 503, since nothing sends to it any more.</p>

<h2 id="limits">What it doesn't do yet</h2>

<ul>
  <li><b>HTTPS.</b> The shared proxy answers plain HTTP.</li>
  <li><b>Your DNS.</b> You point the names by hand.</li>
  <li><b>A name twice.</b> Each name is one load balancer's on a shared proxy: a second one asking for it is refused.</li>
  <li><b>Taking an unused proxy off.</b> A proxy no load balancer uses keeps running until you delete its environment, which <a href="../concepts/environments.html#delete">takes it off its hosts</a>.</li>
</ul>

<div class="related">
  <a href="../concepts/hosts.html#groups"><b>Hosts</b><span>groups of servers plinto deploys to</span></a>
  <a href="../concepts/environments.html"><b>Environments</b><span>where an app runs, and its deploy settings</span></a>
  <a href="../concepts/pipelines.html"><b>Pipelines</b><span>what happens to each new revision</span></a>
  <a href="../guides/load-balancer-hooks.html"><b>A load balancer of your own</b><span>out before a restart, back once it answers</span></a>
</div>
