2025-01-11 22:34:36 +08:00
|
|
|
|
package proto
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2025-01-14 15:22:34 +08:00
|
|
|
|
"VideoStream/worker"
|
2025-01-11 22:34:36 +08:00
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"os"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var Config ConfigStruct
|
2025-01-14 15:00:40 +08:00
|
|
|
|
var Url_map = map[string]bool{} // 不需要token验证的url
|
|
|
|
|
|
var SigningKey = []byte{}
|
2025-01-11 22:34:36 +08:00
|
|
|
|
|
|
|
|
|
|
type ConfigStruct struct {
|
2025-01-14 15:00:40 +08:00
|
|
|
|
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"`
|
2025-01-11 22:34:36 +08:00
|
|
|
|
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" // 默认端口
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-01-14 15:22:34 +08:00
|
|
|
|
//将配置写入redis
|
2025-01-14 15:36:15 +08:00
|
|
|
|
jsonData, err2 := json.Marshal(Config)
|
|
|
|
|
|
if err2 != nil {
|
|
|
|
|
|
fmt.Println("ReadConfigToSetSystem Error encoding config,err :", err2)
|
|
|
|
|
|
}
|
|
|
|
|
|
worker.SetRedis("vp_stream_config", string(jsonData))
|
2025-01-14 15:00:40 +08:00
|
|
|
|
SigningKey = []byte(Config.TOKEN_SECRET)
|
|
|
|
|
|
if Config.TOKEN_SECRET == "" {
|
2025-01-14 15:39:43 +08:00
|
|
|
|
err = fmt.Errorf("token secret is empty: %s", string(jsonData))
|
2025-01-14 15:00:40 +08:00
|
|
|
|
}
|
2025-01-11 22:34:36 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|