Skip to content

Commit

Permalink
feat: add fallback for unknown hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoffroy Empain committed Dec 7, 2020
1 parent c0bfd57 commit 9e0df52
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/caddy/config/fallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { getReverseProxyDial } from '../utils/get-reverse-proxy-dial';
import { env } from '../../env';

export const fallback = {
/*
* By default, caddy returns 200 with an empty response when no route matches.
* This is a bit confusing, so we're changing it to a 523, used by Cloudflare to
* indicate that the destination is unreachable.
*/
handle: [
{
handler: 'rewrite',
uri: '/static/523.html',
},
// https://caddyserver.com/docs/json/apps/http/servers/routes/handle/reverse_proxy/
{
handler: 'reverse_proxy',
upstreams: [{
dial: getReverseProxyDial(env.MELI_URL_INTERNAL),
}],
handle_response: [{
status_code: '523',
}],
},
],
terminal: true,
};
2 changes: 2 additions & 0 deletions src/caddy/generate-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { generateManualCertificatesConfig, generateServerTlsConfig } from './con
import { uiRoute } from './config/ui-route';
import { apiRoute } from './config/api-route';
import { URL } from 'url';
import { fallback } from './config/fallback';

const meliUrl = new URL(env.MELI_URL);

Expand Down Expand Up @@ -34,6 +35,7 @@ export async function generateConfig(): Promise<any> {
apiRoute,
uiRoute,
...sites.flatMap(generateSiteRoutes),
fallback,
],
errors: getErrorRoutes(sites),
...(sslDisabled ? [] : generateServerTlsConfig(sites)),
Expand Down
2 changes: 1 addition & 1 deletion src/public/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
</div>

<h1>404</h1>
<p class="message">We greatly apologize, but your page could not be found</p>
<p class="message">We greatly apologize, but your page could not be found.</p>
</section>
</body>
</html>
47 changes: 47 additions & 0 deletions src/public/523.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Meli | Unknown host</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:wght@200;400;600;900&display=swap">
<style>
body {
display: flex;
justify-content: center;
align-items: center;
font-family: Source Sans Pro, sans-serif;
margin-top: 100px;
}

section {
text-align: center;
}

.logo {
height: 50px;
}

