package main import ( "errors" "git.noahlan.cn/northlan/ngs" "git.noahlan.cn/northlan/ngs/examples/cluster/chat" "git.noahlan.cn/northlan/ngs/examples/cluster/gate" "git.noahlan.cn/northlan/ngs/examples/cluster/master" "git.noahlan.cn/northlan/ngs/internal/log" "git.noahlan.cn/northlan/ngs/serialize/json" "git.noahlan.cn/northlan/ngs/session" "github.com/urfave/cli" "net/http" "os" "path/filepath" "runtime" ) func main() { app := cli.NewApp() app.Name = "ClusterDemo" app.Author = "NorthLan" app.Email = "6995syu@163.com" app.Description = "cluster demo" app.Commands = []cli.Command{ { Name: "master", Flags: []cli.Flag{ cli.StringFlag{ Name: "listen,l", Usage: "--l --listen :8888 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 runMaster(args *cli.Context) error { listen := args.String("listen") if listen == "" { return errors.New("master listen address cannot empty") } webDir := filepath.Join(srcPath(), "master", "web") log.Println("Ngs master server web content directory", webDir) log.Println("Ngs 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 Ngs server with the specified listen address ngs.Listen(listen, ngs.WithMaster(), ngs.WithComponents(master.Services), ngs.WithSerializer(json.NewSerializer()), ngs.WithDebugMode(), ) return nil } func srcPath() string { _, file, _, _ := runtime.Caller(0) return filepath.Dir(file) } func runGate(args *cli.Context) error { listen := args.String("listen") if listen == "" { return errors.New("gate listen address cannot empty") } masterAddr := args.String("master") if masterAddr == "" { return errors.New("master address cannot empty") } gateAddr := args.String("gate-address") if gateAddr == "" { return errors.New("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 Ngs server with the specified listen address ngs.Listen(listen, ngs.WithAdvertiseAddr(masterAddr), ngs.WithClientAddr(gateAddr), ngs.WithComponents(gate.Services), ngs.WithSerializer(json.NewSerializer()), ngs.WithIsWebsocket(true), ngs.WithWSPath("/ngs"), ngs.WithCheckOriginFunc(func(_ *http.Request) bool { return true }), ngs.WithDebugMode(), ) return nil } func runChat(args *cli.Context) error { listen := args.String("listen") if listen == "" { return errors.New("chat listen address cannot empty") } masterAddr := args.String("master") if listen == "" { return errors.New("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 Ngs server with the specified listen address ngs.Listen(listen, ngs.WithAdvertiseAddr(masterAddr), ngs.WithComponents(chat.Services), ngs.WithSerializer(json.NewSerializer()), ngs.WithDebugMode(), ) return nil }