55 lines
1.6 KiB
JavaScript
55 lines
1.6 KiB
JavaScript
#!/usr/bin/env node
|
|
import { createCloudApiServer } from "../../internal/cloud/server.mjs";
|
|
|
|
const host = process.env.HWLAB_CLOUD_API_HOST || "0.0.0.0";
|
|
const port = Number.parseInt(process.env.HWLAB_CLOUD_API_PORT || process.env.PORT || "6667", 10);
|
|
const commitId = process.env.HWLAB_COMMIT_ID || process.env.HWLAB_GIT_SHA;
|
|
const codeAgentTimeoutMs = parseTimeout(process.env.HWLAB_CODE_AGENT_TIMEOUT_MS, 150000, {
|
|
min: 1000,
|
|
max: 300000
|
|
});
|
|
const codeAgentHardTimeoutMs = parseTimeout(process.env.HWLAB_CODE_AGENT_HARD_TIMEOUT_MS, Math.max(600000, codeAgentTimeoutMs * 4), {
|
|
min: codeAgentTimeoutMs,
|
|
max: 1800000
|
|
});
|
|
|
|
if (!process.env.HWLAB_IMAGE && commitId) {
|
|
process.env.HWLAB_IMAGE = `ghcr.io/pikastech/hwlab-cloud-api:${commitId.slice(0, 7)}`;
|
|
}
|
|
|
|
if (!process.env.HWLAB_IMAGE_TAG && commitId) {
|
|
process.env.HWLAB_IMAGE_TAG = commitId.slice(0, 7);
|
|
}
|
|
|
|
const server = createCloudApiServer({
|
|
codeAgentTimeoutMs,
|
|
codeAgentHardTimeoutMs
|
|
});
|
|
|
|
function parseTimeout(value, fallback, { min, max }) {
|
|
const parsed = Number.parseInt(value || "", 10);
|
|
if (!Number.isInteger(parsed)) return fallback;
|
|
return Math.min(Math.max(parsed, min), max);
|
|
}
|
|
|
|
server.listen(port, host, () => {
|
|
const address = server.address();
|
|
const resolvedPort = typeof address === "object" && address ? address.port : port;
|
|
process.stdout.write(
|
|
`${JSON.stringify({
|
|
serviceId: "hwlab-cloud-api",
|
|
status: "listening",
|
|
host,
|
|
port: resolvedPort
|
|
})}\n`
|
|
);
|
|
});
|
|
|
|
for (const signal of ["SIGINT", "SIGTERM"]) {
|
|
process.on(signal, () => {
|
|
server.close(() => {
|
|
process.exit(0);
|
|
});
|
|
});
|
|
}
|