-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
67 lines (58 loc) · 1.67 KB
/
main.go
File metadata and controls
67 lines (58 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"log"
"net/http"
"os"
)
func main() {
// 获取环境变量
port := os.Getenv("PORT")
if port == "" {
port = "52133"
}
adminPassword = os.Getenv("ADMIN_PASSWORD")
if adminPassword == "" {
adminPassword = "admin123"
log.Println("Warning: Using default password 'admin123'. Set ADMIN_PASSWORD environment variable for security.")
}
// 初始化数据库
if err := initDB(); err != nil {
log.Fatalf("Database initialization failed: %v", err)
}
defer func() {
if db != nil {
db.Close()
}
}()
// 路由设置
http.HandleFunc("/", indexHandler)
http.HandleFunc("/manifest.json", manifestHandler)
http.HandleFunc("/sw.js", serviceWorkerHandler)
http.HandleFunc("/icon-192.png", iconHandler)
http.HandleFunc("/icon-512.png", iconHandler)
// API 路由
http.HandleFunc("/api/login", loginHandler)
http.HandleFunc("/api/verify", authMiddleware(verifyHandler))
http.HandleFunc("/api/devices", authMiddleware(func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
getDevicesHandler(w, r)
case http.MethodPost:
addDeviceHandler(w, r)
case http.MethodDelete:
deleteDeviceHandler(w, r)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}))
http.HandleFunc("/api/db-status", authMiddleware(dbStatusHandler))
// WOL API (保持原有路径兼容)
http.HandleFunc("/wol", wolHandler)
http.HandleFunc("/health", healthHandler)
log.Printf("Go4WOL Service starting on port %s", port)
log.Printf("Admin password: %s", adminPassword)
log.Printf("Database path: /data/devices.db")
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal("Server failed to start:", err)
}
}