-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
65 lines (61 loc) · 1.5 KB
/
Copy pathmain.go
File metadata and controls
65 lines (61 loc) · 1.5 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
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"tenon/pkg/rpcc"
"tenon/pkg/rpcstate"
"time"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { return true },
}
func main() {
ctx, exit := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer exit()
devMode := len(os.Args) > 1 && os.Args[1] == "--dev"
mux := http.NewServeMux()
if !devMode {
mux.Handle("/", http.FileServer(http.Dir("dist")))
}
mux.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Printf("WebSocket连接升级错误: %v\n", err)
return
}
defer conn.Close()
rpcc := rpcc.New(conn)
rpcstate.New().Register(rpcc)
if err := rpcc.Serve(); err != nil {
fmt.Printf("RPC服务错误: %v\n", err)
}
})
server := http.Server{
Addr: "localhost:10270",
Handler: mux,
}
go func() {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
fmt.Printf("服务出错: %v\n", err)
os.Exit(1)
}
}()
if devMode {
fmt.Println("开发模式, WebSocket: ws://localhost:10270/ws")
} else {
fmt.Println("WebUI: http://localhost:10270")
}
fmt.Println("按 Ctrl+C 退出")
<-ctx.Done()
ctx, closeServer := context.WithTimeout(context.Background(), 5*time.Second)
defer closeServer()
if err := server.Shutdown(ctx); err != nil {
fmt.Printf("服务关闭时出错: %v\n", err)
os.Exit(1)
}
}