fix: comprehensive bug fixes and architecture restructuring
Phase 1 — Bug fixes (7 bugs): - Bug 1: Pulsar mode data loss — always persist to SQLite regardless of mode - Bug 2: PulsarSend closing TCP client — use independent resetPulsarProducer() - Bug 3: Greedy regex — use non-greedy (?s)ZCZC.*?NNNN - Bug 4: Data after NNNN discarded — keep remaining buffer data - Bug 5: strings.Index > 0 boundary — use strings.Contains - Bug 6: Variable shadowing in PulsarSend — use = not := - Bug 7: Accept failure nil panic — add continue + retry logic Phase 2 — Architecture restructuring: - Split utils/ into config/, serial/, telegram/, storage/, transport/ - Define Sender, Repository, Reader interfaces - Introduce app/ layer with context.Context lifecycle - Replace spinlock with sync.Mutex - Unified Config struct replaces 15+ global vars Phase 3 — Testing & tooling: - telegram/parser_test.go (7 test cases) - storage/store_test.go (5 test cases) - transport/transport_test.go (4 test cases) - Taskfile: add test, test-race, test-cover tasks - Go version: 1.15 -> 1.21
This commit is contained in:
+35
-59
@@ -25,43 +25,39 @@ import (
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"it2000.com.cn/tele-recv/config"
|
||||
"it2000.com.cn/tele-recv/utils"
|
||||
)
|
||||
|
||||
const ModeName = "TELEGRAM_MODE"
|
||||
|
||||
var (
|
||||
cfgFile string
|
||||
device string
|
||||
baudrate int
|
||||
lograw bool
|
||||
|
||||
dbFile string
|
||||
dbInit bool
|
||||
cfgFile string
|
||||
|
||||
// Legacy global vars for backward compatibility with test command
|
||||
device string
|
||||
baudrate int
|
||||
dbFile string
|
||||
dbInit bool
|
||||
socketAddress string
|
||||
pulsarUrl string
|
||||
topic string
|
||||
name string
|
||||
tcp bool
|
||||
pulsar bool
|
||||
lograw bool
|
||||
|
||||
pulsarUrl string
|
||||
topic string
|
||||
name string
|
||||
|
||||
tcp bool
|
||||
pulsar bool
|
||||
Mode string
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
rootCmd = &cobra.Command{
|
||||
Use: "tele-recv",
|
||||
Short: "telegram receiver",
|
||||
Long: `A serial port telegram receiver.
|
||||
Read the telegram from `,
|
||||
// Uncomment the following line if your bare application
|
||||
// has an action associated with it:
|
||||
// Run: func(cmd *cobra.Command, args []string) { },
|
||||
Read the telegram from serial port`,
|
||||
}
|
||||
|
||||
Mode = os.Getenv(ModeName)
|
||||
|
||||
appCfg *config.Config
|
||||
logger *zap.Logger
|
||||
)
|
||||
|
||||
@@ -77,55 +73,36 @@ func Execute() {
|
||||
func init() {
|
||||
cobra.OnInitialize(initConfig)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
// Cobra supports persistent flags, which, if defined here,
|
||||
// will be global for your application.
|
||||
|
||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (telegram.yaml)")
|
||||
// Cobra also supports local flags, which will only run
|
||||
// when this action is called directly.
|
||||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
|
||||
initLog()
|
||||
defer logger.Sync()
|
||||
// l = logger.Sugar()
|
||||
utils.Log = logger
|
||||
|
||||
}
|
||||
|
||||
// initConfig reads in config file and ENV variables if set.
|
||||
func initConfig() {
|
||||
if cfgFile != "" {
|
||||
// Use config file from the flag.
|
||||
viper.SetConfigFile(cfgFile)
|
||||
} else {
|
||||
// Search config in home directory with name ".tele-recv" (without extension).
|
||||
viper.AddConfigPath(".")
|
||||
viper.SetConfigName("telegram")
|
||||
}
|
||||
|
||||
viper.AutomaticEnv() // read in environment variables that match
|
||||
|
||||
// If a config file is found, read it in.
|
||||
if err := viper.ReadInConfig(); err == nil {
|
||||
// fmt.Println("Using config file:", viper.ConfigFileUsed())
|
||||
logger.Info("Using config file ", zap.String("path", viper.ConfigFileUsed()))
|
||||
device = viper.GetString("serial.device")
|
||||
baudrate = viper.GetInt("serial.baudrate")
|
||||
lograw = viper.GetBool("serial.lograw")
|
||||
dbFile = viper.GetString("sqlite.file")
|
||||
dbInit = viper.GetBool("sqlite.init")
|
||||
socketAddress = viper.GetString("socket.address")
|
||||
pulsarUrl = viper.GetString("pulsar.url")
|
||||
topic = viper.GetString("pulsar.topic")
|
||||
name = viper.GetString("pulsar.name")
|
||||
|
||||
tcp = viper.GetBool("telegram.tcp")
|
||||
utils.Tcp = tcp
|
||||
pulsar = viper.GetBool("telegram.pulsar")
|
||||
utils.Pulsar = pulsar
|
||||
cfg, err := config.Load(cfgFile)
|
||||
if err != nil {
|
||||
logger.Warn("config load error, using defaults", zap.Error(err))
|
||||
return
|
||||
}
|
||||
appCfg = cfg
|
||||
|
||||
// Populate legacy globals for backward compatibility
|
||||
device = cfg.Serial.Device
|
||||
baudrate = cfg.Serial.Baudrate
|
||||
lograw = cfg.Serial.LogRaw
|
||||
dbFile = cfg.SQLite.File
|
||||
dbInit = cfg.SQLite.Init
|
||||
socketAddress = cfg.Socket.Address
|
||||
pulsarUrl = cfg.Pulsar.URL
|
||||
topic = cfg.Pulsar.Topic
|
||||
name = cfg.Pulsar.Name
|
||||
tcp = cfg.Telegram.TCP
|
||||
pulsar = cfg.Telegram.Pulsar
|
||||
utils.Tcp = tcp
|
||||
utils.Pulsar = pulsar
|
||||
}
|
||||
|
||||
func initLog() {
|
||||
@@ -163,5 +140,4 @@ func initLog() {
|
||||
)
|
||||
|
||||
logger = zap.New(core)
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user