feat: add name based endpoints
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -138,4 +138,6 @@ dist
|
|||||||
# Vite files
|
# Vite files
|
||||||
vite.config.js.timestamp-*
|
vite.config.js.timestamp-*
|
||||||
vite.config.ts.timestamp-*
|
vite.config.ts.timestamp-*
|
||||||
.vite/
|
.vite/
|
||||||
|
|
||||||
|
targets.json
|
||||||
21
api.js
21
api.js
@@ -1,6 +1,7 @@
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
import cors from 'cors';
|
import cors from 'cors';
|
||||||
import { sendWolPacket } from './wol.js';
|
import { sendWolPacket } from './wol.js';
|
||||||
|
import targets from "./targets.json" with { type: "json" };
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const port = 9999;
|
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, () => {
|
app.listen(port, () => {
|
||||||
console.log('WOL server running on port ' + port);
|
console.log('WOL server running on port ' + port);
|
||||||
});
|
});
|
||||||
4
targets.example.json
Normal file
4
targets.example.json
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"pc": "00:AA:BB:CC:DD:EE",
|
||||||
|
"server": "01:23:45:67:89:00"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user