Skip to content

Commit 133d66c

Browse files
authored
Default Site customisation and new Settings space (#91)
1 parent 6f1d38a commit 133d66c

File tree

31 files changed

+893
-15
lines changed

31 files changed

+893
-15
lines changed

rootfs/etc/nginx/conf.d/default.conf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ server {
2222
}
2323
}
2424

25-
# Default 80 Host, which shows a "You are not configured" page
25+
# "You are not configured" page, which is the default if another default doesn't exist
2626
server {
27-
listen 80 default;
28-
server_name localhost;
27+
listen 80;
28+
server_name localhost-nginx-proxy-manager;
2929

3030
access_log /data/logs/default.log proxy;
3131

@@ -38,9 +38,9 @@ server {
3838
}
3939
}
4040

41-
# Default 443 Host
41+
# First 443 Host, which is the default if another default doesn't exist
4242
server {
43-
listen 443 ssl default;
43+
listen 443 ssl;
4444
server_name localhost;
4545

4646
access_log /data/logs/default.log proxy;

rootfs/etc/nginx/nginx.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ http {
7070

7171
# Files generated by NPM
7272
include /etc/nginx/conf.d/*.conf;
73+
include /data/nginx/default_host/*.conf;
7374
include /data/nginx/proxy_host/*.conf;
7475
include /data/nginx/redirection_host/*.conf;
7576
include /data/nginx/dead_host/*.conf;

rootfs/etc/services.d/nginx/run

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ mkdir -p /tmp/nginx/body \
77
/data/custom_ssl \
88
/data/logs \
99
/data/access \
10+
/data/nginx/default_host \
11+
/data/nginx/default_www \
1012
/data/nginx/proxy_host \
1113
/data/nginx/redirection_host \
1214
/data/nginx/stream \

src/backend/internal/nginx.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ const internalNginx = {
1717
* - IF BAD: update the meta with offline status and remove the config entirely
1818
* - then reload nginx
1919
*
20-
* @param {Object} model
21-
* @param {String} host_type
22-
* @param {Object} host
20+
* @param {Object|String} model
21+
* @param {String} host_type
22+
* @param {Object} host
2323
* @returns {Promise}
2424
*/
2525
configure: (model, host_type, host) => {
@@ -122,6 +122,11 @@ const internalNginx = {
122122
*/
123123
getConfigName: (host_type, host_id) => {
124124
host_type = host_type.replace(new RegExp('-', 'g'), '_');
125+
126+
if (host_type === 'default') {
127+
return '/data/nginx/default_host/site.conf';
128+
}
129+
125130
return '/data/nginx/' + host_type + '/' + host_id + '.conf';
126131
},
127132

@@ -153,9 +158,11 @@ const internalNginx = {
153158
}
154159

155160
// Manipulate the data a bit before sending it to the template
156-
host.use_default_location = true;
157-
if (typeof host.advanced_config !== 'undefined' && host.advanced_config) {
158-
host.use_default_location = !internalNginx.advancedConfigHasDefaultLocation(host.advanced_config);
161+
if (host_type !== 'default') {
162+
host.use_default_location = true;
163+
if (typeof host.advanced_config !== 'undefined' && host.advanced_config) {
164+
host.use_default_location = !internalNginx.advancedConfigHasDefaultLocation(host.advanced_config);
165+
}
159166
}
160167

161168
renderEngine
@@ -260,7 +267,7 @@ const internalNginx = {
260267

261268
/**
262269
* @param {String} host_type
263-
* @param {Object} host
270+
* @param {Object} [host]
264271
* @param {Boolean} [throw_errors]
265272
* @returns {Promise}
266273
*/
@@ -269,7 +276,7 @@ const internalNginx = {
269276

270277
return new Promise((resolve, reject) => {
271278
try {
272-
let config_file = internalNginx.getConfigName(host_type, host.id);
279+
let config_file = internalNginx.getConfigName(host_type, typeof host === 'undefined' ? 0 : host.id);
273280

274281
if (debug_mode) {
275282
logger.warn('Deleting nginx config: ' + config_file);

src/backend/internal/proxy-host.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ const internalProxyHost = {
108108
*/
109109
update: (access, data) => {
110110
let create_certificate = data.certificate_id === 'new';
111-
console.log('PH UPDATE:', data);
111+
112112
if (create_certificate) {
113113
delete data.certificate_id;
114114
}

src/backend/internal/setting.js

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
const fs = require('fs');
2+
const error = require('../lib/error');
3+
const settingModel = require('../models/setting');
4+
const internalNginx = require('./nginx');
5+
6+
const internalSetting = {
7+
8+
/**
9+
* @param {Access} access
10+
* @param {Object} data
11+
* @param {String} data.id
12+
* @return {Promise}
13+
*/
14+
update: (access, data) => {
15+
return access.can('settings:update', data.id)
16+
.then(access_data => {
17+
return internalSetting.get(access, {id: data.id});
18+
})
19+
.then(row => {
20+
if (row.id !== data.id) {
21+
// Sanity check that something crazy hasn't happened
22+
throw new error.InternalValidationError('Setting could not be updated, IDs do not match: ' + row.id + ' !== ' + data.id);
23+
}
24+
25+
return settingModel
26+
.query()
27+
.where({id: data.id})
28+
.patch(data);
29+
})
30+
.then(() => {
31+
return internalSetting.get(access, {
32+
id: data.id
33+
});
34+
})
35+
.then(row => {
36+
if (row.id === 'default-site') {
37+
// write the html if we need to
38+
if (row.value === 'html') {
39+
fs.writeFileSync('/data/nginx/default_www/index.html', row.meta.html, {encoding: 'utf8'});
40+
}
41+
42+
// Configure nginx
43+
return internalNginx.deleteConfig('default')
44+
.then(() => {
45+
return internalNginx.generateConfig('default', row);
46+
})
47+
.then(() => {
48+
return internalNginx.test();
49+
})
50+
.then(() => {
51+
return internalNginx.reload();
52+
})
53+
.then(() => {
54+
return row;
55+
})
56+
.catch((err) => {
57+
internalNginx.deleteConfig('default')
58+
.then(() => {
59+
return internalNginx.test();
60+
})
61+
.then(() => {
62+
return internalNginx.reload();
63+
})
64+
.then(() => {
65+
// I'm being slack here I know..
66+
throw new error.ValidationError('Could not reconfigure Nginx. Please check logs.');
67+
})
68+
});
69+
} else {
70+
return row;
71+
}
72+
});
73+
},
74+
75+
/**
76+
* @param {Access} access
77+
* @param {Object} data
78+
* @param {String} data.id
79+
* @return {Promise}
80+
*/
81+
get: (access, data) => {
82+
return access.can('settings:get', data.id)
83+
.then(() => {
84+
return settingModel
85+
.query()
86+
.where('id', data.id)
87+
.first();
88+
})
89+
.then(row => {
90+
if (row) {
91+
return row;
92+
} else {
93+
throw new error.ItemNotFoundError(data.id);
94+
}
95+
});
96+
},
97+
98+
/**
99+
* This will only count the settings
100+
*
101+
* @param {Access} access
102+
* @returns {*}
103+
*/
104+
getCount: (access) => {
105+
return access.can('settings:list')
106+
.then(() => {
107+
return settingModel
108+
.query()
109+
.count('id as count')
110+
.first();
111+
})
112+
.then(row => {
113+
return parseInt(row.count, 10);
114+
});
115+
},
116+
117+
/**
118+
* All settings
119+
*
120+
* @param {Access} access
121+
* @returns {Promise}
122+
*/
123+
getAll: (access) => {
124+
return access.can('settings:list')
125+
.then(() => {
126+
return settingModel
127+
.query()
128+
.orderBy('description', 'ASC');
129+
});
130+
}
131+
};
132+
133+
module.exports = internalSetting;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"anyOf": [
3+
{
4+
"$ref": "roles#/definitions/admin"
5+
}
6+
]
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"anyOf": [
3+
{
4+
"$ref": "roles#/definitions/admin"
5+
}
6+
]
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"anyOf": [
3+
{
4+
"$ref": "roles#/definitions/admin"
5+
}
6+
]
7+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const migrate_name = 'settings';
2+
const logger = require('../logger').migrate;
3+
4+
/**
5+
* Migrate
6+
*
7+
* @see http://knexjs.org/#Schema
8+
*
9+
* @param {Object} knex
10+
* @param {Promise} Promise
11+
* @returns {Promise}
12+
*/
13+
exports.up = function (knex/*, Promise*/) {
14+
logger.info('[' + migrate_name + '] Migrating Up...');
15+
16+
return knex.schema.createTable('setting', table => {
17+
table.string('id').notNull().primary();
18+
table.string('name', 100).notNull();
19+
table.string('description', 255).notNull();
20+
table.string('value', 255).notNull();
21+
table.json('meta').notNull();
22+
})
23+
.then(() => {
24+
logger.info('[' + migrate_name + '] setting Table created');
25+
26+
// TODO: add settings
27+
let settingModel = require('../models/setting');
28+
29+
return settingModel
30+
.query()
31+
.insert({
32+
id: 'default-site',
33+
name: 'Default Site',
34+
description: 'What to show when Nginx is hit with an unknown Host',
35+
value: 'congratulations',
36+
meta: {}
37+
});
38+
})
39+
.then(() => {
40+
logger.info('[' + migrate_name + '] Default settings added');
41+
});
42+
};
43+
44+
/**
45+
* Undo Migrate
46+
*
47+
* @param {Object} knex
48+
* @param {Promise} Promise
49+
* @returns {Promise}
50+
*/
51+
exports.down = function (knex, Promise) {
52+
logger.warn('[' + migrate_name + '] You can\'t migrate down the initial data.');
53+
return Promise.resolve(true);
54+
};

src/backend/models/setting.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Objection Docs:
2+
// http://vincit.github.io/objection.js/
3+
4+
const db = require('../db');
5+
const Model = require('objection').Model;
6+
7+
Model.knex(db);
8+
9+
class Setting extends Model {
10+
$beforeInsert () {
11+
// Default for meta
12+
if (typeof this.meta === 'undefined') {
13+
this.meta = {};
14+
}
15+
}
16+
17+
static get name () {
18+
return 'Setting';
19+
}
20+
21+
static get tableName () {
22+
return 'setting';
23+
}
24+
25+
static get jsonAttributes () {
26+
return ['meta'];
27+
}
28+
}
29+
30+
module.exports = Setting;

src/backend/routes/api/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ router.use('/tokens', require('./tokens'));
3131
router.use('/users', require('./users'));
3232
router.use('/audit-log', require('./audit-log'));
3333
router.use('/reports', require('./reports'));
34+
router.use('/settings', require('./settings'));
3435
router.use('/nginx/proxy-hosts', require('./nginx/proxy_hosts'));
3536
router.use('/nginx/redirection-hosts', require('./nginx/redirection_hosts'));
3637
router.use('/nginx/dead-hosts', require('./nginx/dead_hosts'));

0 commit comments

Comments
 (0)