Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request ThingsPanel#246 from November-12/main
Browse files Browse the repository at this point in the history
配置文件变更
  • Loading branch information
November-12 authored Nov 3, 2023
2 parents f2a1029 + 2be03a2 commit d2481aa
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 142 deletions.
File renamed without changes.
65 changes: 65 additions & 0 deletions conf/app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
log:
# 0-控制台输出 1-文件输出 2-文件和控制台输出
adapter_type: 0
# 文件最多保存多少天
maxdays: 7
# 日志级别 (0-紧急 1-报警 2-严重错误 3-错误 4-警告 5-注意 6-信息 7-调试)
level: 4
# 每个文件保存的最大行数
maxlines: 10000

db:
psql:
psqladdr: "39.98.176.26"
psqlport: 5432
psqldb: ThingsPanel
psqluser: postgres
psqlpass: ThingsPanel2023
psqlMaxConns: 10
psqlMaxOpen: 50

# SQL日志级别 (1-静音 2-错误 3-警告 4-信息). 注意: sql日志只在level大于等于5级别才会输出。
sqlloglevel: 2

# 慢SQL阈值(毫秒)。慢SQL会在sqlloglevel大于等于3时输出。
slow_threshold: 200
redis:
# redis 连接字符串
conn: 39.98.176.26:6379
# redis 数据库号
dbNum: 0
# redis 密码
password: "redis2022"



# GOTP_MQTT_SERVER mqtt服务:gmqtt、vernemq
mqtt_server: gmqtt
mqtt:
broker: 39.98.176.26:1883
user: root
pass: root
topicToSubscribe: device/attributes #属性订阅主题
topicToStatus: device/status #在线离线订阅主题
topicToProgress: ota/device/progress #ota上报订阅主题
topicToEvent: device/event #事件订阅主题
gateway_topic: gateway/attributes #网关订阅、发布主题
topicToPublish: device/attributes #属性发布主题
topicToCommand: device/command #发布主题
topicToInform: ota/device/inform #发布主题
#消息服务质量 0:消息最多传递一次,如果当时客户端不可用,则会丢失该消息。1:消息传递至少 1 次。2:消息仅传送一次。
qos: 0

mqtts:
broker: 39.98.176.26:8883
user: root
pass: root
caPath : ./conf/certificate/ca.crt
crtPath: ./conf/certificate/client.crt
keyPath : ./conf/certificate/client.key

api:
http_host: 39.98.176.26:8083

