From 18665f81fdcc850bcc052809dd14a941eb80b65c Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Wed, 3 May 2023 11:54:25 +0100 Subject: [PATCH] probe registration complete --- Common/Types/API/StatusCode.ts | 2 +- Common/Types/PositiveNumber.ts | 2 +- CommonServer/Config.ts | 2 ++ Probe/Config.ts | 24 +++++++++++++++++++ Probe/Index.ts | 3 ++- Probe/Services/Register.ts | 43 ++++++++++++++++++++++++++++++++++ ProbeAPI/API/Register.ts | 24 +++++++++++++++---- 7 files changed, 92 insertions(+), 8 deletions(-) create mode 100644 Probe/Config.ts create mode 100644 Probe/Services/Register.ts diff --git a/Common/Types/API/StatusCode.ts b/Common/Types/API/StatusCode.ts index a0b81a0d52..328d7ffb5b 100644 --- a/Common/Types/API/StatusCode.ts +++ b/Common/Types/API/StatusCode.ts @@ -30,7 +30,7 @@ export default class StatusCode { statusCode = parseInt(statusCode as string); } - if (statusCode >= 100 && statusCode <= 599) { + if (statusCode as number >= 100 && statusCode as number <= 599) { return true; } diff --git a/Common/Types/PositiveNumber.ts b/Common/Types/PositiveNumber.ts index cc8377c4fb..515ea4e032 100644 --- a/Common/Types/PositiveNumber.ts +++ b/Common/Types/PositiveNumber.ts @@ -18,7 +18,7 @@ export default class PositiveNumber { } } - if (positiveNumber < 0) { + if (positiveNumber as number < 0) { throw new BadDataException('positiveNumber cannot be less than 0'); } diff --git a/CommonServer/Config.ts b/CommonServer/Config.ts index 6910f89c1f..065c13cb96 100644 --- a/CommonServer/Config.ts +++ b/CommonServer/Config.ts @@ -47,6 +47,8 @@ export const ClusterKey: ObjectID = new ObjectID( process.env['ONEUPTIME_SECRET'] || 'secret' ); +export const hasClusterKey: boolean = !!process.env['ONEUPTIME_SECRET']; + export const Domain: Hostname = Hostname.fromString( process.env['DOMAIN'] || 'localhost' ); diff --git a/Probe/Config.ts b/Probe/Config.ts new file mode 100644 index 0000000000..1959aac748 --- /dev/null +++ b/Probe/Config.ts @@ -0,0 +1,24 @@ +import URL from "Common/Types/API/URL"; +import logger from 'CommonServer/Utils/Logger'; +import ObjectID from "Common/Types/ObjectID"; + +if(!process.env['PROBE_API_URL']){ + logger.error("PROBE_API_URL is not set"); + process.exit(); +} + +export const PROBE_API_URL: URL = URL.fromString(process.env['PROBE_API_URL']); + +export const PROBE_NAME: string | null = process.env['PROBE_NAME'] || null; + +export const PROBE_DESCRIPTION: string | null = process.env['PROBE_DESCRIPTION'] || null; + +export const PROBE_ID: ObjectID | null = process.env['PROBE_ID'] ? new ObjectID(process.env['PROBE_ID']) : null; + +if(!process.env['PROBE_KEY']){ + logger.error("PROBE_KEY is not set"); + process.exit(); +} + +export const PROBE_KEY: string = process.env['PROBE_KEY']; + diff --git a/Probe/Index.ts b/Probe/Index.ts index fda2eaf352..74c890f425 100644 --- a/Probe/Index.ts +++ b/Probe/Index.ts @@ -1,6 +1,7 @@ import 'ejs'; import logger from 'CommonServer/Utils/Logger'; import App from 'CommonServer/Utils/StartServer'; +import Register from './Services/Register'; const APP_NAME: string = 'probe'; @@ -10,7 +11,7 @@ const init: Function = async (): Promise => { await App(APP_NAME); // Register this probe. - + await Register.registerProbe(); } catch (err) { logger.error('App Init Failed:'); diff --git a/Probe/Services/Register.ts b/Probe/Services/Register.ts new file mode 100644 index 0000000000..d0804d47d9 --- /dev/null +++ b/Probe/Services/Register.ts @@ -0,0 +1,43 @@ +import API from "Common/Utils/API"; +import { PROBE_API_URL, PROBE_DESCRIPTION, PROBE_ID, PROBE_KEY, PROBE_NAME } from "../Config"; +import URL from "Common/Types/API/URL"; +import { ClusterKey, hasClusterKey } from "CommonServer/Config"; +import logger from "CommonServer/Utils/Logger"; +import HTTPResponse from "Common/Types/API/HTTPResponse"; +import { JSONObject } from "Common/Types/JSON"; +import LocalCache from "CommonServer/Infrastructure/LocalCache"; + +export default class Register { + public static async registerProbe(): Promise { + + if(hasClusterKey){ + const resullt: HTTPResponse = await API.post(URL.fromString(PROBE_API_URL.toString()).addRoute("/register"), { + "probeKey": PROBE_KEY, + "probeName": PROBE_NAME, + "probeDescription": PROBE_DESCRIPTION, + "clusterKey": ClusterKey.toString() + }); + + if(resullt.isSuccess()){ + const probeId = resullt.data['_id']; + LocalCache.setString("PROBE", "PROBE_ID", probeId as string); + } + + }else{ + // validate probe. + if(!PROBE_ID){ + logger.error("PROBE_ID or ONEUPTIME_SECRET should be set"); + return process.exit(); + } + + await API.post(URL.fromString(PROBE_API_URL.toString()).addRoute("/alive"), { + "probeKey": PROBE_KEY, + "probeId": PROBE_ID, + }) + + LocalCache.setString("PROBE", "PROBE_ID", PROBE_ID.toString() as string); + } + + + } +} \ No newline at end of file diff --git a/ProbeAPI/API/Register.ts b/ProbeAPI/API/Register.ts index f9a585db2a..3c11b35f95 100644 --- a/ProbeAPI/API/Register.ts +++ b/ProbeAPI/API/Register.ts @@ -50,16 +50,30 @@ router.post( }); if (probe) { - return Response.sendTextResponse( - req, - res, - 'Probe already registered' - ); + + await ProbeService.updateOneById({ + id: probe.id!, + data: { + name: data['probeName'] as string, + description: data['probeDescription'] as string, + lastAlive: OneUptimeDate.getCurrentDate() + }, + props: { + isRoot: true, + }, + }); + + return Response.sendJsonObjectResponse(req, res, { + _id: probe._id?.toString(), + message: 'Probe already registered', + }); } let newProbe: Probe = new Probe(); newProbe.isGlobalProbe = true; newProbe.key = probeKey; + newProbe.name = data['probeName'] as string; + newProbe.description = data['probeDescription'] as string; newProbe.lastAlive = OneUptimeDate.getCurrentDate(); newProbe = await ProbeService.create({