diff --git a/openapi.json b/openapi.json index 227a503c..c880024f 100644 --- a/openapi.json +++ b/openapi.json @@ -25,6 +25,10 @@ "name": "Monitors", "description": "APIs to interact with monitors" }, + { + "name": "MonitorStatus", + "description": "APIs to interact with monitor status" + }, { "name": "Incidents", "description": "APIs to integrate incidents" @@ -457,7 +461,7 @@ "operationId": "updateMonitorStatus", "summary": "Update status of a monitor", "description": "Update status of a monitor at a given timestamp UTC", - "tags": ["Monitors"], + "tags": ["MonitorStatus"], "security": [ { "bearerAuth": [] @@ -516,7 +520,7 @@ "operationId": "getMonitorStatus", "summary": "Get status of a monitor", "description": "get status of a monitor at timestamp", - "tags": ["Monitors"], + "tags": ["MonitorStatus"], "security": [ { "bearerAuth": [] @@ -1097,6 +1101,900 @@ } ] } - } + }, + "/api/monitor": { + "get": { + "summary": "Search Monitor", + "description": "Search Monitor", + "operationId": "searchMonitor", + "tags": ["Monitors"], + "parameters": [ + { + "name": "tag", + "in": "query", + "schema": { + "type": "string", + "example": "{{tag}}" + }, + "description": "Optional tag to filter results" + } + ], + "responses": { + "200": { + "description": "Response200", + "content": { + "application/json": { + "schema": { + "type": "array", + "items": { + "type": "object", + "properties": { + "category_name": { + "type": "string", + "example": "Home" + }, + "created_at": { + "type": "string", + "example": "2025-04-23T11:14:49.822Z" + }, + "cron": { + "type": "string", + "example": "* * * * *" + }, + "day_degraded_minimum_count": { + "type": "number", + "example": 1 + }, + "day_down_minimum_count": { + "type": "number", + "example": 1 + }, + "default_status": { + "type": "string", + "example": "NONE" + }, + "degraded_trigger": { + "nullable": true, + "example": null + }, + "description": { + "type": "string", + "example": "" + }, + "down_trigger": { + "nullable": true, + "example": null + }, + "id": { + "type": "number", + "example": 15 + }, + "image": { + "type": "string", + "example": "" + }, + "include_degraded_in_downtime": { + "type": "string", + "example": "NO" + }, + "monitor_type": { + "type": "string", + "example": "SSL" + }, + "name": { + "type": "string", + "example": "Github SSL Certificate" + }, + "status": { + "type": "string", + "example": "ACTIVE" + }, + "tag": { + "type": "string", + "example": "GithubSSL" + }, + "type_data": { + "type": "string", + "example": "{\"host\":\"github.com\",\"port\":443,\"degradedRemainingHours\":2,\"downRemainingHours\":1}" + }, + "updated_at": { + "type": "string", + "example": "2025-04-23T12:05:01.941Z" + } + } + }, + "example": [ + { + "category_name": "Home", + "created_at": "2025-04-23T11:14:49.822Z", + "cron": "* * * * *", + "day_degraded_minimum_count": 1, + "day_down_minimum_count": 1, + "default_status": "NONE", + "degraded_trigger": null, + "description": "", + "down_trigger": null, + "id": 15, + "image": "", + "include_degraded_in_downtime": "NO", + "monitor_type": "SSL", + "name": "Github SSL Certificate", + "status": "ACTIVE", + "tag": "GithubSSL", + "type_data": "{\"host\":\"github.com\",\"port\":443,\"degradedRemainingHours\":2,\"downRemainingHours\":1}", + "updated_at": "2025-04-23T12:05:01.941Z" + } + ] + }, + "examples": { + "Response200": { + "value": [ + { + "category_name": "Home", + "created_at": "2025-04-23T11:14:49.822Z", + "cron": "* * * * *", + "day_degraded_minimum_count": 1, + "day_down_minimum_count": 1, + "default_status": "NONE", + "degraded_trigger": null, + "description": "", + "down_trigger": null, + "id": 15, + "image": "", + "include_degraded_in_downtime": "NO", + "monitor_type": "SSL", + "name": "Github SSL Certificate", + "status": "ACTIVE", + "tag": "GithubSSL", + "type_data": "{\"host\":\"github.com\",\"port\":443,\"degradedRemainingHours\":2,\"downRemainingHours\":1}", + "updated_at": "2025-04-23T12:05:01.941Z" + } + ] + } + } + } + } + } + }, + "security": [ + { + "bearerAuth": [] + } + ] + }, + "post": { + "summary": "Create Monitor", + "description": "API to create monitors", + "operationId": "createMonitor", + "tags": ["Monitors"], + "requestBody": { + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "category_name": { + "type": "string", + "example": "Home" + }, + "created_at": { + "type": "string", + "example": "2025-04-22T10:10:00.546Z" + }, + "cron": { + "type": "string", + "example": "* * * * *" + }, + "day_degraded_minimum_count": { + "type": "number", + "example": 1 + }, + "day_down_minimum_count": { + "type": "number", + "example": 1 + }, + "default_status": { + "type": "string", + "example": "NONE" + }, + "degraded_trigger": { + "nullable": true, + "example": null + }, + "description": { + "type": "string", + "example": "" + }, + "down_trigger": { + "nullable": true, + "example": null + }, + "id": { + "type": "number", + "example": 15 + }, + "image": { + "type": "string", + "example": "" + }, + "include_degraded_in_downtime": { + "type": "string", + "example": "NO" + }, + "monitor_type": { + "type": "string", + "example": "API" + }, + "name": { + "type": "string", + "example": "Github" + }, + "status": { + "type": "string", + "example": "ACTIVE" + }, + "tag": { + "type": "string", + "example": "Github" + }, + "type_data": { + "type": "string", + "example": "{\"url\":\"https://github.com/\",\"method\":\"GET\",\"headers\":[],\"body\":\"\",\"timeout\":10000,\"eval\":\"(async function (statusCode, responseTime, responseRaw, modules) {\\n\\tlet statusCodeShort = Math.floor(statusCode/100);\\n if(statusCode == 429 || (statusCodeShort >=2 && statusCodeShort <= 3)) {\\n return {\\n\\t\\t\\tstatus: 'UP',\\n\\t\\t\\tlatency: responseTime,\\n }\\n } \\n\\treturn {\\n\\t\\tstatus: 'DOWN',\\n\\t\\tlatency: responseTime,\\n\\t}\\n})\",\"hideURLForGet\":\"NO\",\"allowSelfSignedCert\":false}" + }, + "updated_at": { + "type": "string", + "example": "2025-04-22T10:10:00.546Z" + } + } + }, + "examples": { + "Create Monitor": { + "value": { + "category_name": "Home", + "created_at": "2025-04-22T10:10:00.546Z", + "cron": "* * * * *", + "day_degraded_minimum_count": 1, + "day_down_minimum_count": 1, + "default_status": "NONE", + "degraded_trigger": null, + "description": "", + "down_trigger": null, + "id": 15, + "image": "", + "include_degraded_in_downtime": "NO", + "monitor_type": "API", + "name": "Github", + "status": "ACTIVE", + "tag": "Github", + "type_data": "{\"url\":\"https://github.com/\",\"method\":\"GET\",\"headers\":[],\"body\":\"\",\"timeout\":10000,\"eval\":\"(async function (statusCode, responseTime, responseRaw, modules) {\\n\\tlet statusCodeShort = Math.floor(statusCode/100);\\n if(statusCode == 429 || (statusCodeShort >=2 && statusCodeShort <= 3)) {\\n return {\\n\\t\\t\\tstatus: 'UP',\\n\\t\\t\\tlatency: responseTime,\\n }\\n } \\n\\treturn {\\n\\t\\tstatus: 'DOWN',\\n\\t\\tlatency: responseTime,\\n\\t}\\n})\",\"hideURLForGet\":\"NO\",\"allowSelfSignedCert\":false}", + "updated_at": "2025-04-22T10:10:00.546Z" + } + } + } + } + } + }, + "responses": { + "201": { + "description": "Response201", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "category_name": { + "type": "string", + "example": "Home" + }, + "created_at": { + "type": "string", + "example": "2025-04-23T12:35:21.030Z" + }, + "cron": { + "type": "string", + "example": "* * * * *" + }, + "day_degraded_minimum_count": { + "type": "number", + "example": 1 + }, + "day_down_minimum_count": { + "type": "number", + "example": 1 + }, + "default_status": { + "type": "string", + "example": "NONE" + }, + "degraded_trigger": { + "nullable": true, + "example": null + }, + "description": { + "type": "string", + "example": "" + }, + "down_trigger": { + "nullable": true, + "example": null + }, + "id": { + "type": "number", + "example": 16 + }, + "image": { + "type": "string", + "example": "" + }, + "include_degraded_in_downtime": { + "type": "string", + "example": "NO" + }, + "monitor_type": { + "type": "string", + "example": "API" + }, + "name": { + "type": "string", + "example": "Github" + }, + "status": { + "type": "string", + "example": "ACTIVE" + }, + "tag": { + "type": "string", + "example": "Github" + }, + "type_data": { + "type": "string", + "example": "{\"url\":\"https://github.com/\",\"method\":\"GET\",\"headers\":[],\"body\":\"\",\"timeout\":10000,\"eval\":\"(async function (statusCode, responseTime, responseRaw, modules) {\\n\\tlet statusCodeShort = Math.floor(statusCode/100);\\n if(statusCode == 429 || (statusCodeShort >=2 && statusCodeShort <= 3)) {\\n return {\\n\\t\\t\\tstatus: 'UP',\\n\\t\\t\\tlatency: responseTime,\\n }\\n } \\n\\treturn {\\n\\t\\tstatus: 'DOWN',\\n\\t\\tlatency: responseTime,\\n\\t}\\n})\",\"hideURLForGet\":\"NO\",\"allowSelfSignedCert\":false}" + }, + "updated_at": { + "type": "string", + "example": "2025-04-23T12:35:21.030Z" + } + } + }, + "examples": { + "Response201": { + "value": { + "category_name": "Home", + "created_at": "2025-04-23T12:35:21.030Z", + "cron": "* * * * *", + "day_degraded_minimum_count": 1, + "day_down_minimum_count": 1, + "default_status": "NONE", + "degraded_trigger": null, + "description": "", + "down_trigger": null, + "id": 16, + "image": "", + "include_degraded_in_downtime": "NO", + "monitor_type": "API", + "name": "Github", + "status": "ACTIVE", + "tag": "Github", + "type_data": "{\"url\":\"https://github.com/\",\"method\":\"GET\",\"headers\":[],\"body\":\"\",\"timeout\":10000,\"eval\":\"(async function (statusCode, responseTime, responseRaw, modules) {\\n\\tlet statusCodeShort = Math.floor(statusCode/100);\\n if(statusCode == 429 || (statusCodeShort >=2 && statusCodeShort <= 3)) {\\n return {\\n\\t\\t\\tstatus: 'UP',\\n\\t\\t\\tlatency: responseTime,\\n }\\n } \\n\\treturn {\\n\\t\\tstatus: 'DOWN',\\n\\t\\tlatency: responseTime,\\n\\t}\\n})\",\"hideURLForGet\":\"NO\",\"allowSelfSignedCert\":false}", + "updated_at": "2025-04-23T12:35:21.030Z" + } + } + } + } + } + }, + "400": { + "description": "Response400", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "error": { + "type": "string", + "example": "monitor id must be empty or 0" + } + } + }, + "examples": { + "Response400": { + "value": { + "error": "monitor id must be empty or 0" + } + } + } + } + } + } + }, + "security": [ + { + "bearerAuth": [] + } + ] + } + }, + "/api/monitor/{monitorId}": { + "get": { + "summary": "Get Monitor", + "description": "API to get a specified monitor", + "operationId": "getMonitor", + "tags": ["Monitors"], + "responses": { + "200": { + "description": "Response200", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "category_name": { + "type": "string", + "example": "Home" + }, + "created_at": { + "type": "string", + "example": "2025-04-23T11:14:49.822Z" + }, + "cron": { + "type": "string", + "example": "* * * * *" + }, + "day_degraded_minimum_count": { + "type": "number", + "example": 1 + }, + "day_down_minimum_count": { + "type": "number", + "example": 1 + }, + "default_status": { + "type": "string", + "example": "NONE" + }, + "degraded_trigger": { + "nullable": true, + "example": null + }, + "description": { + "type": "string", + "example": "" + }, + "down_trigger": { + "nullable": true, + "example": null + }, + "id": { + "type": "number", + "example": 15 + }, + "image": { + "type": "string", + "example": "" + }, + "include_degraded_in_downtime": { + "type": "string", + "example": "NO" + }, + "monitor_type": { + "type": "string", + "example": "SSL" + }, + "name": { + "type": "string", + "example": "Github SSL Certificate" + }, + "status": { + "type": "string", + "example": "ACTIVE" + }, + "tag": { + "type": "string", + "example": "GithubSSL" + }, + "type_data": { + "type": "string", + "example": "{\"host\":\"github.com\",\"port\":443,\"degradedRemainingHours\":2,\"downRemainingHours\":1}" + }, + "updated_at": { + "type": "string", + "example": "2025-04-23T12:05:01.941Z" + } + } + }, + "examples": { + "Response200": { + "value": { + "category_name": "Home", + "created_at": "2025-04-23T11:14:49.822Z", + "cron": "* * * * *", + "day_degraded_minimum_count": 1, + "day_down_minimum_count": 1, + "default_status": "NONE", + "degraded_trigger": null, + "description": "", + "down_trigger": null, + "id": 15, + "image": "", + "include_degraded_in_downtime": "NO", + "monitor_type": "SSL", + "name": "Github SSL Certificate", + "status": "ACTIVE", + "tag": "GithubSSL", + "type_data": "{\"host\":\"github.com\",\"port\":443,\"degradedRemainingHours\":2,\"downRemainingHours\":1}", + "updated_at": "2025-04-23T12:05:01.941Z" + } + } + } + } + } + }, + "404": { + "description": "Response404", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "error": { + "type": "string", + "example": "monitor does not exist" + } + } + }, + "examples": { + "Response404": { + "value": { + "error": "monitor does not exist" + } + } + } + } + } + } + }, + "security": [ + { + "bearerAuth": [] + } + ] + }, + "put": { + "summary": "Update Monitor", + "description": "API to update a specified monitor", + "operationId": "updateMonitor", + "tags": ["Monitors"], + "requestBody": { + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "category_name": { + "type": "string", + "example": "Home" + }, + "created_at": { + "type": "string", + "example": "2025-04-23T11:14:49.822Z" + }, + "cron": { + "type": "string", + "example": "* * * * *" + }, + "day_degraded_minimum_count": { + "type": "number", + "example": 1 + }, + "day_down_minimum_count": { + "type": "number", + "example": 1 + }, + "default_status": { + "type": "string", + "example": "NONE" + }, + "degraded_trigger": { + "nullable": true, + "example": null + }, + "description": { + "type": "string", + "example": "" + }, + "down_trigger": { + "nullable": true, + "example": null + }, + "id": { + "type": "number", + "example": 20 + }, + "image": { + "type": "string", + "example": "" + }, + "include_degraded_in_downtime": { + "type": "string", + "example": "NO" + }, + "monitor_type": { + "type": "string", + "example": "SSL" + }, + "name": { + "type": "string", + "example": "Github SSL Certificate" + }, + "status": { + "type": "string", + "example": "ACTIVE" + }, + "tag": { + "type": "string", + "example": "GithubSSL" + }, + "type_data": { + "type": "string", + "example": "{\"host\":\"github.com\",\"port\":443,\"degradedRemainingHours\":2,\"downRemainingHours\":1}" + }, + "updated_at": { + "type": "string", + "example": "2025-04-23T12:05:01.941Z" + } + } + }, + "examples": { + "Update Monitor": { + "value": { + "category_name": "Home", + "created_at": "2025-04-23T11:14:49.822Z", + "cron": "* * * * *", + "day_degraded_minimum_count": 1, + "day_down_minimum_count": 1, + "default_status": "NONE", + "degraded_trigger": null, + "description": "", + "down_trigger": null, + "id": 20, + "image": "", + "include_degraded_in_downtime": "NO", + "monitor_type": "SSL", + "name": "Github SSL Certificate", + "status": "ACTIVE", + "tag": "GithubSSL", + "type_data": "{\"host\":\"github.com\",\"port\":443,\"degradedRemainingHours\":2,\"downRemainingHours\":1}", + "updated_at": "2025-04-23T12:05:01.941Z" + } + } + } + } + } + }, + "responses": { + "200": { + "description": "Response200", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "category_name": { + "type": "string", + "example": "Home" + }, + "created_at": { + "type": "string", + "example": "2025-04-23T11:14:49.822Z" + }, + "cron": { + "type": "string", + "example": "* * * * *" + }, + "day_degraded_minimum_count": { + "type": "number", + "example": 1 + }, + "day_down_minimum_count": { + "type": "number", + "example": 1 + }, + "default_status": { + "type": "string", + "example": "NONE" + }, + "degraded_trigger": { + "nullable": true, + "example": null + }, + "description": { + "type": "string", + "example": "" + }, + "down_trigger": { + "nullable": true, + "example": null + }, + "id": { + "type": "number", + "example": 15 + }, + "image": { + "type": "string", + "example": "" + }, + "include_degraded_in_downtime": { + "type": "string", + "example": "NO" + }, + "monitor_type": { + "type": "string", + "example": "SSL" + }, + "name": { + "type": "string", + "example": "Github SSL Certificate" + }, + "status": { + "type": "string", + "example": "ACTIVE" + }, + "tag": { + "type": "string", + "example": "GithubSSL" + }, + "type_data": { + "type": "string", + "example": "{\"host\":\"github.com\",\"port\":443,\"degradedRemainingHours\":2,\"downRemainingHours\":1}" + }, + "updated_at": { + "type": "string", + "example": "2025-04-23T13:38:22.965Z" + } + } + }, + "examples": { + "Response200": { + "value": { + "category_name": "Home", + "created_at": "2025-04-23T11:14:49.822Z", + "cron": "* * * * *", + "day_degraded_minimum_count": 1, + "day_down_minimum_count": 1, + "default_status": "NONE", + "degraded_trigger": null, + "description": "", + "down_trigger": null, + "id": 15, + "image": "", + "include_degraded_in_downtime": "NO", + "monitor_type": "SSL", + "name": "Github SSL Certificate", + "status": "ACTIVE", + "tag": "GithubSSL", + "type_data": "{\"host\":\"github.com\",\"port\":443,\"degradedRemainingHours\":2,\"downRemainingHours\":1}", + "updated_at": "2025-04-23T13:38:22.965Z" + } + } + } + } + } + }, + "400": { + "description": "Response400", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "error": { + "type": "string", + "example": "id mismatch: payload: 15, url: 14" + } + } + }, + "examples": { + "Response400": { + "value": { + "error": "id mismatch: payload: 15, url: 14" + } + } + } + } + } + }, + "404": { + "description": "Response404", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "error": { + "type": "string", + "example": "monitor does not exist" + } + } + }, + "examples": { + "Response404": { + "value": { + "error": "monitor does not exist" + } + } + } + } + } + } + }, + "security": [ + { + "bearerAuth": [] + } + ] + }, + "delete": { + "summary": "Delete Monitor", + "description": "API to delete a specified monitor", + "operationId": "deleteMonitor", + "tags": ["Monitors"], + "responses": { + "200": { + "description": "Response200", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": {} + }, + "examples": { + "Response200": { + "value": {} + } + } + } + } + }, + "404": { + "description": "Response404", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "error": { + "type": "string", + "example": "monitor does not exist" + } + } + }, + "examples": { + "Response404": { + "value": { + "error": "monitor does not exist" + } + } + } + } + } + } + }, + "security": [ + { + "bearerAuth": [] + } + ] + }, + "parameters": [ + { + "name": "monitorId", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ] + } } } diff --git a/src/lib/server/controllers/controller.js b/src/lib/server/controllers/controller.js index 26952b13..088c7295 100644 --- a/src/lib/server/controllers/controller.js +++ b/src/lib/server/controllers/controller.js @@ -1,15 +1,13 @@ // @ts-nocheck import { - IsValidURL, - IsValidGHObject, - IsValidObject, - IsValidNav, - IsValidHero, - IsValidI18n, IsValidAnalytics, IsValidColors, - IsValidJSONString, + IsValidHero, + IsValidI18n, IsValidJSONArray, + IsValidJSONString, + IsValidNav, + IsValidURL } from "./validators.js"; import db from "../db/db.js"; import bcrypt from "bcrypt"; @@ -18,8 +16,8 @@ import { Resend } from "resend"; import Queue from "queue"; import crypto from "crypto"; -import { format, subMonths, addMonths, startOfMonth } from "date-fns"; -import { UP, DOWN, DEGRADED, NO_DATA, REALTIME, SIGNAL } from "../constants.js"; +import { addMonths, format, startOfMonth, subMonths } from "date-fns"; +import { DEGRADED, DOWN, NO_DATA, SIGNAL, UP } from "../constants.js"; import { GetMinuteStartNowTimestampUTC, GetNowTimestampUTC, ReplaceAllOccurrences, ValidateEmail } from "../tool.js"; import getSMTPTransport from "../notification/smtps.js"; import fs from "fs-extra"; @@ -352,6 +350,22 @@ export const CreateUpdateMonitor = async (monitor) => { } }; +export const CreateMonitor = async (monitor) => { + let monitorData = { ...monitor }; + if (monitorData.id) { + throw new Error('monitor id must be empty or 0'); + } + return await db.insertMonitor(monitorData); +}; + +export const UpdateMonitor = async (monitor) => { + let monitorData = { ...monitor }; + if (!!!monitorData.id || monitorData.id === 0) { + throw new Error('monitor id cannot be empty or 0'); + } + return await db.updateMonitor(monitorData); +}; + export const GetMonitors = async (data) => { return await db.getMonitors(data); }; @@ -555,11 +569,13 @@ export const GetAllAlertsPaginated = async (data) => { total: await db.getMonitorAlertsCount(), }; }; + function generateApiKey() { const prefix = "kener_"; const randomKey = crypto.randomBytes(32).toString("hex"); // 64-character hexadecimal string return prefix + randomKey; } + function createHash(apiKey) { return crypto .createHmac("sha256", process.env.KENER_SECRET_KEY || DUMMY_SECRET) diff --git a/src/routes/(kener)/api/monitor/+server.js b/src/routes/(kener)/api/monitor/+server.js new file mode 100644 index 00000000..8570bbfd --- /dev/null +++ b/src/routes/(kener)/api/monitor/+server.js @@ -0,0 +1,57 @@ +// @ts-nocheck +// @ts-ignore +import { json } from "@sveltejs/kit"; +import { auth } from "$lib/server/webhook"; +import { CreateMonitor, GetMonitors } from "$lib/server/controllers/controller.js"; + +export async function GET({ request, url }) { + const authError = await auth(request); + if (authError !== null) { + return json({ error: authError.message }, { status: 401 }); + } + + let data = {}; + const query = url.searchParams; + const tag = query.get("tag"); + if (tag) { + data = { + tag: tag, + }; + } + + let resp; + try { + resp = await GetMonitors(data); + } catch (error) { + resp = { error: error.message }; + return json(resp, { status: 500 }); + } + return json(resp, { status: 200 }); +} + +export async function POST({ request }) { + const payload = await request.json(); + const authError = await auth(request); + if (authError !== null) { + return json({ error: authError.message }, { status: 401 }); + } + + if (payload.id) { + return json({ error: 'monitor id must be empty or 0' }, { status: 400 }); + } + + try { + await CreateMonitor(payload); + } catch (error) { + return json({ error: error.message }, { status: 500 }); + } + + let resp; + try { + resp = await GetMonitors({ tag: payload.tag }); + } catch (error) { + resp = { error: error.message }; + return json(resp, { status: 500 }); + } + return json(resp[0], { status: 201 }); +} diff --git a/src/routes/(kener)/api/monitor/[monitor_id]/+server.js b/src/routes/(kener)/api/monitor/[monitor_id]/+server.js new file mode 100644 index 00000000..abd9c43f --- /dev/null +++ b/src/routes/(kener)/api/monitor/[monitor_id]/+server.js @@ -0,0 +1,105 @@ +// @ts-nocheck +// @ts-ignore +import { json } from "@sveltejs/kit"; +import { auth } from "$lib/server/webhook"; +import { DeleteMonitorCompletelyUsingTag, GetMonitors, UpdateMonitor } from "$lib/server/controllers/controller"; + +export async function GET({ request, params }) { + const authError = await auth(request); + if (authError !== null) { + return json({ error: authError.message }, { status: 401 }); + } + + const id = params.monitor_id; + if (!!!id) { + return json({ error: "id missing" }, { status: 400 }); + } + + let resp; + try { + resp = await GetMonitors({ id: id }); + } catch (error) { + resp = { error: error.message }; + return json(resp, { status: 500 }); + } + + if (resp.length === 0) { + return json({ error: "monitor does not exist" }, { status: 404 }); + } + return json(resp[0], { status: 200 }); +} + +export async function PUT({ request, params }) { + const payload = await request.json(); + const authError = await auth(request); + if (authError !== null) { + return json({ error: authError.message }, { status: 401 }); + } + + const id = params.monitor_id; + if (!!!id) { + return json({ error: "id missing" }, { status: 400 }); + } + + if (`${payload.id}` !== `${id}`) { + return json({ error: `id mismatch: payload: ${payload.id}, url: ${id}` }, { status: 400 }); + } + + let resp; + try { + resp = await GetMonitors({ id: id }); + } catch (error) { + resp = { error: error.message }; + return json(resp, { status: 500 }); + } + + if (resp.length === 0) { + return json({ error: "monitor does not exist" }, { status: 404 }); + } + + try { + await UpdateMonitor(payload); + } catch (error) { + return json({ error: error.message }, { status: 500 }); + } + + try { + resp = await GetMonitors({ tag: payload.tag }); + } catch (error) { + resp = { error: error.message }; + return json(resp, { status: 500 }); + } + return json(resp[0], { status: 200 }); +} + +export async function DELETE({ request, params }) { + const authError = await auth(request); + if (authError !== null) { + return json({ error: authError.message }, { status: 401 }); + } + + const id = params.monitor_id; + if (!!!id) { + return json({ error: "id missing" }, { status: 400 }); + } + + let resp; + try { + resp = await GetMonitors({ id: id }); + } catch (error) { + resp = { error: error.message }; + return json(resp, { status: 500 }); + } + + if (resp.length === 0) { + return json({ error: "monitor does not exist" }, { status: 404 }); + } + + try { + resp = await DeleteMonitorCompletelyUsingTag(resp[0].tag); + } catch (error) { + resp = { error: error.message }; + return json(resp, { status: 500 }); + } + return json({}, { status: 200 }); +}