grpc:
tptodb_server: 39.98.176.26:50052
35 changes: 6 additions & 29 deletions initialize/casbin/casbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import (
_ "ThingsPanel-Go/initialize/psql"
"fmt"
"log"
"os"
"strconv"

beego "github.com/beego/beego/v2/server/web"
"github.com/casbin/casbin/v2"
gormadapter "github.com/casbin/gorm-adapter/v3"
"github.com/spf13/viper"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
Expand All @@ -18,32 +16,11 @@ var CasbinEnforcer *casbin.Enforcer

func init() {
log.Println("casbin启动...")

// 从配置文件中获取PostgreSQL数据库的配置信息
psqluser, err := beego.AppConfig.String("psqluser")
checkErr(err, "Failed to get psqluser")

psqlpass, err := beego.AppConfig.String("psqlpass")
checkErr(err, "Failed to get psqlpass")

psqladdr := os.Getenv("TP_PG_IP")
if psqladdr == "" {
psqladdr, err = beego.AppConfig.String("psqladdr")
checkErr(err, "Failed to get psqladdr")
}

psqlports := os.Getenv("TP_PG_PORT")
var psqlport int
if psqlports == "" {
psqlport, err = beego.AppConfig.Int("psqlport")
checkErr(err, "Failed to get psqlport")
} else {
psqlport, err = strconv.Atoi(psqlports)
checkErr(err, "Failed to convert psqlports to int")
}

psqldb, err := beego.AppConfig.String("psqldb")
checkErr(err, "Failed to get psqldb")
psqladdr := viper.GetString("db.psql.psqladdr")
psqlport := viper.GetInt("db.psql.psqlport")
psqluser := viper.GetString("db.psql.psqluser")
psqlpass := viper.GetString("db.psql.psqlpass")
psqldb := viper.GetString("db.psql.psqldb")

// 构建数据源字符串
dataSource := fmt.Sprintf("host=%s port=%d user=%s dbname=%s password=%s sslmode=disable TimeZone=Asia/Shanghai",
Expand Down
24 changes: 24 additions & 0 deletions initialize/conf/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package conf

import (
"fmt"
"log"
"strings"

"github.com/spf13/viper"
)

func init() {
log.Println("系统配置文件初始化...")
viper.SetEnvPrefix("TP")
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetConfigName("./conf/app")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("failed to read configuration file: %s", err))
}
log.Println("系统配置文件初始化完成")
}
26 changes: 6 additions & 20 deletions initialize/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,17 @@ import (
"time"

"github.com/beego/beego/v2/core/logs"
beego "github.com/beego/beego/v2/server/web"
"github.com/spf13/viper"
)

func init() {
log.SetFlags(log.Lshortfile | log.Ltime | log.Ldate)
log.Println("系统日志初始化...")

dateStr := time.Now().Format("2006-01-02")

maxdays, err := beego.AppConfig.Int("maxdays")
if err != nil {
log.Println("无法获取maxdays:", err)
}

level, err := beego.AppConfig.Int("level")
if err != nil {
log.Println("无法获取level:", err)
}

maxlines, err := beego.AppConfig.Int("maxlines")
if err != nil {
log.Println("无法获取maxlines:", err)
}
maxdays := viper.GetInt("log.maxdays")
level := viper.GetInt("log.level")
maxlines := viper.GetInt("log.maxlines")

dataSource := &struct {
Filename string `json:"filename"`
Expand All @@ -53,10 +41,8 @@ func init() {
log.Println("无法创建dataSource:", err)
}
logs.SetLevel(level)
adapter_type, err := beego.AppConfig.Int("adapter_type")
if err != nil {
log.Println("无法获取adapter_type:", err)
}
adapter_type := viper.GetInt("log.adapter_type")

// 日志输出选择
switch adapter_type {
case 0:
Expand Down
66 changes: 12 additions & 54 deletions initialize/psql/psql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package psql
import (
"fmt"
"log"
"os"
"strconv"
"time"

"gorm.io/gorm/logger"

"github.com/beego/beego/v2/core/logs"
beego "github.com/beego/beego/v2/server/web"
"github.com/spf13/viper"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
Expand All @@ -31,65 +29,25 @@ func (w Writer) Printf(format string, args ...interface{}) {
// 设置psql
func init() {
log.Println("连接数据库...")
psqladdr := os.Getenv("TP_PG_IP")
var err error
if psqladdr == "" {
psqladdr, err = beego.AppConfig.String("psqladdr")
if err != nil {
log.Fatalf("无法获取psqladdr: %v", err)
}
}

psqlportStr := os.Getenv("TP_PG_PORT")
psqlport, err := strconv.Atoi(psqlportStr)
if err != nil || psqlport == 0 {
psqlport, err = beego.AppConfig.Int("psqlport")
if err != nil {
log.Fatalf("无法获取psqlport: %v", err)
}
}

psqluser, err := beego.AppConfig.String("psqluser")
if err != nil {
log.Fatalf("无法获取psqluser: %v", err)
}

psqlpass, err := beego.AppConfig.String("psqlpass")
if err != nil {
log.Fatalf("无法获取psqlpass: %v", err)
}

psqldb, err := beego.AppConfig.String("psqldb")
if err != nil {
log.Fatalf("无法获取psqldb: %v", err)
}

psqlMaxConns, err := beego.AppConfig.Int("psqlMaxConns")
if err != nil {
log.Fatalf("无法获取psqlMaxConns: %v", err)
}

psqlMaxOpen, err := beego.AppConfig.Int("psqlMaxOpen")
if err != nil {
log.Fatalf("无法获取psqlMaxOpen: %v", err)
}

sqlloglevel, err := beego.AppConfig.Int("sqlloglevel")
if err != nil {
log.Fatalf("无法获取sqlloglevel: %v", err)
}

psqladdr := viper.GetString("db.psql.psqladdr")
psqlport := viper.GetInt("db.psql.psqlport")
psqluser := viper.GetString("db.psql.psqluser")
psqlpass := viper.GetString("db.psql.psqlpass")
psqldb := viper.GetString("db.psql.psqldb")
psqlMaxConns := viper.GetInt("db.psql.psqlMaxConns")
psqlMaxOpen := viper.GetInt("db.psql.psqlMaxOpen")
sqlloglevel := viper.GetInt("db.psql.sqlloglevel")
dataSource := fmt.Sprintf("host=%s port=%d dbname=%s user=%s password=%s sslmode=disable TimeZone=Asia/Shanghai",
psqladdr,
psqlport,
psqldb,
psqluser,
psqlpass,
)
slow_threshold, err := beego.AppConfig.Int("slow_threshold")
if err != nil {
log.Fatalf("无法获取slow_threshold: %v", err)
}

slow_threshold := viper.GetInt("db.psql.slow_threshold")

//设置gorm日志规则
newLogger := logger.New(
// log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer 单独设置grom日志输出
Expand Down
13 changes: 5 additions & 8 deletions initialize/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package redis
import (
"ThingsPanel-Go/utils"
"log"
"os"
"time"

"github.com/beego/beego/v2/core/logs"
"github.com/beego/beego/v2/server/web"
"github.com/spf13/viper"
"gopkg.in/redis.v5"
)

Expand Down Expand Up @@ -39,12 +38,10 @@ func createClient(redisHost string, password string, dataBase int) *redis.Client
}

func init() {
redisHost := os.Getenv("TP_REDIS_HOST")
if redisHost == "" {
redisHost, _ = web.AppConfig.String("redis.conn")
}
dataBase, _ := web.AppConfig.Int("redis.dbNum")
password, _ := web.AppConfig.String("redis.password")
redisHost := viper.GetString("db.redis.conn")
dataBase := viper.GetInt("db.redis.dbNum")
password := viper.GetString("db.redis.password")

redisCache = createClient(redisHost, password, dataBase)
}

Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package main
import (
"log"

// 初始化配置,顺序似乎要排第一个
_ "ThingsPanel-Go/initialize/conf"

_ "ThingsPanel-Go/initialize/log"
_ "ThingsPanel-Go/initialize/psql"

Expand Down
File renamed without changes.
34 changes: 3 additions & 31 deletions modules/dataService/dataService.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@ import (
"ThingsPanel-Go/modules/dataService/mqtt"
mqtts "ThingsPanel-Go/modules/dataService/mqtts/connect"
tphttp "ThingsPanel-Go/others/http"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"

"github.com/spf13/viper"
)

func init() {
loadConfig()
log.Println("注册mqtt用户...")
//是否gmqtt
if viper.GetString("mqtt_server") == "gmqtt" {
Expand All @@ -31,29 +26,8 @@ func init() {
// listenTCP()
}

func loadConfig() {
log.Println("read config")
var err error
envConfigFile := flag.String("config", "./modules/dataService/config.yml", "path of configuration file")
flag.Parse()
// 设置环境变量前缀
viper.SetEnvPrefix("GOTP")
// 使 Viper 能够读取环境变量
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetConfigFile(*envConfigFile)
if err = viper.ReadInConfig(); err != nil {
fmt.Println("FAILURE", err)
return
}
}

func listenMQTTNew() {
mqttHost := os.Getenv("TP_MQTT_HOST")
if mqttHost == "" {
mqttHost = viper.GetString("mqtt.broker")
}
broker := mqttHost
broker := viper.GetString("mqtt.broker")
user := viper.GetString("mqtt.user")
pass := viper.GetString("mqtt.pass")
go mqtt.ListenNew(broker, user, pass)
Expand All @@ -76,10 +50,8 @@ func ListenTCP() {
//tcp.Listen(tcpPort)
}
func reg_mqtt_root() {
MqttHttpHost := os.Getenv("MQTT_HTTP_HOST")
if MqttHttpHost == "" {
MqttHttpHost = viper.GetString("api.http_host")
}

MqttHttpHost := viper.GetString("api.http_host")
resps, errs := tphttp.Post("http://"+MqttHttpHost+"/v1/accounts/root", "{\"password\":\""+viper.GetString("mqtt.pass")+"\"}")
if errs != nil {
log.Println("失败:", errs.Error())
Expand Down

0 comments on commit d2481aa

Please sign in to comment.