Initial commit
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
PORT=3000
|
||||
TIME=60000
|
||||
@@ -0,0 +1,45 @@
|
||||
const express = require("express");
|
||||
const fs = require("fs").promises;
|
||||
|
||||
const urlStoreController = require("./../models/url_store_controller");
|
||||
const healthStatusController = require("./../models/health_status_controller");
|
||||
const urlPath = "./database/urls.json";
|
||||
|
||||
const router = express.Router();
|
||||
const app = express();
|
||||
|
||||
app.use(express.json());
|
||||
|
||||
router.post("/url/register", async (req, res) => {
|
||||
try {
|
||||
const { url } = req.body;
|
||||
|
||||
if (!url) {
|
||||
return res.status(400).json({ error: "URL is required" });
|
||||
}
|
||||
await urlStoreController.addUrl(url);
|
||||
res.status(201).json({
|
||||
message: "URL added successfully",
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({
|
||||
error: "Failed to add URL",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
router.get("/url", async (req, res) => {
|
||||
try {
|
||||
const data = await fs.readFile(urlPath, "utf8");
|
||||
const urls = JSON.parse(data);
|
||||
|
||||
res.json(urls);
|
||||
} catch (err) {
|
||||
res.status(500).json({
|
||||
error: err.message,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,26 @@
|
||||
const express = require("express");
|
||||
const app = express();
|
||||
const bodyParser = require("body-parser");
|
||||
const cors = require("cors");
|
||||
const path = require("path");
|
||||
const dotenv = require("dotenv");
|
||||
const urlRoutes = require("./Routes/url_routes");
|
||||
const WebSocket = require("ws");
|
||||
const socket = require("./web_socket_server");
|
||||
|
||||
dotenv.config();
|
||||
app.use(express.static(path.join(__dirname, "../frontend")));
|
||||
|
||||
app.use(express.json());
|
||||
|
||||
app.use("/api", urlRoutes);
|
||||
|
||||
app.get("/api/status", (req, res) => {
|
||||
res.status(200).json({ status: "Server is running" });
|
||||
});
|
||||
|
||||
const server = app.listen(process.env.PORT || 3000, () => {
|
||||
console.log(`Server is running on port ${process.env.PORT || 3000}`);
|
||||
});
|
||||
const wss = new WebSocket.Server({ server });
|
||||
socket.setupWebSocket(wss);
|
||||
+167592
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,52 @@
|
||||
[
|
||||
"cloudflare.com",
|
||||
"Akshay",
|
||||
"postgresql.org",
|
||||
"http://localhost:3001",
|
||||
"invalid-url.com",
|
||||
"github.com",
|
||||
"random-string",
|
||||
"mozilla.org",
|
||||
"todo",
|
||||
"amazon.com",
|
||||
"unknown-host-999.com",
|
||||
"google.com",
|
||||
"hello world",
|
||||
"docker.com",
|
||||
"abc123invalid.org",
|
||||
"wikipedia.org",
|
||||
"http://localhost:99999",
|
||||
"duckduckgo.com",
|
||||
"myfakeportal.dev",
|
||||
"nodejs.org",
|
||||
"example.com",
|
||||
"ftp://example.com",
|
||||
"bing.com",
|
||||
"thissitedoesnotexist123456.com",
|
||||
"mongodb.com",
|
||||
"12345",
|
||||
"reddit.com",
|
||||
"openai.com",
|
||||
"https://",
|
||||
"netflix.com",
|
||||
"expressjs.com",
|
||||
"ubuntu.org",
|
||||
"test-invalid-domain.local",
|
||||
"microsoft.com",
|
||||
"http://localhost:8080",
|
||||
"www.",
|
||||
"yahoo.com",
|
||||
"vercel.com",
|
||||
"brokenwebsite.fake",
|
||||
"golang.org",
|
||||
"apple.com",
|
||||
"digitalocean.com",
|
||||
"://invalid",
|
||||
"python.org",
|
||||
"notarealwebsite987654.io",
|
||||
"fake-company-xyz.net",
|
||||
"ubuntu.com",
|
||||
"stackoverflow.com",
|
||||
"idontexist.xyz",
|
||||
"npmjs.com"
|
||||
]
|
||||
@@ -0,0 +1,20 @@
|
||||
const fs = require("node:fs").promises;
|
||||
const url_path = "./database/db.json";
|
||||
|
||||
const StoreRecord = async (urlRecord) => {
|
||||
try {
|
||||
const rawData = await fs.readFile(url_path, "utf8");
|
||||
const data = JSON.parse(rawData);
|
||||
|
||||
data.push({
|
||||
checkedAt: new Date().toISOString(),
|
||||
status: urlRecord,
|
||||
});
|
||||
|
||||
await fs.writeFile(url_path, JSON.stringify(data, null, 4));
|
||||
} catch (err) {
|
||||
console.error("Operation failed:", err);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = { StoreRecord };
|
||||
@@ -0,0 +1,39 @@
|
||||
const axios = require("axios");
|
||||
const fs = require("fs").promises;
|
||||
const path = require("path");
|
||||
const normalizeUrl = require("../utils/utils").normalizeUrl;
|
||||
|
||||
async function checkUrl(url) {
|
||||
const target = normalizeUrl(url);
|
||||
try {
|
||||
const start = Date.now();
|
||||
const response = await axios.get(target, {
|
||||
timeout: 10000,
|
||||
headers: {
|
||||
"User-Agent":
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0 Safari/537.36",
|
||||
},
|
||||
});
|
||||
const responseTime = Date.now() - start;
|
||||
|
||||
return {
|
||||
url: url,
|
||||
statusCode: response.status,
|
||||
responseTime,
|
||||
status: "UP",
|
||||
error: null,
|
||||
checkedAt: new Date(),
|
||||
};
|
||||
} catch (err) {
|
||||
return {
|
||||
url,
|
||||
statusCode: err.response?.status || "No Response",
|
||||
responseTime: null,
|
||||
status: "DOWN",
|
||||
error: err.message,
|
||||
checkedAt: new Date(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { checkUrl };
|
||||
@@ -0,0 +1,16 @@
|
||||
const fs = require("node:fs").promises;
|
||||
const url_path = "./database/urls.json";
|
||||
|
||||
const addUrl = async (url) => {
|
||||
try {
|
||||
const rawData = await fs.readFile(url_path, "utf8");
|
||||
const data = JSON.parse(rawData);
|
||||
data.push(url);
|
||||
|
||||
await fs.writeFile(url_path, JSON.stringify(data, null, 4));
|
||||
} catch (err) {
|
||||
console.error("Operation failed:", err);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = { addUrl };
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "uptime_monitor",
|
||||
"version": "1.0.0",
|
||||
"description": "simple url status monitor application",
|
||||
"license": "ISC",
|
||||
"author": "akshay vadher",
|
||||
"type": "commonjs",
|
||||
"main": "app.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"dependencies": {
|
||||
"dotenv": "^17.4.2"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
const WebSocket = require("ws");
|
||||
let wss;
|
||||
let lastResults = [];
|
||||
|
||||
function normalizeUrl(url) {
|
||||
if (url.startsWith("http://") || url.startsWith("https://")) {
|
||||
return url;
|
||||
}
|
||||
|
||||
return "http://" + url;
|
||||
}
|
||||
|
||||
function init(webSocketServer) {
|
||||
wss = webSocketServer;
|
||||
|
||||
wss.on("connection", (client) => {
|
||||
lastResults.forEach((data) => {
|
||||
if (client.readyState === WebSocket.OPEN) {
|
||||
client.send(JSON.stringify(data));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function broadcast(data) {
|
||||
if (!wss) return;
|
||||
|
||||
if (data.url) {
|
||||
const idx = lastResults.findIndex((r) => r.url === data.url);
|
||||
if (idx >= 0) lastResults[idx] = data;
|
||||
else lastResults.push(data);
|
||||
}
|
||||
|
||||
wss.clients.forEach((client) => {
|
||||
if (client.readyState !== WebSocket.OPEN) return;
|
||||
try {
|
||||
client.send(JSON.stringify(data));
|
||||
} catch (err) {
|
||||
console.error("WebSocket broadcast failed:", err.message);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { normalizeUrl, broadcast, init };
|
||||
@@ -0,0 +1,58 @@
|
||||
const axios = require("axios");
|
||||
const fs = require("fs").promises;
|
||||
const dotenv = require("dotenv");
|
||||
const { StoreRecord } = require("./models/db_store_controller");
|
||||
const { checkUrl } = require("./models/health_status_controller");
|
||||
|
||||
const { init, broadcast } = require("./utils/utils");
|
||||
const urlPath = "./database/urls.json";
|
||||
dotenv.config();
|
||||
|
||||
async function setupWebSocket(wss) {
|
||||
init(wss);
|
||||
let Store = [];
|
||||
try {
|
||||
const urls = await fs.readFile(urlPath, "utf8");
|
||||
const parsedUrls = JSON.parse(urls);
|
||||
|
||||
await Promise.allSettled(
|
||||
parsedUrls.map(async (url) => {
|
||||
const response = await checkUrl(url);
|
||||
broadcast(response);
|
||||
Store.push(response);
|
||||
}),
|
||||
);
|
||||
StoreRecord(Store);
|
||||
broadcast({ type: "checking" });
|
||||
} catch (err) {
|
||||
console.error("WebSocket polling error:", err.message);
|
||||
broadcast({
|
||||
type: "error",
|
||||
message: "Unable to read urls.json or parse data",
|
||||
});
|
||||
}
|
||||
setInterval(async () => {
|
||||
try {
|
||||
const urls = await fs.readFile(urlPath, "utf8");
|
||||
const parsedUrls = JSON.parse(urls);
|
||||
|
||||
await Promise.allSettled(
|
||||
parsedUrls.map(async (url) => {
|
||||
const response = await checkUrl(url);
|
||||
broadcast(response);
|
||||
Store.push(response);
|
||||
}),
|
||||
);
|
||||
StoreRecord(Store);
|
||||
broadcast({ type: "checking" });
|
||||
} catch (err) {
|
||||
console.error("WebSocket polling error:", err.message);
|
||||
broadcast({
|
||||
type: "error",
|
||||
message: "Unable to read urls.json or parse data",
|
||||
});
|
||||
}
|
||||
}, process.env.TIME || 60000);
|
||||
}
|
||||
|
||||
module.exports = { setupWebSocket };
|
||||
Reference in New Issue
Block a user