-
Notifications
You must be signed in to change notification settings - Fork 44
/
main.go
185 lines (179 loc) · 4.55 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
185
package main
import (
"fmt"
client "github.com/OpenIoTHub/gateway-go/client"
"github.com/OpenIoTHub/gateway-go/config"
"github.com/OpenIoTHub/gateway-go/services"
"github.com/OpenIoTHub/gateway-go/utils/qr"
"github.com/OpenIoTHub/gateway-go/utils/str"
utils_models "github.com/OpenIoTHub/utils/models"
uuid "github.com/satori/go.uuid"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
"io"
"log"
"os"
"sync"
)
var (
version = ""
commit = ""
date = ""
builtBy = ""
)
func main() {
client.IsLibrary = false
myApp := cli.NewApp()
myApp.Name = "gateway-go"
myApp.Usage = "-c [config file path]"
myApp.Version = str.BuildVersion(version, commit, date, builtBy)
myApp.Commands = []*cli.Command{
{
Name: "init",
Aliases: []string{"i"},
Usage: "init config file",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Value: config.ConfigFilePath,
Usage: "config file path",
EnvVars: []string{"GatewayConfigFilePath"},
Destination: &config.ConfigFilePath,
},
},
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
},
},
// TODO 与当前本机运行的服务进行通信查询一些信息,比如id,token,log等
// TODO 查询本机所在网络所包含的支持的服务
}
myApp.Flags = []cli.Flag{
//TODO 应该设置工作目录,各组件共享
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Value: config.ConfigFilePath,
Usage: "config file path",
EnvVars: []string{"GatewayConfigFilePath"},
Destination: &config.ConfigFilePath,
},
//token 登录
&cli.StringFlag{
Name: "token",
Aliases: []string{"t"},
Value: config.GatewayLoginToken,
Usage: "login server by gateway token ",
EnvVars: []string{"GatewayLoginToken"},
Destination: &config.GatewayLoginToken,
},
}
myApp.Action = func(c *cli.Context) error {
if config.GatewayLoginToken != "" {
UseGateWayToken()
} else {
_, err := os.Stat(config.ConfigFilePath)
if err != nil {
config.InitConfigFile()
}
UseConfigFile()
}
go client.Run()
wg := sync.WaitGroup{}
wg.Add(1)
wg.Wait()
return nil
}
err := myApp.Run(os.Args)
if err != nil {
log.Println(err.Error())
}
}
//export Run
func Run() {
go client.Run()
}
func UseGateWayToken() {
//使用服务器签发的Token登录
err := services.GatewayManager.AddServer(config.GatewayLoginToken)
if err != nil {
log.Printf(err.Error())
log.Printf("登陆失败!请重新登陆。")
return
}
log.Printf("登陆成功!\n")
}
func UseConfigFile() {
//配置文件存在
log.Println("使用的配置文件位置:", config.ConfigFilePath)
content, err := os.ReadFile(config.ConfigFilePath)
if err != nil {
log.Println(err.Error())
return
}
err = yaml.Unmarshal(content, &config.ConfigMode)
if err != nil {
log.Println(err.Error())
return
}
//找到了配置文件
if len(config.ConfigMode.GatewayUUID) < 35 {
config.ConfigMode.GatewayUUID = uuid.Must(uuid.NewV4()).String()
err = config.WriteConfigFile(config.ConfigMode, config.ConfigFilePath)
if err != nil {
log.Println(err.Error())
}
}
if config.ConfigMode.LoginWithTokenMap == nil {
config.ConfigMode.LoginWithTokenMap = make(map[string]string)
}
//解析日志配置
writers := []io.Writer{}
if config.ConfigMode.LogConfig.EnableStdout {
writers = append(writers, os.Stdout)
}
if config.ConfigMode.LogConfig.LogFilePath != "" {
f, err := os.OpenFile(config.ConfigMode.LogConfig.LogFilePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Fatal(err)
}
writers = append(writers, f)
}
fileAndStdoutWriter := io.MultiWriter(writers...)
log.SetOutput(fileAndStdoutWriter)
//解析配置文件,解析服务器配置文件列表
//解析登录token列表
//如果CLI模式尚未登录自动登陆服务器并创建一个二维码
if len(config.ConfigMode.LoginWithTokenMap) == 0 {
err = qr.AutoLoginAndDisplayQRCode()
if err != nil {
log.Println(err)
}
}
for _, v := range config.ConfigMode.LoginWithTokenMap {
err = services.GatewayManager.AddServer(v)
if err != nil {
continue
}
// 通过gateway jwt(UUID)展示二维码
tokenModel, err := utils_models.DecodeUnverifiedToken(v)
if err != nil {
return
}
err = qr.DisplayQRCodeById(tokenModel.RunId)
if err != nil {
continue
}
}
}