Open plinto
Docs›Tutorials

Two apps on one port, each by its name

Tutorial·15 min read

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

In this tutorial you make both apps from a template, a form each. For each app plinto makes its code, its version group, its environment and its pipeline, 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.

hello.example.com GET /hello shop.example.com GET /hello any other name web-1.example.net host group web · web-1 WebSharedProxy Caddy on port 80 http://hello.example.com load balancer hello http://shop.example.com load balancer shop :80, anything else 404 No app answers to … here Hello port 8080 Shop port 8081 each its own app, deployed by its pipeline
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.

Before you start

No domain yet? You don't need one to start. Each app can be tried by its name with curl before your DNS knows it, as below.

Make the first app

  1. New app

    In the Templates app, New app. Pick GoWebServiceTemplate, the one marked catalog, and name the app Hello. The template names everything after it: the package Hello, the version group hello, and the environments Hello and Hello/Prod.

    The app is a small Go service: /hello says which host answered, which shows the load balancer's turns once the group has more hosts, and /health answers ok while it serves.

  2. Where it runs

    Runs on: web. Leave Port empty: the app takes the first port free on the group's hosts from 8080, here 8080 itself.

  3. Its load balancer

    Under Parts, tick + Load balancer and pick By name: on the group's shared proxy, with other apps. Its own port would give Hello a proxy of its own instead, on a port nobody else uses.

    New app · Parts
    + Load balancer✓ caddy, asking each server /health
    How it answers● By name · on the group's shared proxy, with other apps
    Namehello.example.com
    Onweb
    Port80, the shared proxy's, left empty

    The name is what the app answers to, plain HTTP. On is the group whose shared proxy answers it: the app's own will do.

  4. Read It will make

    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:

    New app · It will make · 5 Load balancer
    • First web's shared proxy, WebSharedProxy, made by the catalog's CaddyEdgeProxyTemplate: caddy on web at port 80, for every app there with a name
    • Then hello, answering hello.example.com, sending to web at 8080 and asking each /health
    • Answers at http://hello.example.com, plain HTTP, once hello.example.com points at web-1.example.net in your DNS

    First: Hello is the first app on web by name, so it makes the group's shared proxy, WebSharedProxy. Every app after it uses that one.

  5. Make it

    Make Hello 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:

    Run · Hello
    StepWhat it says once done
    Names checked … First deploymentThe app, its group, environments and pipeline, built and deployed to Hello/Prod
    ProxyRun 2 made WebSharedProxy and deployed it to web, at port 80
    Load balancerMade hello, of kind caddy, on web through WebSharedProxy/Prod, sending to web at 8080
    Answershttp://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

    The proxy is an app too, with a run of its own: Hello's run starts it and waits, and WebSharedProxy gets its own version group, environment and pipeline. A step that fails stops the run and says why; what it made stays, and Retry carries on from there.

Try it before DNS

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

$ curl -H 'Host: hello.example.com' http://web-1.example.net/hello
Hello, world, from web-1

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

$ curl http://web-1.example.net/hello
No app answers to web-1.example.net here

A second app on the same port

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

This time It will make finds the proxy there:

New app · It will make · 5 Load balancer
  • On web's shared proxy, WebSharedProxy, there already at port 80
  • Then shop, answering shop.example.com, sending to web at 8081 and asking each /health

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

$ curl -H 'Host: shop.example.com' http://web-1.example.net/hello
Hello, world, from web-1

Make the shop say something else

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 workspace on Shop's version group:

$ plinto workspace create shop --version-group shop
$ cd shop
$ plinto workspace add Shop

Change what /hello says:

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

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

"/hello": "Welcome to the shop, from web-1\n",

Commit it in src/Shop, and send it as a review:

$ plinto review -m "Welcome to the shop"

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

$ curl -H 'Host: shop.example.com' http://web-1.example.net/hello
Welcome to the shop, from web-1
$ curl -H 'Host: hello.example.com' http://web-1.example.net/hello
Hello, world, from web-1

Point the names

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

$ curl http://shop.example.com/hello
Welcome to the shop, from web-1

How it works

Move a load balancer you already have

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, Edit…, and set

SettingTo
environmentWebSharedProxy/Prod
onweb, the shared proxy's group
hostits name, such as orders.example.com

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

What it doesn't do yet