Skip to content

Commit

Permalink
fix: persist caddy config and resume it on restart
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoffroy Empain committed Dec 5, 2020
1 parent 86b3342 commit d8f01f3
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ MELI_JWT_SECRET=secret

# directories
MELI_STATIC_DIR=./build/public
MELI_SITES_DIR=./data/caddy
MELI_SITES_DIR=./data/sites

# caddy
MELI_CADDY_ADMIN_API_URL=http://localhost:2019
Expand Down
8 changes: 5 additions & 3 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ services:

caddy:
image: caddy
command: [ 'caddy', 'run', '--config', '/etc/caddy/config.json' ]
command: [ 'caddy', 'run', '--config', '/etc/caddy/config.json', '--resume' ]
ports:
- 127.0.0.1:80:80
- 127.0.0.1:443:443
- 127.0.0.1:2019:2019
volumes:
- ./data/caddy:/sites
- ./caddy/config.json:/etc/caddy/config.json:ro
- ./data/sites:/sites
- ./caddy/config.json:/etc/caddy/config.json
- ./data/caddy/data:/data
- ./data/caddy/config:/config
8 changes: 6 additions & 2 deletions src/caddy/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ const logger = new Logger('meli.server:caddy');
export async function configureCaddy(): Promise<void> {
logger.debug('Configuring Caddy...');
const config = await generateConfig();
logger.debug(JSON.stringify(config, null, 2));
await axios.post(`${env.MELI_CADDY_ADMIN_API_URL}/load`, config);
const url = `${env.MELI_CADDY_ADMIN_API_URL}/load`;
logger.debug(url, JSON.stringify(config, null, 2));
await axios.post(url, config, {
timeout: env.MELI_AXIOS_TIMEOUT,
});
logger.debug('done updating caddy config');
}

export async function configureSiteInCaddy(site: Site) {
Expand Down
2 changes: 1 addition & 1 deletion src/commons/utils/handle-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function handleError(err: any, req: Request, res: Response, next: NextFun
if (err.isAxiosError) {
logger.debug(JSON.stringify(err.toJSON(), null, 2));
const axiosError: AxiosError = err as AxiosError;
logger.debug('response', JSON.stringify(axiosError.response.data, null, 2));
logger.debug('response', JSON.stringify(axiosError.response?.data, null, 2));
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export interface Env {
MELI_BCRYPT_SALTROUNDS: number;
MELI_ACME_SERVER: string;
MELI_ACME_CA_PATH: string;
MELI_AXIOS_TIMEOUT: number;
}

const envSpec: EnvSpec<Env> = {
Expand Down Expand Up @@ -297,6 +298,10 @@ const envSpec: EnvSpec<Env> = {
MELI_ACME_CA_PATH: {
schema: string().optional(),
},
MELI_AXIOS_TIMEOUT: {
transform: stringToInt(),
schema: number().optional().default(10000),
},
};

export const env: Env = parseEnv(envSpec);

0 comments on commit d8f01f3

Please sign in to comment.