Files
bj_power/bj_power_wms/internal/svc/service_context.go
T
SunYF f65a52d9e1 feat: 多系统新增扫码中间件与 AGV 对接,优化拧紧与退库流程
新增扫码中间件支持网络型扫码枪键盘注入,AGV 对接从 WMS 迁至 MES 统一管理;新增拧紧不合格人工处置与 PLC 对账机制,优化退库单关联与工位终端功能,同时清理旧 AGV 任务表,完善数据库与配置项。
2026-10-14 17:24:59 +08:00

77 lines
2.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package svc
import (
"strings"
"bj_power_wms/ent"
"bj_power_wms/internal/config"
"bj_power_wms/internal/db"
"bj_power_wms/internal/eventlog"
"bj_power_wms/internal/mesclient"
)
type ServiceContext struct {
Config config.Config
EntClient *ent.Client
EventLog *eventlog.Writer
Mes *mesclient.Client
}
func NewServiceContext(c config.Config) *ServiceContext {
// 启动期密钥校验:编译期默认密钥已移除,yaml 缺失/为空则直接 panic,
// 杜绝「空 token 绕过内部接口鉴权」的攻击面(安全审计 #6/#13)。
validateWmsSecrets(c)
conf := db.DatabaseConf{
Host: c.Postgres.Host,
Port: c.Postgres.Port,
User: c.Postgres.User,
Password: c.Postgres.Password,
Dbname: c.Postgres.DBName,
}
// 目标数据库不存在则自动创建
if err := db.EnsureDB(conf); err != nil {
panic(err)
}
entClient := db.MustNewDB(conf)
// 启动时自动建表
if err := db.AutoMigrate(entClient); err != nil {
panic(err)
}
// 首次启动预置基础数据
if err := db.SeedIfEmpty(entClient); err != nil {
panic(err)
}
return &ServiceContext{
Config: c,
EntClient: entClient,
EventLog: eventlog.NewWriter(entClient),
Mes: mesclient.New(c.Mes.BaseURL, c.Mes.Token),
}
}
// validateWmsSecrets 启动期校验必要密钥非空(编译期默认密钥已移除)。
func validateWmsSecrets(c config.Config) {
missing := make([]string, 0, 4)
if strings.TrimSpace(c.Auth.AccessSecret) == "" {
missing = append(missing, "Auth.AccessSecret")
}
if strings.TrimSpace(c.Internal.Token) == "" {
missing = append(missing, "Internal.Token")
}
if strings.TrimSpace(c.Mes.Token) == "" {
missing = append(missing, "Mes.Token")
}
if strings.TrimSpace(c.Cli.AdminPassword) == "" {
missing = append(missing, "Cli.AdminPassword")
}
if len(missing) > 0 {
panic("配置缺失必要密钥(已禁止编译期默认密钥,必须在 etc/*.yaml 中显式配置):" + strings.Join(missing, ", "))
}
}