refactor: Improve configuration generation formatting in server.js for better readability and structure
Publish Fast Tabler Docker image / build-and-push-fast (push) Has been cancelled

This commit is contained in:
2025-07-21 14:04:07 +07:00
parent 8a475b38c0
commit 9ca9d3f2df
+39 -30
View File
@@ -425,43 +425,46 @@ app.get('/api/filters/generate-config', async (req, res) => {
let config = '// Frouting filter configuration for MikroTik 7.14+\n'; let config = '// Frouting filter configuration for MikroTik 7.14+\n';
config += '// Generated automatically\n'; config += '// Generated automatically\n';
config += `// Date: ${new Date().toISOString()}\n\n`; config += `// Date: ${new Date().toISOString()}\n\n`;
config += '/routing filter bgp-in-tmp {\n';
// Если есть только один gateway, создаем простую конфигурацию // Если есть только один gateway, создаем простую конфигурацию
if (Object.keys(gatewayGroups).length === 1) { if (Object.keys(gatewayGroups).length === 1) {
const [gateway, communities] = Object.entries(gatewayGroups)[0]; const [gateway, communities] = Object.entries(gatewayGroups)[0];
config += `if (\n`; config += ` if (\n`;
communities.forEach((community, index) => { communities.forEach((community, index) => {
config += `(bgp-communities includes ${community})`; config += ` (bgp-communities includes ${community})`;
if (index < communities.length - 1) { if (index < communities.length - 1) {
config += ` \nor `; config += ` or \n`;
} }
}); });
config += `\n)\n{\n set gw ${gateway}; accept;\n}\nelse\n{\n reject;\n}\n`; config += `\n )\n {\n set gw ${gateway}; accept;\n }\n else\n {\n reject;\n }\n`;
} else { } else {
// Если несколько gateway, создаем каскадную структуру // Если несколько gateway, создаем каскадную структуру
const gatewayEntries = Object.entries(gatewayGroups); const gatewayEntries = Object.entries(gatewayGroups);
gatewayEntries.forEach(([gateway, communities], gatewayIndex) => { gatewayEntries.forEach(([gateway, communities], gatewayIndex) => {
if (gatewayIndex === 0) { if (gatewayIndex === 0) {
config += `if (\n`; config += ` if (\n`;
} else { } else {
config += `else if (\n`; config += ` else if (\n`;
} }
communities.forEach((community, index) => { communities.forEach((community, index) => {
config += `(bgp-communities includes ${community})`; config += ` (bgp-communities includes ${community})`;
if (index < communities.length - 1) { if (index < communities.length - 1) {
config += ` \nor `; config += ` or \n`;
} }
}); });
config += `\n)\n{\n set gw ${gateway}; accept;\n}`; config += `\n )\n {\n set gw ${gateway}; accept;\n }`;
if (gatewayIndex === gatewayEntries.length - 1) { if (gatewayIndex === gatewayEntries.length - 1) {
config += `\nelse\n{\n reject;\n}\n`; config += `\n else\n {\n reject;\n }\n`;
} else { } else {
config += `\n`; config += `\n`;
} }
}); });
} }
config += '}\n';
res.json({ config }); res.json({ config });
} catch (error) { } catch (error) {
@@ -512,43 +515,46 @@ app.post('/api/filters/export-config', async (req, res) => {
let config = '// Frouting filter configuration for MikroTik 7.14+\n'; let config = '// Frouting filter configuration for MikroTik 7.14+\n';
config += '// Generated automatically\n'; config += '// Generated automatically\n';
config += `// Date: ${new Date().toISOString()}\n\n`; config += `// Date: ${new Date().toISOString()}\n\n`;
config += '/routing filter bgp-in-tmp {\n';
// Если есть только один gateway, создаем простую конфигурацию // Если есть только один gateway, создаем простую конфигурацию
if (Object.keys(gatewayGroups).length === 1) { if (Object.keys(gatewayGroups).length === 1) {
const [gateway, communities] = Object.entries(gatewayGroups)[0]; const [gateway, communities] = Object.entries(gatewayGroups)[0];
config += `if (\n`; config += ` if (\n`;
communities.forEach((community, index) => { communities.forEach((community, index) => {
config += `(bgp-communities includes ${community})`; config += ` (bgp-communities includes ${community})`;
if (index < communities.length - 1) { if (index < communities.length - 1) {
config += ` \nor `; config += ` or \n`;
} }
}); });
config += `\n)\n{\n set gw ${gateway}; accept;\n}\nelse\n{\n reject;\n}\n`; config += `\n )\n {\n set gw ${gateway}; accept;\n }\n else\n {\n reject;\n }\n`;
} else { } else {
// Если несколько gateway, создаем каскадную структуру // Если несколько gateway, создаем каскадную структуру
const gatewayEntries = Object.entries(gatewayGroups); const gatewayEntries = Object.entries(gatewayGroups);
gatewayEntries.forEach(([gateway, communities], gatewayIndex) => { gatewayEntries.forEach(([gateway, communities], gatewayIndex) => {
if (gatewayIndex === 0) { if (gatewayIndex === 0) {
config += `if (\n`; config += ` if (\n`;
} else { } else {
config += `else if (\n`; config += ` else if (\n`;
} }
communities.forEach((community, index) => { communities.forEach((community, index) => {
config += `(bgp-communities includes ${community})`; config += ` (bgp-communities includes ${community})`;
if (index < communities.length - 1) { if (index < communities.length - 1) {
config += ` \nor `; config += ` or \n`;
} }
}); });
config += `\n)\n{\n set gw ${gateway}; accept;\n}`; config += `\n )\n {\n set gw ${gateway}; accept;\n }`;
if (gatewayIndex === gatewayEntries.length - 1) { if (gatewayIndex === gatewayEntries.length - 1) {
config += `\nelse\n{\n reject;\n}\n`; config += `\n else\n {\n reject;\n }\n`;
} else { } else {
config += `\n`; config += `\n`;
} }
}); });
} }
config += '}\n';
// Save configuration to S3 // Save configuration to S3
const exportParams = { const exportParams = {
@@ -788,43 +794,46 @@ app.post('/api/server-filters/generate-config', async (req, res) => {
let config = '// Frouting filter configuration for MikroTik 7.14+\n'; let config = '// Frouting filter configuration for MikroTik 7.14+\n';
config += '// Generated automatically\n'; config += '// Generated automatically\n';
config += `// Date: ${new Date().toISOString()}\n\n`; config += `// Date: ${new Date().toISOString()}\n\n`;
config += '/routing filter bgp-in-tmp {\n';
// Если есть только один gateway, создаем простую конфигурацию // Если есть только один gateway, создаем простую конфигурацию
if (Object.keys(gatewayGroups).length === 1) { if (Object.keys(gatewayGroups).length === 1) {
const [gateway, communities] = Object.entries(gatewayGroups)[0]; const [gateway, communities] = Object.entries(gatewayGroups)[0];
config += `if (\n`; config += ` if (\n`;
communities.forEach((community, index) => { communities.forEach((community, index) => {
config += `(bgp-communities includes ${community})`; config += ` (bgp-communities includes ${community})`;
if (index < communities.length - 1) { if (index < communities.length - 1) {
config += ` \nor `; config += ` or \n`;
} }
}); });
config += `\n)\n{\n set gw ${gateway}; accept;\n}\nelse\n{\n reject;\n}\n`; config += `\n )\n {\n set gw ${gateway}; accept;\n }\n else\n {\n reject;\n }\n`;
} else { } else {
// Если несколько gateway, создаем каскадную структуру // Если несколько gateway, создаем каскадную структуру
const gatewayEntries = Object.entries(gatewayGroups); const gatewayEntries = Object.entries(gatewayGroups);
gatewayEntries.forEach(([gateway, communities], gatewayIndex) => { gatewayEntries.forEach(([gateway, communities], gatewayIndex) => {
if (gatewayIndex === 0) { if (gatewayIndex === 0) {
config += `if (\n`; config += ` if (\n`;
} else { } else {
config += `else if (\n`; config += ` else if (\n`;
} }
communities.forEach((community, index) => { communities.forEach((community, index) => {
config += `(bgp-communities includes ${community})`; config += ` (bgp-communities includes ${community})`;
if (index < communities.length - 1) { if (index < communities.length - 1) {
config += ` \nor `; config += ` or \n`;
} }
}); });
config += `\n)\n{\n set gw ${gateway}; accept;\n}`; config += `\n )\n {\n set gw ${gateway}; accept;\n }`;
if (gatewayIndex === gatewayEntries.length - 1) { if (gatewayIndex === gatewayEntries.length - 1) {
config += `\nelse\n{\n reject;\n}\n`; config += `\n else\n {\n reject;\n }\n`;
} else { } else {
config += `\n`; config += `\n`;
} }
}); });
} }
config += '}\n';
console.log('Generated config:', config); console.log('Generated config:', config);
console.log('Sending response:', { config }); console.log('Sending response:', { config });