系统列表
Distributed Systems in Go:生产级分布式平台 SDK 与微服务实战
专栏《分布式 Go 系统工程》配套完整代码实战工程,涵盖并发模型、消息队列适配、Saga 编排、弹性容错与可观测性等生产级模式。
README.mdSYSTEM.mdcmd/auth/main.gocmd/gateway/main.gocmd/orders/main.goconfig/grafana/datasources/datasources.yamlconfig/prometheus.yamlconfig/tempo.yamldeploy/kube/orderflow-infra.yamldocker-compose.ymlgo.modgo.suminternal/auth/handler.gointernal/auth/service.gointernal/domain/events.gointernal/inventory/handler.gointernal/inventory/service.gointernal/notifications/service.gointernal/orders/handler.gointernal/orders/service.gointernal/payment/service.gomigrations/000001_init_schema.up.sqlpkg/platform/auth/auth_test.gopkg/platform/auth/claims.gopkg/platform/auth/jwks.gopkg/platform/auth/middleware.gopkg/platform/auth/password.gopkg/platform/auth/token.gopkg/platform/cli/cli_test.gopkg/platform/cli/health.gopkg/platform/concurrency/context.gopkg/platform/concurrency/shutdown.gopkg/platform/concurrency/shutdown_test.gopkg/platform/concurrency/workerpool.gopkg/platform/concurrency/workerpool_test.gopkg/platform/config/config_test.gopkg/platform/config/loader.gopkg/platform/consistency/consistency_test.gopkg/platform/consistency/idempotency.gopkg/platform/consistency/optimistic.gopkg/platform/consistency/saga.gopkg/platform/ctxutil/context.gopkg/platform/ctxutil/context_test.gopkg/platform/ctxutil/keys.gopkg/platform/database/database_test.gopkg/platform/database/errors.gopkg/platform/database/migrate.gopkg/platform/database/pool.gopkg/platform/database/retry.gopkg/platform/database/tx.gopkg/platform/messaging/fake.gopkg/platform/messaging/fake_test.gopkg/platform/messaging/interfaces.gopkg/platform/messaging/kafka/consumer.gopkg/platform/messaging/kafka/producer.gopkg/platform/messaging/nats/publisher.gopkg/platform/messaging/nats/subscriber.gopkg/platform/messaging/schema.gopkg/platform/middleware/bodyclose.gopkg/platform/middleware/chain.gopkg/platform/middleware/cors.gopkg/platform/middleware/errors.gopkg/platform/middleware/health.gopkg/platform/middleware/logging.gopkg/platform/middleware/middleware_test.gopkg/platform/middleware/recover.gopkg/platform/middleware/requestid.gopkg/platform/middleware/timeout.gopkg/platform/observability/context.gopkg/platform/observability/logger.gopkg/platform/observability/metrics.gopkg/platform/observability/observability_test.gopkg/platform/observability/tracing.gopkg/platform/orchestration/budget.gopkg/platform/orchestration/degrade.gopkg/platform/orchestration/fanout.gopkg/platform/orchestration/orchestration_test.gopkg/platform/resilience/circuit.gopkg/platform/resilience/resilience_test.gopkg/platform/resilience/retry.gotests/live_infra_test.gotests/orderflow_integration_test.go
pkg/platform/config/loader.go
| package config |
| import ( |
| "bufio" |
| "fmt" |
| "os" |
| "reflect" |
| "strconv" |
| "strings" |
| "time" |
| ) |
| // Validator is an optional interface that config structs can implement for domain-specific checks. |
| type Validator interface { |
| Validate() error |
| } |
| // Load populates a config struct T from environment variables based on struct tags. |
| // Supports `env:"VAR_NAME"`, `envDefault:"val"`, and `required:"true"`. |
| func Load[T any]() (T, error) { |
| var cfg T |
| val := reflect.ValueOf(&cfg).Elem() |
| if val.Kind() != reflect.Struct { |
| return cfg, fmt.Errorf("config target must be a struct") |
| } |
| if err := populateStruct(val); err != nil { |
| return cfg, fmt.Errorf("load config: %w", err) |
| } |
| // Run custom validation if implemented |
| if v, ok := any(cfg).(Validator); ok { |
| if err := v.Validate(); err != nil { |
| return cfg, fmt.Errorf("validate config: %w", err) |
| } |
| } else if v, ok := any(&cfg).(Validator); ok { |
| if err := v.Validate(); err != nil { |
| return cfg, fmt.Errorf("validate config: %w", err) |
| } |
| } |
| return cfg, nil |
| } |
| func populateStruct(val reflect.Value) error { |
| t := val.Type() |
| for i := 0; i < t.NumField(); i++ { |
| field := t.Field(i) |
| fieldVal := val.Field(i) |
| if !fieldVal.CanSet() { |
| continue |
| } |
| if fieldVal.Kind() == reflect.Struct && field.Type != reflect.TypeOf(time.Time{}) { |
| if err := populateStruct(fieldVal); err != nil { |
| return err |
| } |
| continue |
| } |
| envKey := field.Tag.Get("env") |
| if envKey == "" { |
| continue |
| } |
| envVal, found := os.LookupEnv(envKey) |
| if !found || envVal == "" { |
| envVal = field.Tag.Get("envDefault") |
| } |
| if envVal == "" { |
| if field.Tag.Get("required") == "true" { |
| return fmt.Errorf("missing required environment variable: %s", envKey) |
| } |
| continue |
| } |
| if err := setFieldValue(fieldVal, envVal); err != nil { |
| return fmt.Errorf("field %s (%s): %w", field.Name, envKey, err) |
| } |
| } |
| return nil |
| } |
| func setFieldValue(f reflect.Value, raw string) error { |
| if f.Type() == reflect.TypeOf(time.Duration(0)) { |
| d, err := time.ParseDuration(raw) |
| if err != nil { |
| return err |
| } |
| f.Set(reflect.ValueOf(d)) |
| return nil |
| } |
| switch f.Kind() { |
| case reflect.String: |
| f.SetString(raw) |
| case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| n, err := strconv.ParseInt(raw, 10, 64) |
| if err != nil { |
| return err |
| } |
| f.SetInt(n) |
| case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
| n, err := strconv.ParseUint(raw, 10, 64) |
| if err != nil { |
| return err |
| } |
| f.SetUint(n) |
| case reflect.Bool: |
| b, err := strconv.ParseBool(raw) |
| if err != nil { |
| return err |
| } |
| f.SetBool(b) |
| case reflect.Float32, reflect.Float64: |
| fl, err := strconv.ParseFloat(raw, 64) |
| if err != nil { |
| return err |
| } |
| f.SetFloat(fl) |
| default: |
| return fmt.Errorf("unsupported type %s", f.Type()) |
| } |
| return nil |
| } |
| // LoadDotEnv parses and sets environment variables from a .env file. |
| func LoadDotEnv(filePath string) error { |
| file, err := os.Open(filePath) |
| if err != nil { |
| if os.IsNotExist(err) { |
| return nil |
| } |
| return err |
| } |
| defer file.Close() |
| scanner := bufio.NewScanner(file) |
| for scanner.Scan() { |
| line := strings.TrimSpace(scanner.Text()) |
| if line == "" || strings.HasPrefix(line, "#") { |
| continue |
| } |
| parts := strings.SplitN(line, "=", 2) |
| if len(parts) == 2 { |
| key := strings.TrimSpace(parts[0]) |
| val := strings.Trim(strings.TrimSpace(parts[1]), `"'`) |
| if _, exists := os.LookupEnv(key); !exists { |
| _ = os.Setenv(key, val) |
| } |
| } |
| } |
| return scanner.Err() |
| } |