add sqllite

This commit is contained in:
2026-02-06 15:35:54 +09:00
parent e7b7414d73
commit 921455749e
9 changed files with 2745 additions and 26 deletions

107
server.js Normal file
View File

@@ -0,0 +1,107 @@
import express from 'express';
import sqlite3 from 'sqlite3';
import cors from 'cors';
import { open } from 'sqlite';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(cors());
app.use(express.json());
// Database setup
let db;
async function initializeDB() {
db = await open({
filename: path.join(__dirname, 'wifi_markers.db'),
driver: sqlite3.Database
});
// Enable WAL mode for concurrency
await db.exec('PRAGMA journal_mode = WAL;');
// Create table
await db.exec(`
CREATE TABLE IF NOT EXISTS markers (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
ssid TEXT NOT NULL,
password TEXT,
lat REAL NOT NULL,
lng REAL NOT NULL,
securityType TEXT NOT NULL,
iconType TEXT NOT NULL,
description TEXT,
addedBy TEXT,
createdAt INTEGER,
isPublic INTEGER DEFAULT 1
)
`);
console.log('Database initialized with WAL mode enabled.');
}
// Routes
app.get('/api/markers', async (req, res) => {
try {
const markers = await db.all('SELECT * FROM markers');
// Convert isPublic from 0/1 to boolean
const formattedMarkers = markers.map(m => ({
...m,
isPublic: !!m.isPublic
}));
res.json(formattedMarkers);
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Failed to fetch markers' });
}
});
app.post('/api/markers', async (req, res) => {
const { id, name, ssid, password, lat, lng, securityType, iconType, description, addedBy, createdAt, isPublic } = req.body;
try {
await db.run(`
INSERT OR REPLACE INTO markers (id, name, ssid, password, lat, lng, securityType, iconType, description, addedBy, createdAt, isPublic)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`, [id, name, ssid, password, lat, lng, securityType, iconType, description || '', addedBy, createdAt, isPublic ? 1 : 0]);
res.json({ success: true });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Failed to save marker' });
}
});
app.delete('/api/markers/:id', async (req, res) => {
const { id } = req.params;
try {
await db.run('DELETE FROM markers WHERE id = ?', [id]);
res.json({ success: true });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Failed to delete marker' });
}
});
// Serve static files from dist
app.use(express.static(path.join(__dirname, 'dist')));
// Fallback for SPA
app.get(/.*/, (req, res) => {
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});
initializeDB().then(() => {
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
});