Canvas: Check agent health

Change-Id: I429eecebde1d661513dfd94e27cdaa820bd49493
diff --git a/apps/canvas/back/src/index.ts b/apps/canvas/back/src/index.ts
index fb77431..9628ef5 100644
--- a/apps/canvas/back/src/index.ts
+++ b/apps/canvas/back/src/index.ts
@@ -25,7 +25,7 @@
 } from "config";
 import { Instant, DateTimeFormatter, ZoneId } from "@js-joda/core";
 import LogStore from "./log.js";
-import { GraphOrConfigSchema, GraphSchema, GraphConfigOrDraft } from "config/dist/graph.js";
+import { GraphOrConfigSchema, GraphSchema, GraphConfigOrDraft, AgentAccess } from "config/dist/graph.js";
 
 async function generateKey(root: string): Promise<[string, string]> {
 	const privKeyPath = path.join(root, "key");
@@ -1200,6 +1200,23 @@
 	};
 }
 
+const handleAgentStatus: express.Handler = async (req, resp) => {
+	const projectId = Number(req.params["projectId"]);
+	const agentName = req.params["agentName"];
+	try {
+		const env = await getEnv(projectId, resp.locals.userId, resp.locals.username);
+		const agent = env.access.find((a): a is AgentAccess => a.type === "https" && a.agentName === agentName);
+		if (!agent) {
+			resp.status(404).send({ status: 404 });
+			return;
+		}
+		const agentResp = await axios.get(agent.address);
+		resp.status(200).send({ status: agentResp.status });
+	} catch {
+		resp.status(200).send({ status: 500 });
+	}
+};
+
 async function start() {
 	await db.$connect();
 	const app = express();
@@ -1231,6 +1248,7 @@
 	projectRouter.post("/:projectId/quitquitquit/:serviceName/:workerId", handleQuitWorker);
 	projectRouter.post("/:projectId/reload", handleReload);
 	projectRouter.get("/:projectId/logs/:service/:workerId", handleServiceLogs);
+	projectRouter.get("/:projectId/agent/:agentName/status", handleAgentStatus);
 	projectRouter.post("/:projectId/remove-deployment", handleRemoveDeployment);
 	projectRouter.get("/", handleProjectAll);
 	projectRouter.post("/", handleProjectCreate);