forked from ehang-io/nps
-
Notifications
You must be signed in to change notification settings - Fork 1
/
json.go
executable file
·51 lines (47 loc) · 942 Bytes
/
json.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
package main
import (
"encoding/json"
"errors"
"io/ioutil"
)
//定义配置文件解析后的结构
type Server struct {
Ip string
Port int
Tcp int
Vkey string
Num int
}
type Site struct {
Host string
Url string
Port int
}
type Config struct {
Server Server
SiteList []Site
Replace int
}
type JsonStruct struct {
}
func NewJsonStruct() *JsonStruct {
return &JsonStruct{}
}
func (jst *JsonStruct) Load(filename string) (Config, error) {
data, err := ioutil.ReadFile(filename)
config := Config{}
if err != nil {
return config, errors.New("配置文件打开错误")
}
err = json.Unmarshal(data, &config)
if err != nil {
return config, errors.New("配置文件解析错误")
}
if config.Server.Tcp <= 0 || config.Server.Tcp >= 65536 {
return config, errors.New("请输入正确的tcp端口")
}
if config.Server.Vkey == "" {
return config, errors.New("密钥不能为空!")
}
return config, nil
}