You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.6 KiB
TypeScript
71 lines
1.6 KiB
TypeScript
import { TypeOf } from "../common/deps/zod.ts";
|
|
import { makeServe, MakeHandlers, } from "../common/deps/yaypi.ts";
|
|
|
|
import { api, GetConfigurationResponseV1, StatusV1 } from "../common/api.ts";
|
|
import { load as loadConfiguration, Configuration } from "./config.ts";
|
|
|
|
const handlers: MakeHandlers<typeof api> = {
|
|
v1: {
|
|
configuration: {
|
|
get: async () => {
|
|
const config = await loadConfiguration();
|
|
|
|
const res: GetConfigurationResponseV1 = {
|
|
hosts: Object.keys(config.hosts),
|
|
services: Object.entries(config.services).map(([k, v]) => ({
|
|
name: k,
|
|
publicUrl: v.publicUrl,
|
|
})),
|
|
};
|
|
|
|
return res;
|
|
}
|
|
},
|
|
host: {
|
|
getStatus: async (hostname) => {
|
|
const config = await loadConfiguration();
|
|
if (!(hostname in config.hosts)) {
|
|
return { error: "unknown host" };
|
|
}
|
|
|
|
const status = await Deno.run({
|
|
cmd: ["ping", "-c1", "-W2", config.hosts[hostname]],
|
|
stdout: "null",
|
|
stdin: "null",
|
|
stderr: "null",
|
|
}).status();
|
|
|
|
if (!status.success) {
|
|
return StatusV1.Down;
|
|
}
|
|
|
|
return StatusV1.Up;
|
|
}
|
|
},
|
|
service: {
|
|
getStatus: async (serviceName) => {
|
|
const config = await loadConfiguration();
|
|
|
|
return getServiceStatus(serviceName, config);
|
|
}
|
|
}
|
|
},
|
|
};
|
|
|
|
export const serve = makeServe(api, handlers, "/api");
|
|
|
|
const getServiceStatus = async (serviceName: string, config: TypeOf<typeof Configuration>) => {
|
|
const sc = config.services[serviceName];
|
|
if (!sc) {
|
|
return { error: "unknown service" };
|
|
}
|
|
|
|
switch (sc.type) {
|
|
case "http": {
|
|
const status = await fetch(sc.url);
|
|
|
|
return status.ok ? StatusV1.Up : StatusV1.Down;
|
|
}
|
|
}
|
|
};
|