From b7008afd296c46418251a91b0ceb2ff9159e842c Mon Sep 17 00:00:00 2001 From: wonder Date: Fri, 26 Jun 2026 15:08:12 +0800 Subject: [PATCH] fix: SPA routing and prompts table handling - Add clean URL routing: /dashboard -> dashboard.html, /settings -> settings.html - Add EnsurePromptsTable for dev environments where prompts table may not exist - Fix nav links to use clean URLs instead of .html paths Co-Authored-By: Claude --- .env | 18 ++++++++++++++++++ frontend/static/common.js | 4 ++-- internal/db/db.go | 18 ++++++++++++++++++ main.go | 22 +++++++++++++++++++--- 4 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 .env diff --git a/.env b/.env new file mode 100644 index 0000000..faa4ce9 --- /dev/null +++ b/.env @@ -0,0 +1,18 @@ +# 服务配置 +SERVER_PORT=8080 +SESSION_SECRET=your-random-secret-key + +# 数据库(连接已有的远程 MySQL 实例) +DB_HOST=your-mysql-public-ip +DB_PORT=3306 +DB_USER=your-db-user +DB_PASSWORD=your-db-password +DB_NAME=prompt_generator + +# 认证 +AUTH_PASSWORD=your-access-password + +# LLM +LLM_API_BASE_URL=https://api.deepseek.com/v1 +LLM_API_KEY=sk-xxx +LLM_MODEL_NAME=deepseek-chat diff --git a/frontend/static/common.js b/frontend/static/common.js index 2554042..a332c7a 100644 --- a/frontend/static/common.js +++ b/frontend/static/common.js @@ -176,8 +176,8 @@ Theme.init(); function renderNavbar(activePage) { const pages = [ { id: 'builder', label: '构建器', href: '/' }, - { id: 'dashboard', label: '复盘看板', href: '/dashboard.html' }, - { id: 'settings', label: '设置', href: '/settings.html' }, + { id: 'dashboard', label: '复盘看板', href: '/dashboard' }, + { id: 'settings', label: '设置', href: '/settings' }, ]; const nav = document.createElement('nav'); diff --git a/internal/db/db.go b/internal/db/db.go index 80708aa..5fafec4 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -109,6 +109,24 @@ func AutoMigrate() error { return nil } +// EnsurePromptsTable creates the prompts table if it doesn't exist (for dev environments) +func EnsurePromptsTable() { + _, err := DB.Exec(`CREATE TABLE IF NOT EXISTS prompts ( + id bigint NOT NULL AUTO_INCREMENT, + session_id varchar(128) NOT NULL, + project_name varchar(255) NOT NULL DEFAULT '', + prompt text NOT NULL, + created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (id), + KEY idx_session_id (session_id), + KEY idx_project_name (project_name), + KEY idx_created_at (created_at) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`) + if err != nil { + log.Printf("Warning: failed to ensure prompts table: %v", err) + } +} + func SeedData() error { // Check if system tags already exist var count int diff --git a/main.go b/main.go index 40fed3c..367f3e7 100644 --- a/main.go +++ b/main.go @@ -38,12 +38,28 @@ func main() { // Initialize handlers handlers.Init(cfg) + // Ensure prompts table exists for dev environments + db.EnsurePromptsTable() + // Setup routes mux := http.NewServeMux() - // Static files (frontend) - fs := http.FileServer(http.Dir("frontend")) - mux.Handle("/", fs) + // Static files with SPA-style routing for HTML pages + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + // Map clean URLs to .html files + switch path { + case "/dashboard": + http.ServeFile(w, r, "frontend/dashboard.html") + return + case "/settings": + http.ServeFile(w, r, "frontend/settings.html") + return + } + // Default file server + fs := http.FileServer(http.Dir("frontend")) + fs.ServeHTTP(w, r) + }) // Auth routes (no auth required) mux.HandleFunc("POST /api/auth/login", handlers.Login)