VideoStream/proto/conf.go

88 lines
2.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package proto
import (
"encoding/json"
"fmt"
"os"
)
var Config ConfigStruct
var Url_map = map[string]bool{} // 不需要token验证的url
var SigningKey = []byte{}
type ConfigStruct struct {
DB int `json:"db"` // 0: mysql, 1: pg
MYSQL_DSN string `json:"mysql_dsn"`
PG_DSN string `json:"pg_dsn"`
TOKEN_SECRET string `json:"token_secret"`
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" // 默认端口
}
}
//将配置写入redis
jsonData, err2 := json.Marshal(Config)
if err2 != nil {
fmt.Println("ReadConfigToSetSystem Error encoding config,err :", err2)
}
SigningKey = []byte(Config.TOKEN_SECRET)
if Config.TOKEN_SECRET == "" {
err = fmt.Errorf("token secret is empty: %s", string(jsonData))
}
return err
}