系统列表

Distributed Systems in Go:生产级分布式平台 SDK 与微服务实战

distributed-systems-in-goGo架构: Platform SDK / Microservices Monorepo·

专栏《分布式 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
internal/auth/service.go
internal/auth/service.go
package auth
import (
"context"
"crypto/ecdsa"
"errors"
"fmt"
"sync"
"time"
"distributed-systems-in-go/pkg/platform/auth"
"github.com/google/uuid"
)
var (
ErrUserExists = errors.New("user with this email already exists")
ErrInvalidCreds = errors.New("invalid email or password")
ErrUserNotFound = errors.New("user not found")
)
type User struct {
ID string
Email string
PasswordHash string
Roles []string
Scopes []string
CreatedAt time.Time
}
type UserStore interface {
Create(ctx context.Context, u *User) error
GetByEmail(ctx context.Context, email string) (*User, error)
GetByID(ctx context.Context, id string) (*User, error)
}
type MemoryUserStore struct {
mu sync.RWMutex
users map[string]*User
}
func NewMemoryUserStore() *MemoryUserStore {
return &MemoryUserStore{users: make(map[string]*User)}
}
func (s *MemoryUserStore) Create(ctx context.Context, u *User) error {
s.mu.Lock()
defer s.mu.Unlock()
for _, existing := range s.users {
if existing.Email == u.Email {
return ErrUserExists
}
}
s.users[u.ID] = u
return nil
}
func (s *MemoryUserStore) GetByEmail(ctx context.Context, email string) (*User, error) {
s.mu.RLock()
defer s.mu.RUnlock()
for _, u := range s.users {
if u.Email == email {
return u, nil
}
}
return nil, ErrUserNotFound
}
func (s *MemoryUserStore) GetByID(ctx context.Context, id string) (*User, error) {
s.mu.RLock()
defer s.mu.RUnlock()
u, ok := s.users[id]
if !ok {
return nil, ErrUserNotFound
}
return u, nil
}
type Service struct {
store UserStore
privateKey *ecdsa.PrivateKey
keyID string
}
func NewService(store UserStore, privKey *ecdsa.PrivateKey, keyID string) *Service {
return &Service{
store: store,
privateKey: privKey,
keyID: keyID,
}
}
func (s *Service) Register(ctx context.Context, email, password string) (*User, error) {
hash, err := auth.HashPassword(password)
if err != nil {
return nil, fmt.Errorf("hash password: %w", err)
}
user := &User{
ID: uuid.NewString(),
Email: email,
PasswordHash: hash,
Roles: []string{"customer"},
Scopes: []string{"orders:create", "orders:read"},
CreatedAt: time.Now().UTC(),
}
if err := s.store.Create(ctx, user); err != nil {
return nil, err
}
return user, nil
}
func (s *Service) Login(ctx context.Context, email, password string) (string, *User, error) {
user, err := s.store.GetByEmail(ctx, email)
if err != nil {
return "", nil, ErrInvalidCreds
}
if !auth.VerifyPassword(user.PasswordHash, password) {
return "", nil, ErrInvalidCreds
}
claims := &auth.Claims{
Subject: user.ID,
Issuer: "orderflow-auth",
Roles: user.Roles,
Scopes: user.Scopes,
IssuedAt: time.Now().UTC(),
ExpiresAt: time.Now().UTC().Add(time.Hour),
}
token, err := auth.GenerateToken(claims, s.privateKey, s.keyID)
if err != nil {
return "", nil, fmt.Errorf("generate token: %w", err)
}
return token, user, nil
}
func (s *Service) PublicKey() *ecdsa.PublicKey {
return &s.privateKey.PublicKey
}