Custom locations bugfix

This commit is contained in:
Laszlo Ancsin 2019-02-19 13:14:48 +01:00
parent b04e9fa04d
commit f44f635e5c
5 changed files with 31 additions and 11 deletions

View file

@ -127,20 +127,28 @@ const internalNginx = {
return '/data/nginx/' + host_type + '/' + host_id + '.conf'; return '/data/nginx/' + host_type + '/' + host_id + '.conf';
}, },
/**
* Generates custom locations
* @param {Object} host
* @returns {Promise}
*/
renderLocations: (host) => { renderLocations: (host) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const tpl = ` let template;
location {{ path }} {
proxy_pass {{ forward_scheme }}://{{ forward_host }}:{{ forward_port: }}; try {
{{ advanced_config }} template = fs.readFileSync(__dirname + '/../templates/_location.conf', {encoding: 'utf8'});
} } catch (err) {
`; reject(new error.ConfigurationError(err.message));
return;
}
let renderer = new Liquid(); let renderer = new Liquid();
let renderedLocations = ''; let renderedLocations = '';
const locationRendering = async () => { const locationRendering = async () => {
for (let i = 0; i < host.locations.length; i++) { for (let i = 0; i < host.locations.length; i++) {
renderedLocations += await renderer.parseAndRender(tpl, host.locations[i]); renderedLocations += await renderer.parseAndRender(template, host.locations[i]);
} }
} }
@ -175,8 +183,10 @@ const internalNginx = {
} }
let locationsPromise; let locationsPromise;
let origLocations;
if (host.locations) { if (host.locations) {
origLocations = [].concat(host.locations);
locationsPromise = internalNginx.renderLocations(host).then((renderedLocations) => { locationsPromise = internalNginx.renderLocations(host).then((renderedLocations) => {
host.locations = renderedLocations; host.locations = renderedLocations;
}); });
@ -194,6 +204,9 @@ const internalNginx = {
logger.success('Wrote config:', filename, config_text); logger.success('Wrote config:', filename, config_text);
} }
// Restore locations array
host.locations = origLocations;
resolve(true); resolve(true);
}) })
.catch(err => { .catch(err => {

View file

@ -26,9 +26,6 @@ class ProxyHost extends Model {
this.meta = {}; this.meta = {};
} }
// Serialize custom locations
this.locations = JSON.stringify(this.locations);
this.domain_names.sort(); this.domain_names.sort();
} }

View file

@ -0,0 +1,8 @@
location {{ path }} {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Scheme $scheme;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass {{ forward_scheme }}://{{ forward_host }}:{{ forward_port: }};
{{ advanced_config }}
}

View file

@ -17,7 +17,7 @@
<div class="tab-pane" id="locations"> <div class="tab-pane" id="locations">
<div class="row"> <div class="row">
<div class="col-sm-12"> <div class="col-sm-12">
<button type="button" class="btn btn-secondary add_location">Add location</button> <button type="button" class="btn btn-secondary add_location"><%- i18n('locations', 'new_location') %></button>
<div class="locations_container mt-3"></div> <div class="locations_container mt-3"></div>
</div> </div>
</div> </div>

View file

@ -240,6 +240,8 @@ module.exports = Mn.View.extend({
this.model = new ProxyHostModel.Model(); this.model = new ProxyHostModel.Model();
} }
this.locationsCollection = new ProxyLocationModel.Collection();
// Custom locations // Custom locations
this.showChildView('locations_regions', new CustomLocation.LocationCollectionView({ this.showChildView('locations_regions', new CustomLocation.LocationCollectionView({
collection: this.locationsCollection collection: this.locationsCollection