Files
tele-recv/config/loader.go
T
w1ndyb0y 419f25f0dd 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
2026-07-10 15:32:34 +08:00

54 lines
1.0 KiB
Go

package config
import (
"github.com/spf13/viper"
)
// Load reads configuration from telegram.yaml using viper.
func Load(path string) (*Config, error) {
v := viper.New()
if path != "" {
v.SetConfigFile(path)
} else {
v.AddConfigPath(".")
v.SetConfigName("telegram")
}
v.AutomaticEnv()
if err := v.ReadInConfig(); err != nil {
return nil, err
}
cfg := &Config{
Serial: SerialConfig{
Device: v.GetString("serial.device"),
Baudrate: v.GetInt("serial.baudrate"),
LogRaw: v.GetBool("serial.lograw"),
},
Telegram: TelegramConfig{
TCP: v.GetBool("telegram.tcp"),
Pulsar: v.GetBool("telegram.pulsar"),
},
SQLite: SQLiteConfig{
File: v.GetString("sqlite.file"),
Init: v.GetBool("sqlite.init"),
},
Socket: SocketConfig{
Address: v.GetString("socket.address"),
},
Pulsar: PulsarConfig{
URL: v.GetString("pulsar.url"),
Topic: v.GetString("pulsar.topic"),
Name: v.GetString("pulsar.name"),
},
Log: LogConfig{
Dir: "./logs",
MaxAge: 60,
RotateHour: 1,
},
}
return cfg, nil
}