Files
goauto/server/config/extend.go
T

123 lines
3.7 KiB
Go

package config
import (
"os"
"strconv"
"strings"
sdkconfig "github.com/go-admin-team/go-admin-core/sdk/config"
)
var ExtConfig Extend
// Extend 扩展配置
//
// extend:
// demo:
// name: demo-name
//
// 使用方法: config.ExtConfig......即可!!
type Extend struct {
AMap AMap // 这里配置对应配置文件的结构即可
SYB SYB
}
type AMap struct {
Key string
}
// SYB holds the 顺云宝 ERP connection settings (#48).
//
// `[必须]` Username and Password are NOT read from settings.yml — they come
// from GOAUTO_SYB_USERNAME / GOAUTO_SYB_PASSWORD via ApplyEnvironment, so no
// credential ever lands in a tracked file. Everything else here is non-secret
// and belongs in settings.yml.
//
// See docs/12-syb-erp-interface.md §8.
type SYB struct {
BaseURL string
Username string
Password string
PageSize int
// MaxMatches caps how many shipment orders one sync may touch. Zero means
// the caller's own default applies.
MaxMatches int
// OcrURL is the captcha recognition service. Empty disables OCR and falls
// back to the manual-entry path — that is a supported configuration, not an
// error (§8.1).
//
// `[必须]` Captcha images are sent to this host. Re-evaluate before pointing
// it at a service operated by somebody else.
OcrURL string
OcrMaxAttempts int
}
// SYBDefaults are the values used when settings.yml leaves a field blank.
const (
DefaultSYBBaseURL = "https://www.shunyunbaoerp.com"
DefaultSYBPageSize = 50
DefaultSYBMaxMatches = 10000
DefaultSYBOcrMaxAttempts = 5
)
// Resolved returns the SYB settings with blanks replaced by defaults. It never
// defaults Username or Password: missing credentials must surface as an error
// at the call site, not as an attempt to log in as nobody.
func (s SYB) Resolved() SYB {
if strings.TrimSpace(s.BaseURL) == "" {
s.BaseURL = DefaultSYBBaseURL
}
if s.PageSize <= 0 {
s.PageSize = DefaultSYBPageSize
}
if s.MaxMatches <= 0 {
s.MaxMatches = DefaultSYBMaxMatches
}
if s.OcrMaxAttempts <= 0 {
s.OcrMaxAttempts = DefaultSYBOcrMaxAttempts
}
s.Username = strings.TrimSpace(s.Username)
s.BaseURL = strings.TrimRight(strings.TrimSpace(s.BaseURL), "/")
s.OcrURL = strings.TrimSpace(s.OcrURL)
return s
}
// HasCredentials reports whether both account fields were supplied. Callers
// check this before starting a sync so a missing environment variable fails
// with a clear message instead of a 未登录 error from SYB.
func (s SYB) HasCredentials() bool {
return strings.TrimSpace(s.Username) != "" && s.Password != ""
}
// ApplyEnvironment replaces tracked defaults with process-local runtime values.
// Credentials stay outside tracked configuration files. GOAUTO_DB_DRIVER
// defaults to mysql when GOAUTO_DB_DSN is present.
func ApplyEnvironment() {
if port, err := strconv.Atoi(strings.TrimSpace(os.Getenv("GOAUTO_SERVER_PORT"))); err == nil && port >= 1 && port <= 65535 {
sdkconfig.ApplicationConfig.Port = int64(port)
}
if username := strings.TrimSpace(os.Getenv("GOAUTO_SYB_USERNAME")); username != "" {
ExtConfig.SYB.Username = username
}
// `[必须]` The password is taken verbatim: trimming it would silently change
// a credential whose leading or trailing space is intentional.
if password := os.Getenv("GOAUTO_SYB_PASSWORD"); password != "" {
ExtConfig.SYB.Password = password
}
dsn := strings.TrimSpace(os.Getenv("GOAUTO_DB_DSN"))
if dsn == "" {
return
}
driver := strings.TrimSpace(os.Getenv("GOAUTO_DB_DRIVER"))
if driver == "" {
driver = "mysql"
}
sdkconfig.DatabaseConfig.Driver = driver
sdkconfig.DatabaseConfig.Source = dsn
for _, database := range sdkconfig.DatabasesConfig {
database.Driver = driver
database.Source = dsn
}
}