-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
74 lines (61 loc) · 1.81 KB
/
main.go
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
68
69
70
71
72
73
74
package main
import (
"embed"
"flag"
"fmt"
"log"
"net"
"net/http"
"tvbox/db"
"tvbox/handlers"
"github.com/gorilla/mux"
)
//go:embed static/*
var staticContent embed.FS
func getLocalIP() string {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
return "0.0.0.0"
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP.String()
}
func main() {
// 定义命令行参数
port := flag.Int("port", 2345, "Port to run the server on")
domain := flag.String("domain", "", "Domain to bind to (default: local IP)")
flag.Parse()
// 初始化数据库
err := db.InitDB()
if err != nil {
log.Fatal(err)
}
r := mux.NewRouter()
handlers.InitHandlers(staticContent)
// 路由设置
r.HandleFunc("/", handlers.TVBoxHandler)
r.HandleFunc("/admin", handlers.AdminHandler)
r.HandleFunc("/admin/login", handlers.LoginHandler)
r.HandleFunc("/admin/logout", handlers.LogoutHandler)
r.HandleFunc("/admin/add-link", handlers.AddLinkHandler)
r.HandleFunc("/admin/edit-link", handlers.EditLinkHandler)
r.HandleFunc("/admin/delete-link/{id}", handlers.DeleteLinkHandler)
r.HandleFunc("/admin/change-state/{id}", handlers.ChangeStateHandler)
r.HandleFunc("/admin/update-password", handlers.UpdatePasswordHandler)
// 添加静态文件服务
fileServer := http.FileServer(http.FS(staticContent))
r.PathPrefix("/static/").Handler(http.StripPrefix("", fileServer))
// 构建服务器地址
localIP := getLocalIP()
listenAddr := fmt.Sprintf(":%d", *port)
var publicURL string
if *domain != "" {
publicURL = fmt.Sprintf("http://%s:%d", *domain, *port)
} else {
publicURL = fmt.Sprintf("http://%s:%d", localIP, *port)
}
log.Printf("Server starting, public URL: %s\n", publicURL)
log.Printf("Server listening on %s\n", listenAddr)
log.Fatal(http.ListenAndServe(listenAddr, r))
}