-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.go
148 lines (143 loc) · 3.68 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
package main
import (
"fmt"
"github.com/OpenIoTHub/server-go/config"
"github.com/OpenIoTHub/server-go/manager"
"github.com/OpenIoTHub/utils/models"
"github.com/OpenIoTHub/utils/net"
"github.com/urfave/cli/v2"
"log"
"os"
"time"
)
var (
version = "dev"
commit = ""
date = ""
builtBy = ""
)
func main() {
myApp := cli.NewApp()
myApp.Name = "server-go"
myApp.Usage = "-c [config File Path]"
myApp.Version = buildVersion(version, commit, date, builtBy)
myApp.Flags = []cli.Flag{
//TODO 应该设置工作目录,各组件共享
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Value: config.DefaultConfigFilePath,
Usage: "config file path",
EnvVars: []string{"ServerConfigFilePath"},
Destination: &config.DefaultConfigFilePath,
},
}
myApp.Commands = []*cli.Command{
{
Name: "generate",
Aliases: []string{"g"},
Usage: "generate one token for gateway and one token for OpenIoTHub",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Value: config.DefaultConfigFilePath,
Usage: "config file path",
EnvVars: []string{"GatewayConfigFilePath"},
Destination: &config.DefaultConfigFilePath,
},
},
Action: func(c *cli.Context) error {
err := config.LoadConfig()
if err != nil {
log.Println(err)
return err
}
GateWayToken, OpenIoTHubToken, err := models.GetTokenByServerConfig(&config.ConfigMode, 200000000000)
if err != nil {
return err
}
log.Println("Generated one pair of token:")
log.Println("注意不要复制了换行符:")
log.Println("====================Gateway Token:->====================")
log.Println(GateWayToken)
log.Println("====================OpenIoTHub Token:->=================")
log.Println(OpenIoTHubToken)
log.Println("========================================")
return nil
},
},
{
Name: "init",
Aliases: []string{"i"},
Usage: "init config file",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Value: config.DefaultConfigFilePath,
Usage: "config file path",
EnvVars: []string{"GatewayConfigFilePath"},
Destination: &config.DefaultConfigFilePath,
},
},
Action: func(c *cli.Context) error {
config.InitConfigFile()
return nil
},
},
{
Name: "test",
Aliases: []string{"t"},
Usage: "test this command",
Action: func(c *cli.Context) error {
fmt.Println("ok")
return nil
},
},
}
myApp.Action = func(c *cli.Context) error {
err := run()
if err != nil {
os.Exit(1)
}
for {
time.Sleep(time.Hour)
}
}
err := myApp.Run(os.Args)
if err != nil {
log.Println(err.Error())
}
}
func run() (err error) {
err = config.LoadConfig()
if err != nil {
log.Println(err)
return
}
manager.InitSessionsCtl()
manager.LoadConfigFromIoTManager()
go manager.SessionsCtl.RunTLS()
go manager.SessionsCtl.RunTCP()
go manager.SessionsCtl.RunKCP()
go manager.SessionsCtl.StartgRpcListenAndServ()
go manager.SessionsCtl.StartHttpListenAndServ()
go nettool.RunUDPApiServer(config.ConfigMode.Common.UdpApiPort)
go nettool.RunKCPApiServer(config.ConfigMode.Common.KcpApiPort)
log.Println("服务器正在运行,网关配置请根据本服务器配置填写!")
return
}
func buildVersion(version, commit, date, builtBy string) string {
var result = version
if commit != "" {
result = fmt.Sprintf("%s\ncommit: %s", result, commit)
}
if date != "" {
result = fmt.Sprintf("%s\nbuilt at: %s", result, date)
}
if builtBy != "" {
result = fmt.Sprintf("%s\nbuilt by: %s", result, builtBy)
}
return result
}