mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-09 19:22:51 +00:00
feat: reload nginx on proxyconfig change
This commit is contained in:
14
src/cli.ts
14
src/cli.ts
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2019 StApps
|
* Copyright (C) 2022 StApps
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU Affero General Public License as
|
* it under the terms of the GNU Affero General Public License as
|
||||||
* published by the Free Software Foundation, either version 3 of the
|
* published by the Free Software Foundation, either version 3 of the
|
||||||
@@ -17,7 +17,7 @@ import {Logger} from '@openstapps/logger';
|
|||||||
import {execSync} from 'child_process';
|
import {execSync} from 'child_process';
|
||||||
import * as Dockerode from 'dockerode';
|
import * as Dockerode from 'dockerode';
|
||||||
import {render} from 'mustache';
|
import {render} from 'mustache';
|
||||||
import {asyncReadFile, asyncWriteFile, configFile} from './common';
|
import {asyncReadFile, asyncWriteFile} from './common';
|
||||||
import {getContainers, getTemplateView} from './main';
|
import {getContainers, getTemplateView} from './main';
|
||||||
|
|
||||||
// handle unhandled promise rejections
|
// handle unhandled promise rejections
|
||||||
@@ -27,6 +27,7 @@ process.on('unhandledRejection', async (error) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let containerHashCache = '';
|
let containerHashCache = '';
|
||||||
|
let configHashCache = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads the container information from the docker socket and updates the nginx config if necessary
|
* Reads the container information from the docker socket and updates the nginx config if necessary
|
||||||
@@ -40,16 +41,19 @@ async function updateNginxConfig() {
|
|||||||
})
|
})
|
||||||
.join(',');
|
.join(',');
|
||||||
|
|
||||||
|
delete require.cache[require.resolve('config')];
|
||||||
|
const configFile = require('config');
|
||||||
|
const configHash = JSON.stringify(configFile);
|
||||||
|
|
||||||
// if containers changed -> write config file, reload nginx
|
// if containers changed -> write config file, reload nginx
|
||||||
if (containerHash !== containerHashCache) {
|
if (containerHash !== containerHashCache || configHash !== configHashCache) {
|
||||||
Logger.log('Generating new NGINX configuration');
|
Logger.log('Generating new NGINX configuration');
|
||||||
|
|
||||||
// render nginx config file
|
// render nginx config file
|
||||||
const nginxConfig = render(await asyncReadFile('nginx.conf.template', 'utf8'), await getTemplateView(containers));
|
const nginxConfig = render(await asyncReadFile('nginx.conf.template', 'utf8'), await getTemplateView(containers));
|
||||||
|
|
||||||
Logger.log(`containers (${containerHash}) matched the configuration.`);
|
|
||||||
|
|
||||||
containerHashCache = containerHash;
|
containerHashCache = containerHash;
|
||||||
|
configHashCache = configHash;
|
||||||
|
|
||||||
Logger.log(`Writing new config file "${configFile.output}"`);
|
Logger.log(`Writing new config file "${configFile.output}"`);
|
||||||
|
|
||||||
|
|||||||
10
src/main.ts
10
src/main.ts
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2019 StApps
|
* Copyright (C) 2022 StApps
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU Affero General Public License as
|
* it under the terms of the GNU Affero General Public License as
|
||||||
* published by the Free Software Foundation, either version 3 of the
|
* published by the Free Software Foundation, either version 3 of the
|
||||||
@@ -21,7 +21,7 @@ import {join} from 'path';
|
|||||||
import * as semver from 'semver';
|
import * as semver from 'semver';
|
||||||
import {
|
import {
|
||||||
asyncReadFile,
|
asyncReadFile,
|
||||||
configFile,
|
ConfigFile,
|
||||||
isFileType,
|
isFileType,
|
||||||
protocolHardeningParameters,
|
protocolHardeningParameters,
|
||||||
SSLFilePaths,
|
SSLFilePaths,
|
||||||
@@ -107,7 +107,7 @@ export async function generateUpstreamMap(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (activeBackends.length !== 0) {
|
if (activeBackends.length !== 0) {
|
||||||
// not only dublicates
|
// not only duplicates
|
||||||
foundMatchingContainer = true;
|
foundMatchingContainer = true;
|
||||||
|
|
||||||
const gateWayOfContainer = await getGatewayOfStAppsBackend(activeBackends[0]);
|
const gateWayOfContainer = await getGatewayOfStAppsBackend(activeBackends[0]);
|
||||||
@@ -205,6 +205,10 @@ function generateRateLimitAllowList(entries: string[]): string {
|
|||||||
* @param containers List of container info
|
* @param containers List of container info
|
||||||
*/
|
*/
|
||||||
export async function getTemplateView(containers: Dockerode.ContainerInfo[]): Promise<TemplateView> {
|
export async function getTemplateView(containers: Dockerode.ContainerInfo[]): Promise<TemplateView> {
|
||||||
|
delete require.cache[require.resolve('config')];
|
||||||
|
const config = require('config');
|
||||||
|
const configFile = config as ConfigFile;
|
||||||
|
|
||||||
const cors = await asyncReadFile('./fixtures/cors.template', 'utf8');
|
const cors = await asyncReadFile('./fixtures/cors.template', 'utf8');
|
||||||
|
|
||||||
const visibleRoutesPromises = ['/'].map(async (route) => {
|
const visibleRoutesPromises = ['/'].map(async (route) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user