Fix Traefik route insertion placing routes in middlewares section

The route insertion logic split on `  services:` which placed new
router blocks after the middlewares section instead of in the routers
section. Now splits on `  middlewares:` first to insert routers in
the correct position. Also generates proper YAML format with HTTP
redirect routers for each new route.

Fixed live custom.yml: moved racker + timer routes to routers section.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-27 11:29:55 -06:00
parent feec35ffce
commit 5119c94b37
2 changed files with 253 additions and 693 deletions

View File

@@ -179,10 +179,19 @@ router.post('/routes', wrap(async (req) => {
const routerBlock = [
` ${routeName}:`,
` rule: "Host(\`${domain}\`)"`,
` entryPoints:`,
` - https`,
` service: ${routeName}`,
` entryPoints: [websecure]`,
` tls:`,
` certResolver: letsencrypt`
` certResolver: letsencrypt`,
``,
` ${routeName}-http:`,
` rule: "Host(\`${domain}\`)"`,
` entryPoints:`,
` - http`,
` middlewares:`,
` - redirect-to-https`,
` service: ${routeName}`,
].join('\n');
const serviceBlock = [
@@ -196,9 +205,16 @@ router.post('/routes', wrap(async (req) => {
return { added: false, reason: 'route_exists' };
}
// Insert router before middlewares section, service before end of services section
let newYml;
const middlewaresSplit = current.split(' middlewares:');
const servicesSplit = current.split(' services:');
if (servicesSplit.length === 2) {
if (middlewaresSplit.length === 2 && servicesSplit.length === 2) {
// Insert router block before middlewares, service block at end of services
newYml = middlewaresSplit[0].trimEnd() + '\n\n' + routerBlock + '\n\n middlewares:' + middlewaresSplit[1];
// Now add service at the end
newYml = newYml.trimEnd() + '\n\n' + serviceBlock + '\n';
} else if (servicesSplit.length === 2) {
newYml = servicesSplit[0].trimEnd() + '\n' + routerBlock + '\n\n services:' + servicesSplit[1].trimEnd() + '\n' + serviceBlock + '\n';
} else {
newYml = current.trimEnd() + '\n' + routerBlock + '\n' + serviceBlock + '\n';
@@ -445,10 +461,19 @@ router.post('/upsert', wrap(async (req) => {
const routerBlock = [
` ${routeName}:`,
` rule: "Host(\`${appConfig.domain}\`)"`,
` entryPoints:`,
` - https`,
` service: ${routeName}`,
` entryPoints: [websecure]`,
` tls:`,
` certResolver: letsencrypt`,
``,
` ${routeName}-http:`,
` rule: "Host(\`${appConfig.domain}\`)"`,
` entryPoints:`,
` - http`,
` middlewares:`,
` - redirect-to-https`,
` service: ${routeName}`,
].join('\n');
const serviceBlock = [
` ${routeName}:`,
@@ -457,9 +482,14 @@ router.post('/upsert', wrap(async (req) => {
` - url: "http://${server.ip}:${appConfig.port}"`,
].join('\n');
// Insert router before middlewares section, service at end
let newYml;
const middlewaresSplit = currentYml.split(' middlewares:');
const servicesSplit = currentYml.split(' services:');
if (servicesSplit.length === 2) {
if (middlewaresSplit.length === 2 && servicesSplit.length === 2) {
newYml = middlewaresSplit[0].trimEnd() + '\n\n' + routerBlock + '\n\n middlewares:' + middlewaresSplit[1];
newYml = newYml.trimEnd() + '\n\n' + serviceBlock + '\n';
} else if (servicesSplit.length === 2) {
newYml = servicesSplit[0].trimEnd() + '\n' + routerBlock + '\n\n services:' + servicesSplit[1].trimEnd() + '\n' + serviceBlock + '\n';
} else {
newYml = currentYml.trimEnd() + '\n' + routerBlock + '\n' + serviceBlock + '\n';