From 7beec630f784d72d21ee621700df42aaa4f4b498 Mon Sep 17 00:00:00 2001 From: Li Zhineng Date: Tue, 1 Jul 2025 12:13:45 +0800 Subject: use sqlite --- server.mjs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'server.mjs') diff --git a/server.mjs b/server.mjs index 8176fc0..2bfbde7 100644 --- a/server.mjs +++ b/server.mjs @@ -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() -- cgit v1.2.3