diff options
Diffstat (limited to 'server.mjs')
| -rw-r--r-- | server.mjs | 23 |
1 files changed, 22 insertions, 1 deletions
@@ -1,7 +1,18 @@ 'use strict' +import { DatabaseSync } from 'node:sqlite' import { createServer } from 'node:http' +const database = new DatabaseSync(':memory:') + +database.exec(` + CREATE TABLE eagles( + id INTEGER PRIMARY KEY, + mac_address TEXT, + key TEXT + ) STRICT +`) + const server = createServer((req, res) => { if (req.method === 'GET' && req.url === '/') { res.writeHead(200, { 'Content-Type': 'text/plain' }) @@ -10,8 +21,18 @@ const server = createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'application/json' }) res.end(JSON.stringify({ time: Math.floor(Date.now() / 1000) })) } else if (req.method === 'GET' && req.url.startsWith('/eagle')) { + const params = new URLSearchParams(req.url.substring('/eagle'.length)) + const mac = params.get('mac') + const key = params.get('key') + if (mac === null || key === null) { + res.writeHead(400, { 'Content-Type': 'application/json' }) + res.end('{}\n') + return + } + const insert = database.prepare('INSERT INTO eagles(mac_address, key) VALUES(?, ?)') + const record = insert.run(mac, key) res.writeHead(200, { 'Content-Type': 'application/json' }) - res.end(JSON.stringify({ status: 200, data: { eagleId: 0 } })) + res.end(JSON.stringify({ status: 200, data: { eagleId: record.lastInsertRowid } })) } else { res.writeHead(404) res.end() |
