Просмотр исходного кода

refactor(config): Up Config Load

lejianwen 1 год назад
Родитель
Сommit
a7e4ce4f72
3 измененных файлов с 41 добавлено и 16 удалено
  1. 0 3
      cmd/apimain.go
  2. 5 2
      config/config.go
  3. 36 11
      config/rustdesk.go

+ 0 - 3
cmd/apimain.go

@@ -102,9 +102,6 @@ func InitGlobal() {
102 102
 	//配置解析
103 103
 	global.Viper = config.Init(&global.Config, global.ConfigPath)
104 104
 
105
-	//从配置文件中加载密钥
106
-	config.LoadKeyFile(&global.Config.Rustdesk)
107
-
108 105
 	//日志
109 106
 	global.Logger = logger.New(&logger.Config{
110 107
 		Path:         global.Config.Logger.Path,

+ 5 - 2
config/config.go

@@ -40,7 +40,7 @@ type Config struct {
40 40
 }
41 41
 
42 42
 // Init 初始化配置
43
-func Init(rowVal interface{}, path string) *viper.Viper {
43
+func Init(rowVal *Config, path string) *viper.Viper {
44 44
 	if path == "" {
45 45
 		path = DefaultConfig
46 46
 	}
@@ -61,11 +61,14 @@ func Init(rowVal interface{}, path string) *viper.Viper {
61 61
 		if err2 := v.Unmarshal(rowVal); err2 != nil {
62 62
 			fmt.Println(err2)
63 63
 		}
64
+		rowVal.Rustdesk.LoadKeyFile()
65
+		rowVal.Rustdesk.ParsePort()
64 66
 	})
65 67
 	if err := v.Unmarshal(rowVal); err != nil {
66 68
 		fmt.Println(err)
67 69
 	}
68
-
70
+	rowVal.Rustdesk.LoadKeyFile()
71
+	rowVal.Rustdesk.ParsePort()
69 72
 	return v
70 73
 }
71 74
 

+ 36 - 11
config/rustdesk.go

@@ -2,31 +2,56 @@ package config
2 2
 
3 3
 import (
4 4
 	"os"
5
+	"strconv"
6
+	"strings"
7
+)
8
+
9
+const (
10
+	DefaultIdServerPort    = 21116
11
+	DefaultRelayServerPort = 21117
5 12
 )
6 13
 
7 14
 type Rustdesk struct {
8
-	IdServer    string `mapstructure:"id-server"`
9
-	RelayServer string `mapstructure:"relay-server"`
10
-	ApiServer   string `mapstructure:"api-server"`
11
-	Key         string `mapstructure:"key"`
12
-	KeyFile     string `mapstructure:"key-file"`
13
-	Personal    int    `mapstructure:"personal"`
15
+	IdServer     string `mapstructure:"id-server"`
16
+	IdServerPort int    `mapstructure:"-"`
17
+	RelayServer  string `mapstructure:"relay-server"`
18
+	RelayPort    int    `mapstructure:"-"`
19
+	ApiServer    string `mapstructure:"api-server"`
20
+	Key          string `mapstructure:"key"`
21
+	KeyFile      string `mapstructure:"key-file"`
22
+	Personal     int    `mapstructure:"personal"`
14 23
 	//webclient-magic-queryonline
15 24
 	WebclientMagicQueryonline int `mapstructure:"webclient-magic-queryonline"`
16 25
 }
17 26
 
18
-func LoadKeyFile(rustdesk *Rustdesk) {
27
+func (rd *Rustdesk) LoadKeyFile() {
19 28
 	// Load key file
20
-	if rustdesk.Key != "" {
29
+	if rd.Key != "" {
21 30
 		return
22 31
 	}
23
-	if rustdesk.KeyFile != "" {
32
+	if rd.KeyFile != "" {
24 33
 		// Load key from file
25
-		b, err := os.ReadFile(rustdesk.KeyFile)
34
+		b, err := os.ReadFile(rd.KeyFile)
26 35
 		if err != nil {
27 36
 			return
28 37
 		}
29
-		rustdesk.Key = string(b)
38
+		rd.Key = string(b)
30 39
 		return
31 40
 	}
32 41
 }
42
+func (rd *Rustdesk) ParsePort() {
43
+	// Parse port
44
+	idres := strings.Split(rd.IdServer, ":")
45
+	if len(idres) == 1 {
46
+		rd.IdServerPort = DefaultIdServerPort
47
+	} else if len(idres) == 2 {
48
+		rd.IdServerPort, _ = strconv.Atoi(idres[1])
49
+	}
50
+
51
+	relayres := strings.Split(rd.RelayServer, ":")
52
+	if len(relayres) == 1 {
53
+		rd.RelayPort = DefaultRelayServerPort
54
+	} else if len(relayres) == 2 {
55
+		rd.RelayPort, _ = strconv.Atoi(relayres[1])
56
+	}
57
+}