Skip to content

Commit

Permalink
feat: redirect 404 to each site /404.html
Browse files Browse the repository at this point in the history
  • Loading branch information
gempain committed Dec 7, 2020
1 parent 5f4945b commit 050268f
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 47 deletions.
38 changes: 0 additions & 38 deletions src/caddy/config/errors.ts

This file was deleted.

11 changes: 4 additions & 7 deletions src/caddy/config/generate-site-routes.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import {
AcmeSslConfiguration, Site, SiteDomain,
} from '../../entities/sites/site';
import { AcmeSslConfiguration, Site, SiteDomain } from '../../entities/sites/site';
import { env } from '../../env';
import { unique } from '../../utils/arrays-utils';
import { RedirectType, ReverseProxyRedirectConfig } from '../../entities/sites/redirect';
import { relative, resolve } from 'path';
import {
getBranchFilesDir, getBranchStaticDir, getFileRedirectFileName,
} from '../../entities/sites/get-site-dir';
import { getBranchFilesDir, getFileRedirectFileName } from '../../entities/sites/get-site-dir';
import { URL } from 'url';
import { getReverseProxyDial } from '../utils/get-reverse-proxy-dial';
import { BranchPassword } from '../../entities/sites/branch';
import { base64Encode } from '../../commons/utils/base64';
import { getBranchDirInCaddy } from '../utils/get-branch-dir-in-caddy';

const sitesUrl = new URL(env.MELI_SITES_URL);

Expand Down Expand Up @@ -94,7 +91,7 @@ export function generateSiteRoutes(site: Site): any[] {
}],
handle: [{
handler: 'file_server',
root: resolve(env.MELI_CADDY_DIR, relative(env.MELI_SITES_DIR, getBranchStaticDir(site._id, branch))),
root: getBranchDirInCaddy(site._id, branch),
}],
terminal: true,
},
Expand Down
44 changes: 44 additions & 0 deletions src/caddy/config/get-error-routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { env } from '../../env';
import { getReverseProxyDial } from '../utils/get-reverse-proxy-dial';
import { Site } from '../../entities/sites/site';
import { getSiteErrorRoutes } from './get-site-error-routes';

export function getErrorRoutes(sites: Site[]) {
return {
routes: [
// https://caddy.community/t/v2-need-help-with-the-json-configuration-for-custom-404-and-redirects/7059/13
{
handle: [{
handler: 'vars',
status_code: '{http.error.status_code}',
}],
},
...sites.flatMap(getSiteErrorRoutes),
// fallback
{
match: [{
vars: {
status_code: '404',
},
}],
handle: [
{
handler: 'rewrite',
uri: '/static/404.html',
},
// https://caddyserver.com/docs/json/apps/http/servers/routes/handle/reverse_proxy/
{
handler: 'reverse_proxy',
upstreams: [{
dial: getReverseProxyDial(env.MELI_URL_INTERNAL.toString()),
}],
handle_response: [{
status_code: '{http.error.status_code}',
}],
},
],
terminal: true,
},
],
};
}
44 changes: 44 additions & 0 deletions src/caddy/config/get-site-error-routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Site } from '../../entities/sites/site';
import { URL } from 'url';
import { env } from '../../env';
import { getBranchDirInCaddy } from '../utils/get-branch-dir-in-caddy';

const sitesUrl = new URL(env.MELI_SITES_URL);

export function getSiteErrorRoutes(site: Site) {
return site.branches.map(branch => {
const hosts = [
`${branch.slug}.${site.name}.${sitesUrl.host}`,
...site.domains.map(domain => `${branch.slug}.${domain.name}`),
];
if (site.mainBranch === branch._id) {
hosts.push(`${site.name}.${sitesUrl.host}`);
hosts.push(...site.domains.map(domain => domain.name));
}
const branchDir = getBranchDirInCaddy(site._id, branch);
return getErrorRoute(hosts, branchDir);
});
}

function getErrorRoute(hosts: string[], rootDir: string) {
return {
match: [{
vars: {
status_code: '404',
},
host: hosts,
}],
handle: [
{
handler: 'rewrite',
uri: '/404.html',
},
// https://caddyserver.com/docs/json/apps/http/servers/routes/handle/reverse_proxy/
{
handler: 'file_server',
root: rootDir,
},
],
terminal: true,
};
}
4 changes: 2 additions & 2 deletions src/caddy/generate-config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Sites } from '../entities/sites/site';
import { env } from '../env';
import { generateSiteRoutes } from './config/generate-site-routes';
import { errors } from './config/errors';
import { getErrorRoutes } from './config/get-error-routes';
import { generateManualCertificatesConfig, generateServerTlsConfig } from './config/ssl';
import { uiRoute } from './config/ui-route';
import { apiRoute } from './config/api-route';
Expand Down Expand Up @@ -35,7 +35,7 @@ export async function generateConfig(): Promise<any> {
uiRoute,
...sites.flatMap(generateSiteRoutes),
],
errors,
errors: getErrorRoutes(sites),
...(sslDisabled ? [] : generateServerTlsConfig(sites)),
},
},
Expand Down
9 changes: 9 additions & 0 deletions src/caddy/utils/get-branch-dir-in-caddy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { relative, resolve } from 'path';
import { env } from '../../env';
import { getBranchStaticDir } from '../../entities/sites/get-site-dir';
import { Branch } from '../../entities/sites/branch';

export function getBranchDirInCaddy(siteId: string, branch: Branch) {
const branchStaticDir = getBranchStaticDir(siteId, branch);
return resolve(env.MELI_CADDY_DIR, relative(env.MELI_SITES_DIR, branchStaticDir));
}

0 comments on commit 050268f

Please sign in to comment.