73 lines
2.2 KiB
Go
73 lines
2.2 KiB
Go
package proto
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"os"
|
||
)
|
||
|
||
var Config ConfigStruct
|
||
|
||
type ConfigStruct struct {
|
||
REDIS_ADDR string `json:"redis_addr"`
|
||
TOKEN_USE_REDIS bool `json:"token_use_redis"`
|
||
REDIS_User_PW bool `json:"redis_user_pw"` // 是否使用密码
|
||
REDIS_PASSWORD string `json:"redis_password"`
|
||
REDIS_DB int `json:"redis_db"`
|
||
SERVER_PORT string `json:"SERVER_PORT" form:"SERVER_PORT"` // 服务器端口
|
||
DeviceInfo []DeviceInfo `json:"device_info" form:"device_info"` // 设备信息
|
||
}
|
||
|
||
type DeviceInfo struct {
|
||
ID int `json:"id" form:"id"`
|
||
Name string `json:"name" form:"name"`
|
||
IP string `json:"ip" form:"ip"`
|
||
Type string `json:"type" form:"type"`
|
||
Stream string `json:"stream" form:"stream"`
|
||
Control string `json:"control" form:"control"`
|
||
NextStop bool `json:"next_stop" form:"next_stop"` // 下一帧,是否停止
|
||
CheckFrameWidth int `json:"check_frame_width" form:"check_frame_width"` // 检测帧宽度
|
||
CheckFrameHeight int `json:"check_frame_height" form:"check_frame_height"` // 检测帧高度
|
||
LogFrame int `json:"log_frame" form:"log_frame"` // 日志帧间隔,默认值为0,即不输出日志
|
||
}
|
||
|
||
// 读取配置文件
|
||
func ReadConfig(path string) error {
|
||
//查看配置文件是否存在,不存在则创建
|
||
_, err := os.Stat(path)
|
||
if err != nil {
|
||
fmt.Println("Config file not found!")
|
||
//写入json文件
|
||
file, err := os.Create(path)
|
||
if err != nil {
|
||
fmt.Println("Error creating config file")
|
||
return err
|
||
}
|
||
defer file.Close()
|
||
encoder := json.NewEncoder(file)
|
||
err = encoder.Encode(&Config)
|
||
if err != nil {
|
||
fmt.Println("Error encoding config")
|
||
}
|
||
return err
|
||
}
|
||
|
||
//读json文件
|
||
file, err := os.Open(path)
|
||
if err != nil {
|
||
fmt.Println("Error opening config file")
|
||
return err
|
||
}
|
||
defer file.Close()
|
||
decoder := json.NewDecoder(file)
|
||
err = decoder.Decode(&Config)
|
||
if err != nil {
|
||
fmt.Println("Error decoding config")
|
||
} else {
|
||
if Config.SERVER_PORT == "" {
|
||
Config.SERVER_PORT = "5002" // 默认端口
|
||
}
|
||
}
|
||
return err
|
||
}
|