refactor(routes): Phase 3.6 - standardize sites.js

This commit is contained in:
Krystie
2026-03-29 20:16:08 -07:00
parent cabcbcf98a
commit ac23b2e093
2 changed files with 61 additions and 38 deletions

View File

@@ -1,29 +1,43 @@
const express = require('express'); const express = require('express');
const fs = require('fs'); const fs = require('fs');
const { CADDY, REGEX, LIMITS } = require('../constants'); const { CADDY, REGEX, LIMITS } = require('../constants');
const { ValidationError } = require('../errors'); const { ValidationError, ConflictError, NotFoundError } = require('../errors');
const { validateURL } = require('../input-validator');
module.exports = function(ctx) { /**
* Sites route factory
* @param {Object} deps - Explicit dependencies
* @param {Function} deps.asyncHandler - Async route handler wrapper
* @param {Object} deps.caddy - Caddy manager
* @param {Object} deps.dns - DNS manager
* @param {Function} deps.fetchT - Fetch with timeout
* @param {Function} deps.buildDomain - Domain builder function
* @param {Function} deps.addServiceToConfig - Service config adder
* @param {Object} deps.siteConfig - Site configuration
* @param {Object} deps.log - Logger instance
* @returns {express.Router}
*/
module.exports = function({ asyncHandler, caddy, dns, fetchT, buildDomain, addServiceToConfig, siteConfig, log }) {
const router = express.Router(); const router = express.Router();
// Get Caddyfile contents // Get Caddyfile contents
router.get('/caddyfile', ctx.asyncHandler(async (req, res) => { router.get('/caddyfile', asyncHandler(async (req, res) => {
const content = await ctx.caddy.read(); const content = await caddy.read();
res.json({ success: true, content }); res.json({ success: true, content });
}, 'caddyfile-get')); }, 'caddyfile-get'));
// Get current Caddy config (from admin API) // Get current Caddy config (from admin API)
router.get('/caddy/config', ctx.asyncHandler(async (req, res) => { router.get('/caddy/config', asyncHandler(async (req, res) => {
const response = await ctx.fetchT(`${ctx.caddy.adminUrl}/config/`); const response = await fetchT(`${caddy.adminUrl}/config/`);
const config = await response.json(); const config = await response.json();
res.json({ success: true, config }); res.json({ success: true, config });
}, 'caddy-config')); }, 'caddy-config'));
// Reload Caddy configuration via admin API // Reload Caddy configuration via admin API
router.post('/caddy/reload', ctx.asyncHandler(async (req, res) => { router.post('/caddy/reload', asyncHandler(async (req, res) => {
const caddyfileContent = await ctx.caddy.read(); const caddyfileContent = await caddy.read();
const response = await ctx.fetchT(`${ctx.caddy.adminUrl}/load`, { const response = await fetchT(`${caddy.adminUrl}/load`, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': CADDY.CONTENT_TYPE }, headers: { 'Content-Type': CADDY.CONTENT_TYPE },
body: caddyfileContent body: caddyfileContent
@@ -31,16 +45,16 @@ module.exports = function(ctx) {
if (!response.ok) { if (!response.ok) {
const errorText = await response.text(); const errorText = await response.text();
ctx.log.error('caddy', 'Caddy reload failed', { error: errorText }); log.error('caddy', 'Caddy reload failed', { error: errorText });
return ctx.errorResponse(res, 500, '[DC-303] Caddy reload failed. Check server logs for details.'); throw new Error('Caddy reload failed. Check server logs for details.');
} }
res.json({ success: true, message: 'Caddy configuration reloaded successfully' }); res.json({ success: true, message: 'Caddy configuration reloaded successfully' });
}, 'caddy-reload')); }, 'caddy-reload'));
// Get Certificate Authorities from Caddyfile // Get Certificate Authorities from Caddyfile
router.get('/caddy/cas', ctx.asyncHandler(async (req, res) => { router.get('/caddy/cas', asyncHandler(async (req, res) => {
const content = await ctx.caddy.read(); const content = await caddy.read();
const cas = []; const cas = [];
const pkiRegex = /pki\s*\{([^}]*(?:\{[^}]*\}[^}]*)*)\}/gs; const pkiRegex = /pki\s*\{([^}]*(?:\{[^}]*\}[^}]*)*)\}/gs;
@@ -117,11 +131,11 @@ module.exports = function(ctx) {
}, 'caddy-get-cas')); }, 'caddy-get-cas'));
// Remove a site from Caddyfile // Remove a site from Caddyfile
router.delete('/site/:domain', ctx.asyncHandler(async (req, res) => { router.delete('/site/:domain', asyncHandler(async (req, res) => {
const { domain } = req.params; const { domain } = req.params;
if (!domain) throw new ValidationError('Domain is required'); if (!domain) throw new ValidationError('Domain is required');
const result = await ctx.caddy.modify((content) => { const result = await caddy.modify((content) => {
const escapedDomain = domain.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); const escapedDomain = domain.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const siteBlockRegex = new RegExp( const siteBlockRegex = new RegExp(
`\\n?${escapedDomain}\\s*\\{[^{}]*(?:\\{[^{}]*(?:\\{[^{}]*\\}[^{}]*)*\\}[^{}]*)*\\}\\s*`, 'g' `\\n?${escapedDomain}\\s*\\{[^{}]*(?:\\{[^{}]*(?:\\{[^{}]*\\}[^{}]*)*\\}[^{}]*)*\\}\\s*`, 'g'
@@ -133,16 +147,16 @@ module.exports = function(ctx) {
if (!result.success) { if (!result.success) {
if (result.rolledBack) { if (result.rolledBack) {
return ctx.errorResponse(res, 500, `Removed "${domain}" but Caddy reload failed (rolled back): ${result.error}`); throw new Error( `Removed "${domain}" but Caddy reload failed (rolled back): ${result.error}`);
} }
return ctx.errorResponse(res, 404, `Site block for "${domain}" not found in Caddyfile`); throw new NotFoundError(`Site block for "" in Caddyfile`);
} }
res.json({ success: true, message: `Site "${domain}" removed from Caddyfile and Caddy reloaded` }); res.json({ success: true, message: `Site "${domain}" removed from Caddyfile and Caddy reloaded` });
}, 'site-delete')); }, 'site-delete'));
// Add a new site to Caddyfile and reload // Add a new site to Caddyfile and reload
router.post('/site', ctx.asyncHandler(async (req, res) => { router.post('/site', asyncHandler(async (req, res) => {
const { domain, upstream, config } = req.body; const { domain, upstream, config } = req.body;
if (!domain || !upstream) throw new ValidationError('Domain and upstream are required'); if (!domain || !upstream) throw new ValidationError('Domain and upstream are required');
if (!REGEX.DOMAIN.test(domain)) throw new ValidationError('[DC-301] Invalid domain format'); if (!REGEX.DOMAIN.test(domain)) throw new ValidationError('[DC-301] Invalid domain format');
@@ -150,27 +164,27 @@ module.exports = function(ctx) {
const upstreamRegex = /^[a-z0-9.-]+:\d{1,5}$/i; const upstreamRegex = /^[a-z0-9.-]+:\d{1,5}$/i;
if (!upstreamRegex.test(upstream)) throw new ValidationError('Invalid upstream format. Use host:port'); if (!upstreamRegex.test(upstream)) throw new ValidationError('Invalid upstream format. Use host:port');
let content = await ctx.caddy.read(); let content = await caddy.read();
const escapedDomain = domain.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); const escapedDomain = domain.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const siteBlockRegex = new RegExp(`\\n?${escapedDomain}\\s*\\{`, 'g'); const siteBlockRegex = new RegExp(`\\n?${escapedDomain}\\s*\\{`, 'g');
if (siteBlockRegex.test(content)) { if (siteBlockRegex.test(content)) {
return ctx.errorResponse(res, 409, `[DC-302] Site block for "${domain}" already exists in Caddyfile`); throw new ConflictError(`Site block for "" already exists in Caddyfile`);
} }
// Always generate structured config — never allow raw Caddy config injection // Always generate structured config — never allow raw Caddy config injection
const newSiteBlock = `\n${domain} {\n reverse_proxy ${upstream}\n tls internal\n}\n`; const newSiteBlock = `\n${domain} {\n reverse_proxy ${upstream}\n tls internal\n}\n`;
const result = await ctx.caddy.modify(c => c + newSiteBlock); const result = await caddy.modify(c => c + newSiteBlock);
if (!result.success) { if (!result.success) {
return ctx.errorResponse(res, 500, `[DC-303] Site added to Caddyfile but reload failed: ${result.error}`, throw new Error( `[DC-303] Site added to Caddyfile but reload failed: ${result.error}`,
result.rolledBack ? { note: 'Caddyfile was rolled back to previous state' } : {}); result.rolledBack ? { note: 'Caddyfile was rolled back to previous state' } : {});
} }
ctx.ok(res, { message: `Site "${domain}" added to Caddyfile and Caddy reloaded successfully` }); res.json({ success: true, message: `Site "${domain}" added to Caddyfile and Caddy reloaded successfully` });
}, 'site-add')); }, 'site-add'));
// Add external service reverse proxy to Caddyfile // Add external service reverse proxy to Caddyfile
router.post('/site/external', ctx.asyncHandler(async (req, res) => { router.post('/site/external', asyncHandler(async (req, res) => {
const { subdomain, externalUrl, preserveHost, followRedirects, sslType, caddyfilePath, reloadCaddy: shouldReload, createDns, serviceName, logo } = req.body; const { subdomain, externalUrl, preserveHost, followRedirects, sslType, caddyfilePath, reloadCaddy: shouldReload, createDns, serviceName, logo } = req.body;
if (!subdomain || !externalUrl) { if (!subdomain || !externalUrl) {
@@ -181,22 +195,22 @@ module.exports = function(ctx) {
} }
try { try {
ctx.validateURL(externalUrl); validateURL(externalUrl);
} catch (validationErr) { } catch (validationErr) {
return ctx.errorResponse(res, 400, validationErr.message); throw new ValidationError(validationErr.message);
} }
const domain = ctx.buildDomain(subdomain); const domain = buildDomain(subdomain);
let dnsWarning = null; let dnsWarning = null;
try { try {
if (createDns) { if (createDns) {
try { try {
await ctx.dns.createRecord(subdomain, ctx.siteConfig.dnsServerIp); await dns.createRecord(subdomain, siteConfig.dnsServerIp);
ctx.log.info('dns', 'DNS record created for external proxy', { domain, ip: ctx.siteConfig.dnsServerIp }); log.info('dns', 'DNS record created for external proxy', { domain, ip: siteConfig.dnsServerIp });
} catch (dnsError) { } catch (dnsError) {
dnsWarning = `DNS creation failed: ${dnsError.message}. You may need to create the DNS record manually.`; dnsWarning = `DNS creation failed: ${dnsError.message}. You may need to create the DNS record manually.`;
ctx.log.warn('dns', 'DNS creation failed for external proxy', { domain, error: dnsError.message }); log.warn('dns', 'DNS creation failed for external proxy', { domain, error: dnsError.message });
} }
} }
@@ -221,29 +235,29 @@ module.exports = function(ctx) {
proxyConfig = `\n${domain} {\n ${sslConfig}\n\n reverse_proxy ${externalUrl} {${hostHeader}\n transport http {\n tls\n }\n }\n}\n`; proxyConfig = `\n${domain} {\n ${sslConfig}\n\n reverse_proxy ${externalUrl} {${hostHeader}\n transport http {\n tls\n }\n }\n}\n`;
} }
const caddyResult = await ctx.caddy.modify(c => { const caddyResult = await caddy.modify(c => {
const escapedDomain = domain.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); const escapedDomain = domain.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
if (new RegExp(`\\n?${escapedDomain}\\s*\\{`, 'g').test(c)) return null; if (new RegExp(`\\n?${escapedDomain}\\s*\\{`, 'g').test(c)) return null;
return c + proxyConfig; return c + proxyConfig;
}); });
if (!caddyResult.success && !caddyResult.rolledBack) { if (!caddyResult.success && !caddyResult.rolledBack) {
return ctx.errorResponse(res, 409, `[DC-302] Site block for "${domain}" already exists in Caddyfile`); throw new ConflictError(`Site block for "" already exists in Caddyfile`);
} }
if (!caddyResult.success) { if (!caddyResult.success) {
return ctx.errorResponse(res, 500, `[DC-303] External proxy added but Caddy reload failed (rolled back): ${caddyResult.error}`); throw new Error( `[DC-303] External proxy added but Caddy reload failed (rolled back): ${caddyResult.error}`);
} }
if (serviceName && logo) { if (serviceName && logo) {
try { try {
await ctx.addServiceToConfig({ await addServiceToConfig({
id: subdomain, name: serviceName, logo, id: subdomain, name: serviceName, logo,
isExternal: true, externalUrl, isExternal: true, externalUrl,
deployedAt: new Date().toISOString() deployedAt: new Date().toISOString()
}); });
ctx.log.info('deploy', 'Service added to dashboard', { subdomain }); log.info('deploy', 'Service added to dashboard', { subdomain });
} catch (serviceError) { } catch (serviceError) {
ctx.log.warn('deploy', 'Failed to add service to dashboard', { subdomain, error: serviceError.message }); log.warn('deploy', 'Failed to add service to dashboard', { subdomain, error: serviceError.message });
} }
} }
@@ -254,7 +268,7 @@ module.exports = function(ctx) {
if (dnsWarning) response.warning = dnsWarning; if (dnsWarning) response.warning = dnsWarning;
res.json(response); res.json(response);
} catch (error) { } catch (error) {
ctx.errorResponse(res, 500, ctx.safeErrorMessage(error)); throw error;
} }
}, 'site-external')); }, 'site-external'));

View File

@@ -358,7 +358,16 @@ async function createApp() {
SERVICES_FILE: ctx.SERVICES_FILE, SERVICES_FILE: ctx.SERVICES_FILE,
log: ctx.log log: ctx.log
})); }));
apiRouter.use(sitesRoutes(ctx)); apiRouter.use(sitesRoutes({
asyncHandler: ctx.asyncHandler,
caddy: ctx.caddy,
dns: ctx.dns,
fetchT: ctx.fetchT,
buildDomain: ctx.buildDomain,
addServiceToConfig: ctx.addServiceToConfig,
siteConfig: ctx.siteConfig,
log: ctx.log
}));
apiRouter.use(credentialsRoutes({ apiRouter.use(credentialsRoutes({
credentialManager: ctx.credentialManager, credentialManager: ctx.credentialManager,
asyncHandler: ctx.asyncHandler asyncHandler: ctx.asyncHandler