-
Notifications
You must be signed in to change notification settings - Fork 2
/
channel.go
55 lines (48 loc) · 1.43 KB
/
channel.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
package commands
import (
"syscall"
"github.com/juju/errors"
"github.com/ngaut/log"
"github.com/spf13/cobra"
"github.com/sryanyuan/ForeverMS/core/gosync"
"github.com/sryanyuan/ForeverMS/core/server/channel"
)
type channelCommandOptions struct {
config string
}
func executeChannelCommand(cmd *cobra.Command, options *channelCommandOptions) {
var err error
// Parse config file
var config channel.Config
if err = config.LoadFromFile(options.config); nil != err {
log.Errorf("Load config from file [%s] error: %v",
options.config, err)
return
}
server := channel.NewChannelServer(&config)
ctx := gosync.NewContextWithCancel()
if err = server.Serve(ctx); nil != err {
log.Errorf("Channel server serve error: %v",
errors.ErrorStack(err))
return
}
log.Infof("Channel server serve at %v", config.ListenClients)
sig := gosync.WaitSignals(syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
log.Infof("Receive signal %v", sig)
server.Stop()
ctx.Cancel()
ctx.Wait()
}
func NewChannelCommand() *cobra.Command {
var options channelCommandOptions
channelCommand := &cobra.Command{
Use: "channel",
Short: "Run channel server with specified address",
Long: "Run channel server with specified address",
Run: func(cmd *cobra.Command, args []string) {
executeChannelCommand(cmd, &options)
},
}
channelCommand.Flags().StringVarP(&options.config, "config", "c", "", "Config file path")
return channelCommand
}