-
Notifications
You must be signed in to change notification settings - Fork 447
/
main.go
184 lines (163 loc) · 4.42 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"log"
"net/http"
"os"
"path/filepath"
"runtime"
"github.com/lonng/nano"
"github.com/lonng/nano/cluster"
"github.com/lonng/nano/examples/cluster/chat"
"github.com/lonng/nano/examples/cluster/gate"
"github.com/lonng/nano/examples/cluster/master"
"github.com/lonng/nano/serialize/json"
"github.com/lonng/nano/session"
"github.com/pingcap/errors"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Name = "NanoClusterDemo"
app.Author = "Lonng"
app.Email = "[email protected]"
app.Description = "Nano cluster demo"
app.Commands = []cli.Command{
{
Name: "master",
Flags: []cli.Flag{
cli.StringFlag{
Name: "listen,l",
Usage: "Master service listen address",
Value: "127.0.0.1:34567",
},
},
Action: runMaster,
},
{
Name: "gate",
Flags: []cli.Flag{
cli.StringFlag{
Name: "master",
Usage: "master server address",
Value: "127.0.0.1:34567",
},
cli.StringFlag{
Name: "listen,l",
Usage: "Gate service listen address",
Value: "",
},
cli.StringFlag{
Name: "gate-address",
Usage: "Client connect address",
Value: "",
},
},
Action: runGate,
},
{
Name: "chat",
Flags: []cli.Flag{
cli.StringFlag{
Name: "master",
Usage: "master server address",
Value: "127.0.0.1:34567",
},
cli.StringFlag{
Name: "listen,l",
Usage: "Chat service listen address",
Value: "",
},
},
Action: runChat,
},
}
log.SetFlags(log.LstdFlags | log.Lshortfile)
if err := app.Run(os.Args); err != nil {
log.Fatalf("Startup server error %+v", err)
}
}
func srcPath() string {
_, file, _, _ := runtime.Caller(0)
return filepath.Dir(file)
}
func runMaster(args *cli.Context) error {
listen := args.String("listen")
if listen == "" {
return errors.Errorf("master listen address cannot empty")
}
webDir := filepath.Join(srcPath(), "master", "web")
log.Println("Nano master server web content directory", webDir)
log.Println("Nano master listen address", listen)
log.Println("Open http://127.0.0.1:12345/web/ in browser")
http.Handle("/web/", http.StripPrefix("/web/", http.FileServer(http.Dir(webDir))))
go func() {
if err := http.ListenAndServe(":12345", nil); err != nil {
panic(err)
}
}()
// Register session closed callback
session.Lifetime.OnClosed(master.OnSessionClosed)
// Startup Nano server with the specified listen address
nano.Listen(listen,
nano.WithMaster(),
nano.WithComponents(master.Services),
nano.WithSerializer(json.NewSerializer()),
nano.WithDebugMode(),
nano.WithUnregisterCallback(func(m cluster.Member) {
log.Println("Todo alarm unregister:", m.String())
}),
)
return nil
}
func runGate(args *cli.Context) error {
listen := args.String("listen")
if listen == "" {
return errors.Errorf("gate listen address cannot empty")
}
masterAddr := args.String("master")
if masterAddr == "" {
return errors.Errorf("master address cannot empty")
}
gateAddr := args.String("gate-address")
if gateAddr == "" {
return errors.Errorf("gate address cannot empty")
}
log.Println("Current server listen address", listen)
log.Println("Current gate server address", gateAddr)
log.Println("Remote master server address", masterAddr)
// Startup Nano server with the specified listen address
nano.Listen(listen,
nano.WithAdvertiseAddr(masterAddr),
nano.WithClientAddr(gateAddr),
nano.WithComponents(gate.Services),
nano.WithSerializer(json.NewSerializer()),
nano.WithIsWebsocket(true),
nano.WithWSPath("/nano"),
nano.WithCheckOriginFunc(func(_ *http.Request) bool { return true }),
nano.WithDebugMode(),
nano.WithNodeId(2), // if you deploy multi gate, option set nodeId, default nodeId = os.Getpid()
)
return nil
}
func runChat(args *cli.Context) error {
listen := args.String("listen")
if listen == "" {
return errors.Errorf("chat listen address cannot empty")
}
masterAddr := args.String("master")
if listen == "" {
return errors.Errorf("master address cannot empty")
}
log.Println("Current chat server listen address", listen)
log.Println("Remote master server address", masterAddr)
// Register session closed callback
session.Lifetime.OnClosed(chat.OnSessionClosed)
// Startup Nano server with the specified listen address
nano.Listen(listen,
nano.WithAdvertiseAddr(masterAddr),
nano.WithComponents(chat.Services),
nano.WithSerializer(json.NewSerializer()),
nano.WithDebugMode(),
)
return nil
}