🎨 style: auto format

This commit is contained in:
2026-01-16 12:32:36 +07:00
parent 5a6d3da26d
commit 98b685fe1b
18 changed files with 743 additions and 703 deletions
@@ -1,8 +1,11 @@
import { PrismaClient } from '@minikura/db';
import type { ReverseProxyServer, CustomEnvironmentVariable } from '@minikura/db';
import { BaseController } from './base-controller';
import type { ReverseProxyConfig } from '../types';
import { createReverseProxyServer, deleteReverseProxyServer } from '../resources/reverseProxyServer';
import type { PrismaClient } from "@minikura/db";
import type { ReverseProxyServer, CustomEnvironmentVariable } from "@minikura/db";
import { BaseController } from "./base-controller";
import type { ReverseProxyConfig } from "../types";
import {
createReverseProxyServer,
deleteReverseProxyServer,
} from "../resources/reverseProxyServer";
type ReverseProxyWithEnvVars = ReverseProxyServer & {
env_variables: CustomEnvironmentVariable[];
@@ -16,7 +19,7 @@ export class ReverseProxyController extends BaseController {
}
protected getControllerName(): string {
return 'ReverseProxyController';
return "ReverseProxyController";
}
protected async syncResources(): Promise<void> {
@@ -25,31 +28,35 @@ export class ReverseProxyController extends BaseController {
const coreApi = this.k8sClient.getCoreApi();
const networkingApi = this.k8sClient.getNetworkingApi();
const proxies = await this.prisma.reverseProxyServer.findMany({
const proxies = (await this.prisma.reverseProxyServer.findMany({
include: {
env_variables: true,
}
}) as ReverseProxyWithEnvVars[];
const currentProxyIds = new Set(proxies.map(proxy => proxy.id));
},
})) as ReverseProxyWithEnvVars[];
const currentProxyIds = new Set(proxies.map((proxy) => proxy.id));
// Delete reverse proxy servers that are no longer in the database
for (const [proxyId, proxy] of this.deployedProxies.entries()) {
if (!currentProxyIds.has(proxyId)) {
console.log(`Reverse proxy server ${proxy.id} (${proxyId}) has been removed from the database, deleting from Kubernetes...`);
console.log(
`Reverse proxy server ${proxy.id} (${proxyId}) has been removed from the database, deleting from Kubernetes...`
);
await deleteReverseProxyServer(proxy.id, proxy.type, appsApi, coreApi, this.namespace);
this.deployedProxies.delete(proxyId);
}
}
// Create or update reverse proxy servers that are in the database
for (const proxy of proxies) {
const deployedProxy = this.deployedProxies.get(proxy.id);
// If proxy doesn't exist yet or has been updated
if (!deployedProxy || this.hasProxyChanged(deployedProxy, proxy)) {
console.log(`${!deployedProxy ? 'Creating' : 'Updating'} reverse proxy server ${proxy.id} (${proxy.id}) in Kubernetes...`);
console.log(
`${!deployedProxy ? "Creating" : "Updating"} reverse proxy server ${proxy.id} (${proxy.id}) in Kubernetes...`
);
const proxyConfig: ReverseProxyConfig = {
id: proxy.id,
external_address: proxy.external_address,
@@ -59,55 +66,55 @@ export class ReverseProxyController extends BaseController {
apiKey: proxy.api_key,
type: proxy.type,
memory: proxy.memory,
env_variables: proxy.env_variables?.map(ev => ({
env_variables: proxy.env_variables?.map((ev) => ({
key: ev.key,
value: ev.value
}))
value: ev.value,
})),
};
await createReverseProxyServer(
proxyConfig,
appsApi,
coreApi,
networkingApi,
proxyConfig,
appsApi,
coreApi,
networkingApi,
this.namespace
);
// Update cache
this.deployedProxies.set(proxy.id, { ...proxy });
}
}
} catch (error) {
console.error('Error syncing reverse proxy servers:', error);
console.error("Error syncing reverse proxy servers:", error);
throw error;
}
}
private hasProxyChanged(
oldProxy: ReverseProxyWithEnvVars,
oldProxy: ReverseProxyWithEnvVars,
newProxy: ReverseProxyWithEnvVars
): boolean {
// Check basic properties
const basicPropsChanged =
const basicPropsChanged =
oldProxy.external_address !== newProxy.external_address ||
oldProxy.external_port !== newProxy.external_port ||
oldProxy.listen_port !== newProxy.listen_port ||
oldProxy.description !== newProxy.description;
if (basicPropsChanged) return true;
// Check if environment variables have changed
const oldEnvVars = oldProxy.env_variables || [];
const newEnvVars = newProxy.env_variables || [];
if (oldEnvVars.length !== newEnvVars.length) return true;
for (const newEnv of newEnvVars) {
const oldEnv = oldEnvVars.find(e => e.key === newEnv.key);
const oldEnv = oldEnvVars.find((e) => e.key === newEnv.key);
if (!oldEnv || oldEnv.value !== newEnv.value) {
return true;
}
}
return false;
}
}
}