From 6d39c4b8e969dbd9e900fbd1f6aadcfdd376b3f4 Mon Sep 17 00:00:00 2001 From: rosa Date: Wed, 28 Jan 2026 02:00:48 +0100 Subject: [PATCH] feat: add name based endpoints --- .gitignore | 4 +++- api.js | 21 +++++++++++++++++++++ targets.example.json | 4 ++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 targets.example.json diff --git a/.gitignore b/.gitignore index f1a90a0..ab16286 100644 --- a/.gitignore +++ b/.gitignore @@ -138,4 +138,6 @@ dist # Vite files vite.config.js.timestamp-* vite.config.ts.timestamp-* -.vite/ \ No newline at end of file +.vite/ + +targets.json \ No newline at end of file diff --git a/api.js b/api.js index f7edaa4..23321f1 100644 --- a/api.js +++ b/api.js @@ -1,6 +1,7 @@ import express from 'express'; import cors from 'cors'; import { sendWolPacket } from './wol.js'; +import targets from "./targets.json" with { type: "json" }; const app = express(); const port = 9999; @@ -37,6 +38,26 @@ app.get('/wake', (req, res) => { }); }); +app.get("/wake/:name", (req, res) => { + const name = req.params.name; + const mac = targets[name]; + + if (!mac) { + return res.status(404).json({ status: "error", message: "Unknown target" }); + } + + sendWolPacket(mac) + .then(() => res.json({ status: "ok" })) + .catch(err => { + console.error(err); + res.status(500).json({ status: "error", message: "Failed to send WOL packet" }); + }); +}); + +app.get("/targets", (req, res) => { + res.json(Object.keys(targets)); +}); + app.listen(port, () => { console.log('WOL server running on port ' + port); }); \ No newline at end of file diff --git a/targets.example.json b/targets.example.json new file mode 100644 index 0000000..79e576a --- /dev/null +++ b/targets.example.json @@ -0,0 +1,4 @@ +{ + "pc": "00:AA:BB:CC:DD:EE", + "server": "01:23:45:67:89:00" +} \ No newline at end of file