114 lines
3.4 KiB
TypeScript
114 lines
3.4 KiB
TypeScript
import Database from 'better-sqlite3'
|
|
import { resolve } from 'path'
|
|
|
|
// 싱글톤 DB 인스턴스
|
|
let db: Database.Database | null = null
|
|
|
|
export function getDb(): Database.Database {
|
|
if (!db) {
|
|
const dbPath = resolve(process.cwd(), 'database/osolit-monitor.db')
|
|
db = new Database(dbPath)
|
|
db.pragma('journal_mode = WAL')
|
|
}
|
|
return db
|
|
}
|
|
|
|
export function initPubnetTables(): void {
|
|
const db = getDb()
|
|
|
|
db.exec(`
|
|
CREATE TABLE IF NOT EXISTS pubnet_targets (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT NOT NULL,
|
|
url TEXT NOT NULL UNIQUE,
|
|
is_active INTEGER DEFAULT 1,
|
|
sort_order INTEGER DEFAULT 0,
|
|
created_at TEXT DEFAULT (datetime('now', 'localtime')),
|
|
updated_at TEXT DEFAULT (datetime('now', 'localtime'))
|
|
)
|
|
`)
|
|
|
|
db.exec(`
|
|
CREATE TABLE IF NOT EXISTS pubnet_logs (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
target_id INTEGER NOT NULL,
|
|
is_success INTEGER NOT NULL,
|
|
checked_at TEXT DEFAULT (datetime('now', 'localtime')),
|
|
FOREIGN KEY (target_id) REFERENCES pubnet_targets(id)
|
|
)
|
|
`)
|
|
|
|
db.exec(`
|
|
CREATE TABLE IF NOT EXISTS pubnet_status (
|
|
id INTEGER PRIMARY KEY CHECK (id = 1),
|
|
current_index INTEGER DEFAULT 0,
|
|
check_interval INTEGER DEFAULT 300000,
|
|
is_healthy INTEGER DEFAULT 1,
|
|
last_target_id INTEGER,
|
|
last_checked_at TEXT,
|
|
scheduler_running INTEGER DEFAULT 0,
|
|
updated_at TEXT DEFAULT (datetime('now', 'localtime'))
|
|
)
|
|
`)
|
|
|
|
const statusExists = db.prepare('SELECT COUNT(*) as cnt FROM pubnet_status').get() as { cnt: number }
|
|
if (statusExists.cnt === 0) {
|
|
db.prepare('INSERT INTO pubnet_status (id) VALUES (1)').run()
|
|
}
|
|
|
|
console.log('[DB] pubnet tables initialized')
|
|
}
|
|
|
|
export function initPrivnetTables(): void {
|
|
const db = getDb()
|
|
|
|
db.exec(`
|
|
CREATE TABLE IF NOT EXISTS privnet_targets (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT NOT NULL,
|
|
url TEXT NOT NULL UNIQUE,
|
|
is_active INTEGER DEFAULT 1,
|
|
sort_order INTEGER DEFAULT 0,
|
|
created_at TEXT DEFAULT (datetime('now', 'localtime')),
|
|
updated_at TEXT DEFAULT (datetime('now', 'localtime'))
|
|
)
|
|
`)
|
|
|
|
db.exec(`
|
|
CREATE TABLE IF NOT EXISTS privnet_logs (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
target_id INTEGER NOT NULL,
|
|
is_success INTEGER NOT NULL,
|
|
checked_at TEXT DEFAULT (datetime('now', 'localtime')),
|
|
FOREIGN KEY (target_id) REFERENCES privnet_targets(id)
|
|
)
|
|
`)
|
|
|
|
db.exec(`
|
|
CREATE TABLE IF NOT EXISTS privnet_status (
|
|
id INTEGER PRIMARY KEY CHECK (id = 1),
|
|
current_index INTEGER DEFAULT 0,
|
|
check_interval INTEGER DEFAULT 300000,
|
|
is_healthy INTEGER DEFAULT 1,
|
|
last_target_id INTEGER,
|
|
last_checked_at TEXT,
|
|
scheduler_running INTEGER DEFAULT 0,
|
|
updated_at TEXT DEFAULT (datetime('now', 'localtime'))
|
|
)
|
|
`)
|
|
|
|
const statusExists = db.prepare('SELECT COUNT(*) as cnt FROM privnet_status').get() as { cnt: number }
|
|
if (statusExists.cnt === 0) {
|
|
db.prepare('INSERT INTO privnet_status (id) VALUES (1)').run()
|
|
}
|
|
|
|
console.log('[DB] privnet tables initialized')
|
|
}
|
|
|
|
export function closeDb(): void {
|
|
if (db) {
|
|
db.close()
|
|
db = null
|
|
}
|
|
}
|