mirror of
https://github.com/OneUptime/oneuptime.git
synced 2026-04-06 00:32:12 +02:00
probe registration complete
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
|
||||
@@ -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'
|
||||
);
|
||||
|
||||
24
Probe/Config.ts
Normal file
24
Probe/Config.ts
Normal file
@@ -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'];
|
||||
|
||||
@@ -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<void> => {
|
||||
await App(APP_NAME);
|
||||
|
||||
// Register this probe.
|
||||
|
||||
await Register.registerProbe();
|
||||
|
||||
} catch (err) {
|
||||
logger.error('App Init Failed:');
|
||||
|
||||
43
Probe/Services/Register.ts
Normal file
43
Probe/Services/Register.ts
Normal file
@@ -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<void> {
|
||||
|
||||
if(hasClusterKey){
|
||||
const resullt: HTTPResponse<JSONObject> = 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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user