- Flask + SQLite 个人导航页系统 - 前台导航页(分类Tab、卡片展示) - 管理后台(服务管理、分类管理、健康检测) - 响应式设计 - Systemd 服务配置
113 lines
3.7 KiB
Python
113 lines
3.7 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""数据库初始化脚本"""
|
||
|
||
import sqlite3
|
||
import os
|
||
from config import Config
|
||
|
||
def init_database():
|
||
"""初始化数据库表"""
|
||
db_path = Config.DATABASE_PATH
|
||
|
||
# 如果数据库已存在,先删除(可选,开发环境)
|
||
if os.path.exists(db_path):
|
||
print(f"数据库已存在: {db_path}")
|
||
return
|
||
|
||
conn = sqlite3.connect(db_path)
|
||
cursor = conn.cursor()
|
||
|
||
# 创建 services 表
|
||
cursor.execute('''
|
||
CREATE TABLE IF NOT EXISTS services (
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
name VARCHAR(100) NOT NULL,
|
||
url VARCHAR(500) NOT NULL,
|
||
description TEXT,
|
||
icon VARCHAR(50),
|
||
category VARCHAR(50) DEFAULT '默认',
|
||
is_enabled INTEGER DEFAULT 1,
|
||
sort_order INTEGER DEFAULT 0,
|
||
health_check_url VARCHAR(500),
|
||
health_check_enabled INTEGER DEFAULT 0,
|
||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||
)
|
||
''')
|
||
|
||
# 创建 categories 表
|
||
cursor.execute('''
|
||
CREATE TABLE IF NOT EXISTS categories (
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
name VARCHAR(50) NOT NULL UNIQUE,
|
||
sort_order INTEGER DEFAULT 0
|
||
)
|
||
''')
|
||
|
||
# 创建 users 表
|
||
cursor.execute('''
|
||
CREATE TABLE IF NOT EXISTS users (
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
username VARCHAR(50) NOT NULL UNIQUE,
|
||
password_hash VARCHAR(255) NOT NULL,
|
||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||
)
|
||
''')
|
||
|
||
# 创建索引
|
||
cursor.execute('CREATE INDEX IF NOT EXISTS idx_services_category ON services(category)')
|
||
cursor.execute('CREATE INDEX IF NOT EXISTS idx_services_enabled ON services(is_enabled)')
|
||
cursor.execute('CREATE INDEX IF NOT EXISTS idx_services_sort ON services(sort_order DESC)')
|
||
|
||
conn.commit()
|
||
conn.close()
|
||
|
||
print(f"数据库初始化完成: {db_path}")
|
||
|
||
def insert_initial_data():
|
||
"""插入初始数据"""
|
||
import hashlib
|
||
|
||
conn = sqlite3.connect(Config.DATABASE_PATH)
|
||
cursor = conn.cursor()
|
||
|
||
# 创建默认管理员账号 (admin / admin123)
|
||
# 使用 SHA256 简单哈希(生产环境建议用 bcrypt)
|
||
password_hash = hashlib.sha256('admin123'.encode()).hexdigest()
|
||
|
||
cursor.execute('''
|
||
INSERT INTO users (username, password_hash)
|
||
VALUES (?, ?)
|
||
''', ('admin', password_hash))
|
||
|
||
# 创建默认分类
|
||
categories = [('内网服务', 1), ('开发工具', 2), ('测试环境', 3)]
|
||
for name, sort_order in categories:
|
||
cursor.execute('''
|
||
INSERT OR IGNORE INTO categories (name, sort_order)
|
||
VALUES (?, ?)
|
||
''', (name, sort_order))
|
||
|
||
# 创建默认服务
|
||
services = [
|
||
('违禁品查获排行榜', 'http://127.0.0.1:9517', '实时数据统计 · 自动刷新', '📊', '内网服务', 1, 100, 'http://127.0.0.1:9517/api/rankings', 1),
|
||
('短信接收端-Python', 'http://127.0.0.1:9518', 'HTTP接口 + Web管理', '📱', '内网服务', 1, 90, None, 0),
|
||
('短信接收端-Go', 'http://127.0.0.1:28001', '高性能版本 · 端口28001', '🔧', '内网服务', 1, 80, 'http://127.0.0.1:28001/', 1),
|
||
]
|
||
|
||
for service in services:
|
||
cursor.execute('''
|
||
INSERT INTO services (name, url, description, icon, category, is_enabled, sort_order, health_check_url, health_check_enabled)
|
||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||
''', service)
|
||
|
||
conn.commit()
|
||
conn.close()
|
||
|
||
print("初始数据插入完成")
|
||
print("默认管理员账号: admin / admin123")
|
||
|
||
if __name__ == '__main__':
|
||
init_database()
|
||
insert_initial_data()
|