67 lines
1.8 KiB
JavaScript
67 lines
1.8 KiB
JavaScript
const wol = require('wakeonlan');
|
|
const express = require('express');
|
|
const app = express();
|
|
const bodyParser = require('body-parser');
|
|
const path = require('path');
|
|
const ping = require('ping');
|
|
const { getDb, getServers, getServerByCol } = require('./dbMgr');
|
|
|
|
const port = process.env.PORT || 3000;
|
|
|
|
app.use(bodyParser.json());
|
|
app.use(express.static('public'));
|
|
|
|
app.get('/', (req, res) => {
|
|
res.sendFile(path.join(__dirname, 'index.html'));
|
|
});
|
|
|
|
app.get('/api/server/getAll', async (req, res) => {
|
|
try {
|
|
const db = await getDb()
|
|
res.json({ status: 'success', data: await getServers(db) })
|
|
} catch (error) {
|
|
console.log(error)
|
|
res.json({ status: 'error', error: error })
|
|
}
|
|
})
|
|
|
|
app.get('/api/server/getAll', async (req, res) => {
|
|
try {
|
|
const db = await getDb()
|
|
res.json({ status: 'success', data: await getServerByCol(db, req.query.col, req.query.val) })
|
|
} catch (error) {
|
|
console.log(error)
|
|
res.json({ status: 'error', error: error })
|
|
}
|
|
})
|
|
|
|
app.get('/api/wake', (req, res) => {
|
|
const mac = req.query.mac;
|
|
|
|
if (mac && mac !== '') {
|
|
wol(mac)
|
|
.then(() => {
|
|
res.json({ status: 'success', message: `WOL packet sent to ${mac}` });
|
|
})
|
|
.catch(err => {
|
|
res.json({ status: 'error', message: err.message });
|
|
});
|
|
} else {
|
|
res.json({ status: 'failed', message: 'No MAC address specified' });
|
|
}
|
|
});
|
|
|
|
app.get('/api/ping', (req, res) => {
|
|
const host = req.query.host
|
|
if(host && host != ''){
|
|
ping.sys.probe(host, (isAlive) => {
|
|
res.json({ status: 'success', message: isAlive })
|
|
})
|
|
} else {
|
|
res.json({ status: 'failed', message: 'No Host address specified' })
|
|
}
|
|
})
|
|
|
|
// Start the server
|
|
app.listen(port);
|