Canvas: Generate graph state out of dodo-app config

Restructure code, create shared config lib.

Change-Id: I2cf06d35c486d4557484daf8618a2c215316fa7e
diff --git a/apps/canvas/front/src/lib/config.ts b/apps/canvas/front/src/lib/config.ts
index 513f1f0..39db5b4 100644
--- a/apps/canvas/front/src/lib/config.ts
+++ b/apps/canvas/front/src/lib/config.ts
@@ -1,235 +1,5 @@
-import { AppNode, Env, Message, MessageType, NodeType, ServiceType, VolumeType } from "./state";
-
-export type AuthDisabled = {
-	enabled: false;
-};
-
-export type AuthEnabled = {
-	enabled: true;
-	groups: string[];
-	noAuthPathPatterns: string[];
-};
-
-export type Auth = AuthDisabled | AuthEnabled;
-
-export type Ingress = {
-	network: string;
-	subdomain: string;
-	port: { name: string } | { value: string };
-	auth: Auth;
-};
-
-export type Domain = {
-	network: string;
-	subdomain: string;
-};
-
-export type PortValue =
-	| {
-			name: string;
-	  }
-	| {
-			value: number;
-	  };
-
-export type PortDomain = Domain & {
-	port: PortValue;
-};
-
-export type Service = {
-	type: ServiceType;
-	name: string;
-	source: {
-		repository: string;
-		branch: string;
-		rootDir: string;
-	};
-	ports?: {
-		name: string;
-		value: number;
-		protocol: "TCP" | "UDP";
-	}[];
-	env?: {
-		name: string;
-		alias?: string;
-	}[];
-	ingress?: Ingress[];
-	expose?: PortDomain[];
-	volume?: string[];
-	preBuildCommands?: { bin: string }[];
-	dev?: {
-		enabled: boolean;
-		username?: string;
-		ssh?: Domain;
-		codeServer?: Domain;
-	};
-};
-
-export type Volume = {
-	name: string;
-	accessMode: VolumeType;
-	size: string;
-};
-
-export type PostgreSQL = {
-	name: string;
-	size: string;
-	expose?: Domain[];
-};
-
-export type MongoDB = {
-	name: string;
-	size: string;
-	expose?: Domain[];
-};
-
-export type Config = {
-	input: {
-		appId: string;
-		managerAddr: string;
-	};
-	service?: Service[];
-	volume?: Volume[];
-	postgresql?: PostgreSQL[];
-	mongodb?: MongoDB[];
-};
-
-export function generateDodoConfig(appId: string | undefined, nodes: AppNode[], env: Env): Config | null {
-	try {
-		if (appId == null || env.managerAddr == null) {
-			return null;
-		}
-		const networkMap = new Map(env.networks.map((n) => [n.domain, n.name]));
-		const ingressNodes = nodes
-			.filter((n) => n.type === "gateway-https")
-			.filter((n) => n.data.https !== undefined && !n.data.readonly);
-		const tcpNodes = nodes
-			.filter((n) => n.type === "gateway-tcp")
-			.filter((n) => n.data.exposed !== undefined && !n.data.readonly);
-		const findExpose = (n: AppNode): PortDomain[] => {
-			return n.data.ports
-				.map((p) => [n.id, p.id, p.name])
-				.flatMap((sp) => {
-					return tcpNodes.flatMap((i) =>
-						(i.data.exposed || [])
-							.filter((t) => t.serviceId === sp[0] && t.portId === sp[1])
-							.map(() => ({
-								network: networkMap.get(i.data.network!)!,
-								subdomain: i.data.subdomain!,
-								port: { name: sp[2] },
-							})),
-					);
-				});
-		};
-		return {
-			input: {
-				appId: appId,
-				managerAddr: env.managerAddr,
-			},
-			service: nodes
-				.filter((n) => n.type === "app")
-				.map((n): Service => {
-					return {
-						type: n.data.type,
-						name: n.data.label,
-						source: {
-							repository: nodes
-								.filter((i) => i.type === "github")
-								.find((i) => i.id === n.data.repository.id)!.data.repository!.sshURL,
-							branch: n.data.repository.branch,
-							rootDir: n.data.repository.rootDir,
-						},
-						ports: (n.data.ports || [])
-							.filter((p) => !n.data.dev?.enabled || (p.value != 22 && p.value != 9090))
-							.map((p) => ({
-								name: p.name,
-								value: p.value,
-								protocol: "TCP", // TODO(gio)
-							})),
-						env: (n.data.envVars || [])
-							.filter((e) => "name" in e)
-							.map((e) => ({
-								name: e.name,
-								alias: "alias" in e ? e.alias : undefined,
-							})),
-						ingress: ingressNodes
-							.filter((i) => i.data.https!.serviceId === n.id)
-							.map(
-								(i): Ingress => ({
-									network: networkMap.get(i.data.network!)!,
-									subdomain: i.data.subdomain!,
-									port: {
-										name: n.data.ports.find((p) => p.id === i.data.https!.portId)!.name,
-									},
-									auth:
-										i.data.auth?.enabled || false
-											? {
-													enabled: true,
-													groups: i.data.auth!.groups,
-													noAuthPathPatterns: i.data.auth!.noAuthPathPatterns,
-												}
-											: {
-													enabled: false,
-												},
-								}),
-							),
-						expose: findExpose(n),
-						preBuildCommands: n.data.preBuildCommands
-							? n.data.preBuildCommands.split("\n").map((cmd) => ({ bin: cmd }))
-							: [],
-						dev: {
-							enabled: n.data.dev ? n.data.dev.enabled : false,
-							username: n.data.dev && n.data.dev.enabled ? env.user.username : undefined,
-							codeServer:
-								n.data.dev?.enabled && n.data.dev.expose != null
-									? {
-											network: networkMap.get(n.data.dev.expose.network)!,
-											subdomain: n.data.dev.expose.subdomain,
-										}
-									: undefined,
-							ssh:
-								n.data.dev?.enabled && n.data.dev.expose != null
-									? {
-											network: networkMap.get(n.data.dev.expose.network)!,
-											subdomain: n.data.dev.expose.subdomain,
-										}
-									: undefined,
-						},
-					};
-				}),
-			volume: nodes
-				.filter((n) => n.type === "volume")
-				.map(
-					(n): Volume => ({
-						name: n.data.label,
-						accessMode: n.data.type,
-						size: n.data.size,
-					}),
-				),
-			postgresql: nodes
-				.filter((n) => n.type === "postgresql")
-				.map(
-					(n): PostgreSQL => ({
-						name: n.data.label,
-						size: "1Gi", // TODO(gio)
-						expose: findExpose(n).map((e) => ({ network: e.network, subdomain: e.subdomain })),
-					}),
-				),
-			mongodb: nodes
-				.filter((n) => n.type === "mongodb")
-				.map(
-					(n): MongoDB => ({
-						name: n.data.label,
-						size: "1Gi", // TODO(gio)
-						expose: findExpose(n).map((e) => ({ network: e.network, subdomain: e.subdomain })),
-					}),
-				),
-		};
-	} catch (e) {
-		console.log(e);
-		return null;
-	}
-}
+import { AppNode, NodeType } from "config";
+import { Message, MessageType } from "./state";
 
 export interface Validator {
 	(nodes: AppNode[]): Message[];
@@ -347,7 +117,7 @@
 				}) satisfies Message,
 		);
 	const noApp = git
-		.filter((n) => !nodes.some((i) => i.type === "app" && i.data?.repository?.id === n.id))
+		.filter((n) => !nodes.some((i) => i.type === "app" && i.data?.repository?.repoNodeId === n.id))
 		.map(
 			(n) =>
 				({
@@ -383,7 +153,7 @@
 			}),
 		);
 	const noSource = apps
-		.filter((n) => n.data == null || n.data.repository == null || n.data.repository.id === "")
+		.filter((n) => n.data == null || n.data.repository == null || n.data.repository.repoNodeId === "")
 		.map(
 			(n): Message => ({
 				id: `${n.id}-no-repo`,