.message {
margin-top: 50px;
max-width: 75%;
width: 400px;
margin: 0 auto;
}
</style>
</head>
<body>
<section>
<svg viewBox="0 0 409 150" fill="none" xmlns="http://www.w3.org/2000/svg" class="logo">
<path d="M2 146.8V136.6L11.2 134.4C11.3333 129.6 11.4 124.533 11.4 119.2C11.4 113.733 11.4 108.933 11.4 104.8V92.4C11.4 88.6667 11.4 85.6667 11.4 83.4C11.4 81.1333 11.3333 79 11.2 77C11.2 75 11.0667 72.6667 10.8 70L0 68.2V60L44.2 46.2L47.8 48.4L48.8 62.2C53.4667 56.6 58.1333 52.5333 62.8 50C67.4667 47.4667 72.7333 46.2 78.6 46.2C92.7333 46.2 101.933 52 106.2 63.6C111.8 56.6667 116.933 52.0667 121.6 49.8C126.267 47.4 131.333 46.2 136.8 46.2C147.733 46.2 155.533 49.2 160.2 55.2C165 61.2 167.4 70.0667 167.4 81.8V104.8C167.4 109.067 167.4 113.933 167.4 119.4C167.4 124.867 167.467 130 167.6 134.8L176 136.6V146.8H122.4V136.6L130 134.6C130.133 129.8 130.2 124.667 130.2 119.2C130.2 113.733 130.2 108.933 130.2 104.8V84.6C130.2 77.8 129.533 73.2667 128.2 71C126.867 68.7333 124.467 67.6 121 67.6C116.733 67.6 112.467 69.2 108.2 72.4C108.333 73.4667 108.4 74.6 108.4 75.8C108.533 77 108.6 78.2667 108.6 79.6V104.8C108.6 109.067 108.6 113.933 108.6 119.4C108.6 124.867 108.667 130.067 108.8 135L116.4 136.6V146.8H62.6V136.6L71.2 134.4C71.3333 129.733 71.4 124.667 71.4 119.2C71.4 113.733 71.4 108.933 71.4 104.8V84.6C71.4 78.4667 70.7333 74.1333 69.4 71.6C68.2 68.9333 65.6667 67.6 61.8 67.6C59.8 67.6 57.7333 68.0667 55.6 69C53.4667 69.9333 51.4 71.1333 49.4 72.6V104.8C49.4 108.933 49.4 113.8 49.4 119.4C49.4 124.867 49.4667 130.067 49.6 135L57.2 136.6V146.8H2ZM235.231 57C232.165 57 229.565 59.2667 227.431 63.8C225.298 68.3333 224.098 76.8 223.831 89.2H233.231C238.031 89.2 241.231 88.2667 242.831 86.4C244.565 84.4 245.431 80.8667 245.431 75.8C245.431 68.8667 244.365 64 242.231 61.2C240.231 58.4 237.898 57 235.231 57ZM235.831 150C225.831 150 216.898 147.933 209.031 143.8C201.298 139.667 195.165 133.733 190.631 126C186.098 118.133 183.831 108.733 183.831 97.8C183.831 89.1333 185.365 81.6 188.431 75.2C191.498 68.8 195.631 63.4667 200.831 59.2C206.031 54.8 211.831 51.5333 218.231 49.4C224.631 47.2667 231.098 46.2 237.631 46.2C247.231 46.2 255.165 48.2 261.431 52.2C267.831 56.0667 272.631 61.3333 275.831 68C279.031 74.5333 280.631 81.8667 280.631 90C280.631 92.1333 280.565 94 280.431 95.6C280.298 97.0667 280.031 98.7333 279.631 100.6H224.031C225.098 110.333 227.965 117.4 232.631 121.8C237.431 126.2 242.765 128.4 248.631 128.4C253.698 128.4 258.031 127.533 261.631 125.8C265.365 123.933 268.631 121.667 271.431 119L279.231 126.6C274.965 134.733 269.098 140.667 261.631 144.4C254.298 148.133 245.698 150 235.831 150ZM286.198 146.8V136.6L295.198 134.6C295.332 129.533 295.398 124.533 295.398 119.6C295.532 114.667 295.598 109.733 295.598 104.8V22.6L284.798 20.6V11.6L330.998 2.6L334.398 4.79999L333.598 32.6V104.8C333.598 109.733 333.598 114.733 333.598 119.8C333.732 124.733 333.865 129.733 333.998 134.8L342.998 136.6V146.8H286.198ZM353.195 146.8V136.6L362.195 134.6C362.329 129.667 362.395 124.533 362.395 119.2C362.395 113.733 362.395 108.933 362.395 104.8V95.2C362.395 89.7333 362.329 85.2 362.195 81.6C362.195 77.8667 362.062 74 361.795 70L350.995 68.2V60L397.395 46.2L400.995 48.4L400.395 78.2V104.8C400.395 108.933 400.395 113.8 400.395 119.4C400.395 124.867 400.462 130 400.595 134.8L408.995 136.6V146.8H353.195ZM380.995 34.4C375.929 34.4 371.595 32.8 367.995 29.6C364.395 26.2667 362.595 22.1333 362.595 17.2C362.595 12.1333 364.395 8 367.995 4.79999C371.595 1.59999 375.929 0 380.995 0C386.062 0 390.395 1.59999 393.995 4.79999C397.595 8 399.395 12.1333 399.395 17.2C399.395 22.1333 397.595 26.2667 393.995 29.6C390.395 32.8 386.062 34.4 380.995 34.4Z" fill="black"/>
</svg>
<div class="hero">
Shipping frontend
</div>

<h1>523</h1>
<p class="message">We greatly apologize, but the requested host could is not served by this server.</p>
</section>
</body>
</html>

0 comments on commit 9e0df52

Please sign in to comment.