Skip to content

Commit

Permalink
fix: caddy config uses site url instead of host
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoffroy Empain committed Dec 4, 2020
1 parent dd16fc5 commit 7649c3b
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/caddy/config/fallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export const fallback = {
host: [
sitesUrl,
`*.${sitesUrl}`,
// TODO workaround we find how to make a generic fallback
`*.*.${sitesUrl}`,
],
}],
handle: [
Expand Down
4 changes: 2 additions & 2 deletions src/caddy/config/generate-site-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function generateSiteRoutes(site: Site): any[] {
const domains: SiteDomain[] = [
...(site.domains || []),
{
name: `${site.name}.${sitesUrl}`,
name: `${site.name}.${sitesUrl.host}`,
sslConfiguration: {
type: 'acme',
} as AcmeSslConfiguration,
Expand Down Expand Up @@ -78,7 +78,7 @@ export function generateSiteRoutes(site: Site): any[] {
headers: {
request: {
add: {
host: [new URL((redirect.config as ReverseProxyRedirectConfig).url)],
host: [new URL((redirect.config as ReverseProxyRedirectConfig).url).host],
'X-Proxied-By': ['Meli/Caddy'],
},
},
Expand Down
4 changes: 2 additions & 2 deletions src/caddy/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export async function configureSiteBranchInCaddy(site: Site, branch: Branch): Pr
await configureCaddy();
}

export async function removeSiteBranchFromCaddy(siteId: string, channelName: string): Promise<void> {
logger.debug(`Reconfigured site branch ${siteId}:${channelName} from Caddy`);
export async function removeSiteBranchFromCaddy(site: Site, branch: Branch): Promise<void> {
logger.debug(`Reconfigured site branch ${site.name}:${branch.name} from Caddy`);
// TODO reconfigure branch only (or use another system so we don't have to reconfigure anything on publish)
await configureCaddy();
}
3 changes: 2 additions & 1 deletion 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_HOST);

Expand Down Expand Up @@ -34,7 +35,7 @@ export async function generateConfig(): Promise<any> {
apiRoute,
uiRoute,
...sites.flatMap(generateSiteRoutes),
// fallback,
fallback,
],
errors,
...(sslDisabled ? [] : generateServerTlsConfig(sites)),
Expand Down
2 changes: 1 addition & 1 deletion src/entities/sites/get-branch-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import { URL } from 'url';
const sitesUrl = new URL(env.MELI_SITES_HOST);

export function getBranchUrl(site: Site, branch: Branch) {
return `${sitesUrl.protocol}//${branch.name}.${site.name}.${sitesUrl}`;
return `${sitesUrl.protocol}//${branch.name}.${site.name}.${sitesUrl.host}`;
}
2 changes: 1 addition & 1 deletion src/entities/sites/get-site-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import { URL } from 'url';
const sitesUrl = new URL(env.MELI_SITES_HOST);

export function getSiteUrl(site: Site) {
return `${sitesUrl.protocol}//${site.name}.${sitesUrl}`;
return `${sitesUrl.protocol}//${site.name}.${sitesUrl.host}`;
}
3 changes: 3 additions & 0 deletions src/entities/sites/handlers/branches/add-branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { EventType } from '../../../../events/app-event';
import { $channelName, Branch } from '../../branch';
import { uuid } from '../../../../utils/uuid';
import slugify from 'slugify';
import { configureSiteBranchInCaddy } from '../../../../caddy/configuration';

const validators = [
body(object({
Expand Down Expand Up @@ -70,6 +71,8 @@ async function handler(req: Request, res: Response): Promise<void> {
_id: siteId,
});

await configureSiteBranchInCaddy(site, branch);

emitEvent(EventType.site_branch_added, {
site,
branch,
Expand Down
3 changes: 3 additions & 0 deletions src/entities/sites/handlers/branches/delete-branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { promises } from 'fs';
import { canAdminSiteGuard } from '../../guards/can-admin-site-guard';
import { EventType } from '../../../../events/app-event';
import { branchExistsGuard } from '../../guards/branch-exists-guard';
import { removeSiteBranchFromCaddy } from '../../../../caddy/configuration';

const validators = [
params(object({
Expand Down Expand Up @@ -60,6 +61,8 @@ async function handler(req: Request, res: Response): Promise<void> {
},
});

await removeSiteBranchFromCaddy(site, branch);

emitEvent(EventType.site_branch_deleted, {
site,
branch,
Expand Down
3 changes: 3 additions & 0 deletions src/entities/sites/handlers/branches/set-branch-release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { branchExistsGuard } from '../../guards/branch-exists-guard';
import { params } from '../../../../commons/express-joi/params';
import { serializeBranch } from '../../serialize-branch';
import { linkBranchToRelease } from '../../link-branch-to-release';
import { configureSiteBranchInCaddy } from '../../../../caddy/configuration';

const validators = [
params(object({
Expand Down Expand Up @@ -61,6 +62,8 @@ async function handler(req: Request, res: Response): Promise<void> {

await linkBranchToRelease(site, branch, release);

await configureSiteBranchInCaddy(site, branch);

emitEvent(EventType.site_branch_release_set, {
site,
branch,
Expand Down
3 changes: 3 additions & 0 deletions src/entities/sites/handlers/branches/update-branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { BadRequestError } from '../../../../commons/errors/bad-request-error';
import { canAdminSiteGuard } from '../../guards/can-admin-site-guard';
import { Sites } from '../../site';
import { serializeBranch } from '../../serialize-branch';
import { configureSiteBranchInCaddy } from '../../../../caddy/configuration';

async function releaseExists(siteId: string, branchId: string): Promise<boolean> {
const count = await Sites().countDocuments({
Expand Down Expand Up @@ -49,6 +50,8 @@ async function handler(req: Request, res: Response): Promise<void> {
});
const branch = site.branches.find(brch => brch._id === branchId);

await configureSiteBranchInCaddy(site, branch);

emitEvent(EventType.site_updated, {
site,
});
Expand Down
6 changes: 3 additions & 3 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ const envSpec: EnvSpec<Env> = {
MELI_UI_HOST_INTERNAL: {
schema: string().optional().custom(isUrl).default(process.env.MELI_HOST),
},
MELI_UI_DIR: {
schema: string().optional(),
},
MELI_SITES_HOST: {
schema: string().optional().custom(isUrl).default(process.env.MELI_HOST),
},
MELI_UI_DIR: {
schema: string().optional(),
},
MELI_JWT_SECRET: {
schema: string(),
},
Expand Down

0 comments on commit 7649c3b

Please sign in to comment.