fix: Windows 闪退 — 增加日志文件输出、错误时保持窗口
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"embed"
|
||||
"encoding/csv"
|
||||
"encoding/json"
|
||||
@@ -22,7 +23,31 @@ var webFS embed.FS
|
||||
|
||||
var collector *Collector
|
||||
|
||||
// waitExit keeps the console window open on Windows so the user can read errors.
|
||||
func waitExit() {
|
||||
if runtime.GOOS == "windows" {
|
||||
fmt.Println("\n按回车键退出...")
|
||||
bufio.NewReader(os.Stdin).ReadBytes('\n')
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Log to file so crashes are diagnosable even if console closes.
|
||||
lf, lfErr := os.OpenFile("bypos-collector.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
|
||||
if lfErr == nil {
|
||||
log.SetOutput(io.MultiWriter(os.Stderr, lf))
|
||||
defer lf.Close()
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Printf("程序崩溃: %v", r)
|
||||
waitExit()
|
||||
}
|
||||
}()
|
||||
|
||||
log.Println("bypos-collector 启动中...")
|
||||
|
||||
addr := flag.String("addr", "127.0.0.1:8765", "本地监听地址")
|
||||
noOpen := flag.Bool("no-open", false, "不自动打开浏览器")
|
||||
sdog := flag.String("sdogid", "", "中心库账号 id(优先读 $BYPOS_SDOGID 环境变量)")
|
||||
@@ -32,7 +57,12 @@ func main() {
|
||||
log.Println("警告: 未配置 sdogid,请通过 $BYPOS_SDOGID 环境变量、-sdogid 参数或控制台输入框指定")
|
||||
}
|
||||
|
||||
sub, _ := fs.Sub(webFS, "web")
|
||||
sub, err := fs.Sub(webFS, "web")
|
||||
if err != nil {
|
||||
log.Printf("错误: 无法加载内嵌 web 资源: %v", err)
|
||||
waitExit()
|
||||
return
|
||||
}
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/", http.FileServer(http.FS(sub)))
|
||||
mux.HandleFunc("/api/start", handleStart)
|
||||
@@ -43,7 +73,9 @@ func main() {
|
||||
|
||||
ln, err := net.Listen("tcp", *addr)
|
||||
if err != nil {
|
||||
log.Fatalf("无法监听 %s: %v", *addr, err)
|
||||
log.Printf("错误: 无法监听 %s: %v", *addr, err)
|
||||
waitExit()
|
||||
return
|
||||
}
|
||||
realAddr := ln.Addr().String()
|
||||
urlStr := "http://" + realAddr + "/"
|
||||
@@ -52,10 +84,14 @@ func main() {
|
||||
fmt.Println(" 控制台: " + urlStr)
|
||||
fmt.Println(" 关闭本窗口即停止程序")
|
||||
fmt.Println("==============================================")
|
||||
log.Printf("监听地址: %s", realAddr)
|
||||
if !*noOpen {
|
||||
go openBrowser(urlStr)
|
||||
}
|
||||
log.Fatal(http.Serve(ln, mux))
|
||||
if err := http.Serve(ln, mux); err != nil {
|
||||
log.Printf("HTTP 服务异常退出: %v", err)
|
||||
waitExit()
|
||||
}
|
||||
}
|
||||
|
||||
func openBrowser(url string) {
|
||||
|
||||
Reference in New Issue
Block a user