File size: 3,064 Bytes
b80fc11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { existsSync, readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { paths } from "@dokploy/server/constants";
import { dump, load } from "js-yaml";
import type { ApplicationNested } from "../builders";
import { execAsyncRemote } from "../process/execAsync";
import { writeTraefikConfigRemote } from "./application";
import type { FileConfig } from "./file-types";

export const addMiddleware = (config: FileConfig, middlewareName: string) => {
	if (config.http?.routers) {
		const values = Object.keys(config.http.routers);

		for (const routerName of values) {
			const router = config.http.routers[routerName];

			if (router) {
				if (!router.middlewares) {
					router.middlewares = [];
				}
				if (!router.middlewares.includes(middlewareName)) {
					router.middlewares.push(middlewareName);
				}
			}
		}
	}
};

export const deleteMiddleware = (
	config: FileConfig,
	middlewareName: string,
) => {
	if (config.http?.routers) {
		const values = Object.keys(config?.http?.routers);

		for (const routerName of values) {
			const router = config.http.routers[routerName];
			if (router?.middlewares) {
				router.middlewares = router.middlewares.filter(
					(m) => m !== middlewareName,
				);
			}
		}
	}
};

export const deleteAllMiddlewares = async (application: ApplicationNested) => {
	const config = loadMiddlewares<FileConfig>();
	const { security, appName, redirects } = application;

	if (config.http?.middlewares) {
		if (security.length > 0) {
			const middlewareName = `auth-${appName}`;

			delete config.http.middlewares[middlewareName];
		}

		for (const redirect of redirects) {
			const middlewareName = `redirect-${appName}-${redirect.uniqueConfigKey}`;
			delete config.http.middlewares[middlewareName];
		}
	}

	if (application.serverId) {
		await writeTraefikConfigRemote(config, "middlewares", application.serverId);
	} else {
		writeMiddleware(config);
	}
};

export const loadMiddlewares = <T>() => {
	const { DYNAMIC_TRAEFIK_PATH } = paths();
	const configPath = join(DYNAMIC_TRAEFIK_PATH, "middlewares.yml");
	if (!existsSync(configPath)) {
		throw new Error(`File not found: ${configPath}`);
	}
	const yamlStr = readFileSync(configPath, "utf8");
	const config = load(yamlStr) as T;
	return config;
};

export const loadRemoteMiddlewares = async (serverId: string) => {
	const { DYNAMIC_TRAEFIK_PATH } = paths(true);
	const configPath = join(DYNAMIC_TRAEFIK_PATH, "middlewares.yml");

	try {
		const { stdout, stderr } = await execAsyncRemote(
			serverId,
			`cat ${configPath}`,
		);

		if (stderr) {
			console.error(`Error: ${stderr}`);
			throw new Error(`File not found: ${configPath}`);
		}
		const config = load(stdout) as FileConfig;
		return config;
	} catch (_) {
		throw new Error(`File not found: ${configPath}`);
	}
};
export const writeMiddleware = <T>(config: T) => {
	const { DYNAMIC_TRAEFIK_PATH } = paths();
	const configPath = join(DYNAMIC_TRAEFIK_PATH, "middlewares.yml");
	const newYamlContent = dump(config);
	writeFileSync(configPath, newYamlContent, "utf8");
};