重构:拆出 internal/config 与 internal/store 子包
把工程根目录中和"配置加载"、"数据存储"两个领域相关的全部 Go 文件迁到 internal/ 下的子包,按功能分组的第一阶段成果。 internal/config/ - 从 config.go 抽出 Config / MQTTConfig / WebConfig / DatabaseConfig 等 类型并大写导出,函数改名 Default/Load/Write/Validate/BuildTLS 等。 - 测试随被测代码迁移成 package config 的内部测试。 - 根目录留 config.go 作桥接:用 type alias 让旧的小写名(config / mqttConfig / databaseConfig 等)继续可用,避免修改 30+ 处调用方。 internal/store/ - 把 db.go、store_query.go、db_write_queue.go 与 13 个 *_store.go 一并 迁入;26 个 *Record 类型与 store 同包以避免循环依赖。 - store -> Store;50+ 标识符从小写未导出改为大写导出(包括 record、 ListOptions、错误变量、bot/llm/runtime 常量、helpers 等)。 - 新增 DB() / Driver() 访问器供 ai 子系统使用,避免直接访问私有字段。 - bot_pki_store.go 独立出来,把 PKI 解密所需的 store 方法集中归类。 - helpers.go 提供 hashPassword / uint32FromRecord / printJSON 等以前在 其他根目录文件中的辅助;test_helpers_test.go 提供 verifyPassword 与 publicMapTileSourceDTO 让测试可以本地运行而不依赖 main 包。 根目录新增: - store_bridge.go:完整 type-alias / 函数包装层,把 internal/store 的 导出名映射回旧的小写名,让 admin_*_routes.go、web.go、bot_service.go 等仍未迁出的文件继续编译。后续步骤把它们迁到各自领域包后可逐步删除。 - test_helpers_test.go:根目录测试沿用 openTestStore 的入口。 go build ./... 与 go test ./... 全部通过;测试数量与重构前一致。 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
+145
@@ -0,0 +1,145 @@
|
||||
package main
|
||||
|
||||
// 桥接到 internal/store —— 让根目录其余文件无须修改即可继续使用旧的小写类型/函数名。
|
||||
// 当各领域包逐步迁出根目录后,可以删除这些别名。
|
||||
|
||||
import (
|
||||
storepkg "meshtastic_mqtt_server/internal/store"
|
||||
)
|
||||
|
||||
// ---- 类型别名 ----
|
||||
|
||||
type (
|
||||
store = storepkg.Store
|
||||
mqttClientInfo = storepkg.MQTTClientInfo
|
||||
dbWriteQueue = storepkg.WriteQueue
|
||||
listOptions = storepkg.ListOptions
|
||||
mapReportViewportOptions = storepkg.MapReportViewportOptions
|
||||
mapReportViewportResult = storepkg.MapReportViewportResult
|
||||
mapReportClusterRecord = storepkg.MapReportClusterRecord
|
||||
userRecord = storepkg.UserRecord
|
||||
loginLogRecord = storepkg.LoginLogRecord
|
||||
helpContentRecord = storepkg.HelpContentRecord
|
||||
runtimeSettingRecord = storepkg.RuntimeSettingRecord
|
||||
mapTileSourceRecord = storepkg.MapTileSourceRecord
|
||||
mapTileSourceInput = storepkg.MapTileSourceInput
|
||||
discardDetailsRecord = storepkg.DiscardDetailsRecord
|
||||
signRecord = storepkg.SignRecord
|
||||
nodeBlockingRecord = storepkg.NodeBlockingRecord
|
||||
ipBlockingRecord = storepkg.IPBlockingRecord
|
||||
forbiddenWordBlockingRecord = storepkg.ForbiddenWordBlockingRecord
|
||||
mqttForwarderRecord = storepkg.MQTTForwarderRecord
|
||||
mqttForwarderInput = storepkg.MQTTForwarderInput
|
||||
mqttForwardTopicRecord = storepkg.MQTTForwardTopicRecord
|
||||
mqttForwardTopicInput = storepkg.MQTTForwardTopicInput
|
||||
botNodeRecord = storepkg.BotNodeRecord
|
||||
botNodeInput = storepkg.BotNodeInput
|
||||
botMessageRecord = storepkg.BotMessageRecord
|
||||
botDirectMessageRecord = storepkg.BotDirectMessageRecord
|
||||
llmMessageQueueRecord = storepkg.LLMMessageQueueRecord
|
||||
nodeInfoRecord = storepkg.NodeInfoRecord
|
||||
mapReportRecord = storepkg.MapReportRecord
|
||||
textMessageRecord = storepkg.TextMessageRecord
|
||||
llmProviderRecord = storepkg.LLMProviderRecord
|
||||
llmToolRouterRecord = storepkg.LLMToolRouterRecord
|
||||
llmPrimaryConfigRecord = storepkg.LLMPrimaryConfigRecord
|
||||
positionRecord = storepkg.PositionRecord
|
||||
telemetryRecord = storepkg.TelemetryRecord
|
||||
routingRecord = storepkg.RoutingRecord
|
||||
tracerouteRecord = storepkg.TracerouteRecord
|
||||
)
|
||||
|
||||
// AppendPacketFields / MQTTClientRecordFields 已经是导出名,不需要别名。
|
||||
// 直接用包限定名供 root 文件调用:
|
||||
type (
|
||||
AppendPacketFields = storepkg.AppendPacketFields
|
||||
MQTTClientRecordFields = storepkg.MQTTClientRecordFields
|
||||
)
|
||||
|
||||
// 其它额外导出类型的别名(旧的小写形式仍被根目录文件直接使用)。
|
||||
type (
|
||||
runtimeSettingsSnapshot = storepkg.RuntimeSettingsSnapshot
|
||||
mqttForwarderConfig = storepkg.MQTTForwarderConfig
|
||||
botDirectMessageListOptions = storepkg.BotDirectMessageListOptions
|
||||
botMessageListOptions = storepkg.BotMessageListOptions
|
||||
botDirectConversation = storepkg.BotDirectConversation
|
||||
signDayCount = storepkg.SignDayCount
|
||||
)
|
||||
|
||||
var errBlockingAlreadyExists = storepkg.ErrBlockingAlreadyExists
|
||||
var errBotNodeAlreadyExists = storepkg.ErrBotNodeAlreadyExists
|
||||
var (
|
||||
errMapTileSourceAlreadyExists = storepkg.ErrMapTileSourceAlreadyExists
|
||||
errMapTileSourceCannotDeleteDefault = storepkg.ErrMapTileSourceCannotDeleteDefault
|
||||
errMapTileSourceCannotDisableDefault = storepkg.ErrMapTileSourceCannotDisableDefault
|
||||
errMapTileSourceDefaultMustBeEnabled = storepkg.ErrMapTileSourceDefaultMustBeEnabled
|
||||
)
|
||||
|
||||
func mapTileSourceHash(urlTemplate string) string {
|
||||
return storepkg.MapTileSourceHash(urlTemplate)
|
||||
}
|
||||
|
||||
const (
|
||||
forbiddenWordMatchContains = storepkg.ForbiddenWordMatchContains
|
||||
runtimeSettingAllowEncryptedForwarding = storepkg.RuntimeSettingAllowEncryptedForwarding
|
||||
runtimeSettingLLMQueueEnabled = storepkg.RuntimeSettingLLMQueueEnabled
|
||||
runtimeSettingLLMQueueIncludeChannel = storepkg.RuntimeSettingLLMQueueIncludeChannel
|
||||
|
||||
botDefaultPSK = storepkg.BotDefaultPSK
|
||||
botMessageTypeChannel = storepkg.BotMessageTypeChannel
|
||||
botMessageTypeDirect = storepkg.BotMessageTypeDirect
|
||||
botMessageStatusPending = storepkg.BotMessageStatusPending
|
||||
botMessageStatusPublished = storepkg.BotMessageStatusPublished
|
||||
botMessageStatusFailed = storepkg.BotMessageStatusFailed
|
||||
botDirectMessageDirectionInbound = storepkg.BotDirectMessageDirectionInbound
|
||||
botDirectMessageDirectionOutbound = storepkg.BotDirectMessageDirectionOutbound
|
||||
botDefaultTopicPrefix = storepkg.BotDefaultTopicPrefix
|
||||
|
||||
mqttForwardDirectionSourceToTarget = storepkg.MQTTForwardDirectionSourceToTarget
|
||||
mqttForwardDirectionBidirectional = storepkg.MQTTForwardDirectionBidirectional
|
||||
)
|
||||
|
||||
const botDefaultNodeInfoBroadcastSeconds = storepkg.BotDefaultNodeInfoBroadcastSeconds
|
||||
|
||||
func validateBotNodeNum(n int64) error { return storepkg.ValidateBotNodeNum(n) }
|
||||
|
||||
var errUserAlreadyExists = storepkg.ErrUserAlreadyExists
|
||||
|
||||
var (
|
||||
errMQTTForwarderAlreadyExists = storepkg.ErrMQTTForwarderAlreadyExists
|
||||
errMQTTForwardTopicAlreadyExists = storepkg.ErrMQTTForwardTopicAlreadyExists
|
||||
)
|
||||
|
||||
func nullableString(v any) *string { return storepkg.NullableString(v) }
|
||||
func nullableStringValue(v any) *string { return storepkg.NullableStringValue(v) }
|
||||
func decodeBotPublicKey(row botNodeRecord) ([]byte, error) {
|
||||
return storepkg.DecodeBotPublicKey(row)
|
||||
}
|
||||
|
||||
// LLM 消息队列状态字符串常量。
|
||||
const (
|
||||
llmMessageStatusPending = storepkg.LLMMessageStatusPending
|
||||
llmMessageStatusProcessing = storepkg.LLMMessageStatusProcessing
|
||||
llmMessageStatusProcessed = storepkg.LLMMessageStatusProcessed
|
||||
llmMessageStatusError = storepkg.LLMMessageStatusError
|
||||
)
|
||||
|
||||
const defaultHelpMarkdown = storepkg.DefaultHelpMarkdown
|
||||
|
||||
// 旧名 llmMessageDTO 现在通过 store 提供。
|
||||
func llmMessageDTO(row llmMessageQueueRecord) map[string]any {
|
||||
return storepkg.LLMMessageDTO(row)
|
||||
}
|
||||
|
||||
// ---- 工厂函数包装 ----
|
||||
|
||||
func openStore(cfg databaseConfig) (*store, error) { return storepkg.OpenStore(cfg) }
|
||||
func normalizeListOptions(o listOptions) listOptions {
|
||||
return storepkg.NormalizeListOptions(o)
|
||||
}
|
||||
func normalizeMapReportViewportOptions(o mapReportViewportOptions) mapReportViewportOptions {
|
||||
return storepkg.NormalizeMapReportViewportOptions(o)
|
||||
}
|
||||
|
||||
// newDBWriteQueue 在新包里更名,提供旧名字避免改 main.go。
|
||||
func newDBWriteQueue(s *store) *dbWriteQueue { return storepkg.NewWriteQueue(s) }
|
||||
Reference in New Issue
Block a user