From bbba3141274413b1c81216f2aa45d9cec3dfe9be Mon Sep 17 00:00:00 2001 From: NightStar Date: Tue, 28 Jul 2026 19:31:22 +0800 Subject: [PATCH] refactor: extract domain models from provider into internal/model - Move Server/Domain/DNSRecord/Bucket/StorageInstance/CDNInstance/Order/Error structs to internal/model/ - Provider interfaces now reference model.* types - Rainyun provider implementations import model package for data types - CLI commands updated to use model.DNSRecord directly Separates pure data structures from service contract interfaces, following hexagonal architecture: models (entities) live independently from ports (provider interfaces) and adapters (rainyun/* implementations). --- cmd/cli/commands/domain/domain.go | 3 +- internal/model/billing.go | 12 ++++ internal/model/cdn.go | 8 +++ internal/model/domain.go | 19 ++++++ internal/model/errors.go | 18 ++++++ internal/model/server.go | 17 +++++ internal/model/storage.go | 14 ++++ internal/provider/provider.go | 96 ++++------------------------ internal/provider/rainyun/billing.go | 8 +-- internal/provider/rainyun/domain.go | 16 ++--- internal/provider/rainyun/server.go | 14 ++-- internal/provider/rainyun/storage.go | 14 ++-- 12 files changed, 127 insertions(+), 112 deletions(-) create mode 100644 internal/model/billing.go create mode 100644 internal/model/cdn.go create mode 100644 internal/model/domain.go create mode 100644 internal/model/errors.go create mode 100644 internal/model/server.go create mode 100644 internal/model/storage.go diff --git a/cmd/cli/commands/domain/domain.go b/cmd/cli/commands/domain/domain.go index 719075c..6385049 100644 --- a/cmd/cli/commands/domain/domain.go +++ b/cmd/cli/commands/domain/domain.go @@ -3,6 +3,7 @@ package domain import ( "fmt" + "git.yoresee.cc/nightstar/ry/internal/model" "git.yoresee.cc/nightstar/ry/internal/output" "git.yoresee.cc/nightstar/ry/internal/provider" @@ -66,7 +67,7 @@ func Cmd(prov *provider.Provider, out **output.Printer) *cobra.Command { value, _ := cmd.Flags().GetString("value") ttl, _ := cmd.Flags().GetInt("ttl") return svc.DNSAdd(cmd.Context(), args[0], - provider.DNSRecord{Type: recType, Name: name, Value: value, TTL: ttl}) + model.DNSRecord{Type: recType, Name: name, Value: value, TTL: ttl}) }, } diff --git a/internal/model/billing.go b/internal/model/billing.go new file mode 100644 index 0000000..94e4d8d --- /dev/null +++ b/internal/model/billing.go @@ -0,0 +1,12 @@ +package model + +import "time" + +type Order struct { + ID string `json:"id" table:"ID"` + Product string `json:"product" table:"PRODUCT"` + Amount float64 `json:"amount" table:"AMOUNT"` + Status string `json:"status" table:"STATUS"` + Created time.Time `json:"created" table:"CREATED"` + Raw any `json:"-"` +} diff --git a/internal/model/cdn.go b/internal/model/cdn.go new file mode 100644 index 0000000..80f3366 --- /dev/null +++ b/internal/model/cdn.go @@ -0,0 +1,8 @@ +package model + +type CDNInstance struct { + ID string `json:"id" table:"ID"` + Domain string `json:"domain" table:"DOMAIN"` + Status string `json:"status" table:"STATUS"` + Raw any `json:"-"` +} diff --git a/internal/model/domain.go b/internal/model/domain.go new file mode 100644 index 0000000..aa31ec5 --- /dev/null +++ b/internal/model/domain.go @@ -0,0 +1,19 @@ +package model + +import "time" + +type Domain struct { + ID string `json:"id" table:"ID"` + Name string `json:"name" table:"DOMAIN"` + Status string `json:"status" table:"STATUS"` + ExpireAt time.Time `json:"expires" table:"EXPIRES"` + Raw any `json:"-"` +} + +type DNSRecord struct { + ID string `json:"id" table:"ID"` + Type string `json:"type" table:"TYPE"` + Name string `json:"name" table:"NAME"` + Value string `json:"value" table:"VALUE"` + TTL int `json:"ttl" table:"TTL"` +} diff --git a/internal/model/errors.go b/internal/model/errors.go new file mode 100644 index 0000000..480ed56 --- /dev/null +++ b/internal/model/errors.go @@ -0,0 +1,18 @@ +package model + +import "fmt" + +type Error struct { + Code int + Message string + Detail string + Raw []byte +} + +func (e *Error) Error() string { + return e.Message +} + +func (e *Error) Debug() string { + return fmt.Sprintf("%s\n%s", e.Detail, string(e.Raw)) +} diff --git a/internal/model/server.go b/internal/model/server.go new file mode 100644 index 0000000..581c90e --- /dev/null +++ b/internal/model/server.go @@ -0,0 +1,17 @@ +package model + +import "time" + +type Server struct { + ID string `json:"id" table:"ID"` + Name string `json:"name" table:"NAME"` + Status string `json:"status" table:"STATUS"` + IP string `json:"ip" table:"IP"` + CPU int `json:"cpu" table:"CPU"` + Memory int `json:"memory" table:"MEM"` + Disk int `json:"disk" table:"DISK"` + OS string `json:"os" table:"OS"` + Region string `json:"region" table:"REGION"` + ExpireAt time.Time `json:"expires" table:"EXPIRES"` + Raw any `json:"-"` +} diff --git a/internal/model/storage.go b/internal/model/storage.go new file mode 100644 index 0000000..3b4480a --- /dev/null +++ b/internal/model/storage.go @@ -0,0 +1,14 @@ +package model + +type StorageInstance struct { + ID string `json:"id" table:"ID"` + Name string `json:"name" table:"NAME"` + Status string `json:"status" table:"STATUS"` + Raw any `json:"-"` +} + +type Bucket struct { + ID string `json:"id" table:"ID"` + Name string `json:"name" table:"NAME"` + Region string `json:"region" table:"REGION"` +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 8b623ea..cd66f94 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -2,8 +2,8 @@ package provider import ( "context" - "fmt" - "time" + + "git.yoresee.cc/nightstar/ry/internal/model" ) type ProviderConfig struct { @@ -25,8 +25,8 @@ type Provider interface { } type ServerService interface { - List(ctx context.Context) ([]Server, error) - Get(ctx context.Context, id string) (*Server, error) + List(ctx context.Context) ([]model.Server, error) + Get(ctx context.Context, id string) (*model.Server, error) Start(ctx context.Context, id string) error Stop(ctx context.Context, id string) error Reboot(ctx context.Context, id string) error @@ -37,96 +37,22 @@ type ServerService interface { } type DomainService interface { - List(ctx context.Context) ([]Domain, error) - DNSList(ctx context.Context, domain string) ([]DNSRecord, error) - DNSAdd(ctx context.Context, domain string, record DNSRecord) error + List(ctx context.Context) ([]model.Domain, error) + DNSList(ctx context.Context, domain string) ([]model.DNSRecord, error) + DNSAdd(ctx context.Context, domain string, record model.DNSRecord) error DNSDelete(ctx context.Context, domain string, recordID string) error } type StorageService interface { - List(ctx context.Context) ([]StorageInstance, error) - BucketList(ctx context.Context, instanceID string) ([]Bucket, error) + List(ctx context.Context) ([]model.StorageInstance, error) + BucketList(ctx context.Context, instanceID string) ([]model.Bucket, error) BucketCreate(ctx context.Context, instanceID, name string) error } type CDNService interface { - List(ctx context.Context) ([]CDNInstance, error) + List(ctx context.Context) ([]model.CDNInstance, error) } type BillingService interface { - Orders(ctx context.Context) ([]Order, error) -} - -type Server struct { - ID string `json:"id" table:"ID"` - Name string `json:"name" table:"NAME"` - Status string `json:"status" table:"STATUS"` - IP string `json:"ip" table:"IP"` - CPU int `json:"cpu" table:"CPU"` - Memory int `json:"memory" table:"MEM"` - Disk int `json:"disk" table:"DISK"` - OS string `json:"os" table:"OS"` - Region string `json:"region" table:"REGION"` - ExpireAt time.Time `json:"expires" table:"EXPIRES"` - Raw any `json:"-"` -} - -type Domain struct { - ID string `json:"id" table:"ID"` - Name string `json:"name" table:"DOMAIN"` - Status string `json:"status" table:"STATUS"` - ExpireAt time.Time `json:"expires" table:"EXPIRES"` - Raw any `json:"-"` -} - -type DNSRecord struct { - ID string `json:"id" table:"ID"` - Type string `json:"type" table:"TYPE"` - Name string `json:"name" table:"NAME"` - Value string `json:"value" table:"VALUE"` - TTL int `json:"ttl" table:"TTL"` -} - -type StorageInstance struct { - ID string `json:"id" table:"ID"` - Name string `json:"name" table:"NAME"` - Status string `json:"status" table:"STATUS"` - Raw any `json:"-"` -} - -type Bucket struct { - ID string `json:"id" table:"ID"` - Name string `json:"name" table:"NAME"` - Region string `json:"region" table:"REGION"` -} - -type CDNInstance struct { - ID string `json:"id" table:"ID"` - Domain string `json:"domain" table:"DOMAIN"` - Status string `json:"status" table:"STATUS"` - Raw any `json:"-"` -} - -type Order struct { - ID string `json:"id" table:"ID"` - Product string `json:"product" table:"PRODUCT"` - Amount float64 `json:"amount" table:"AMOUNT"` - Status string `json:"status" table:"STATUS"` - Created time.Time `json:"created" table:"CREATED"` - Raw any `json:"-"` -} - -type Error struct { - Code int - Message string - Detail string - Raw []byte -} - -func (e *Error) Error() string { - return e.Message -} - -func (e *Error) Debug() string { - return fmt.Sprintf("%s\n%s", e.Detail, string(e.Raw)) + Orders(ctx context.Context) ([]model.Order, error) } diff --git a/internal/provider/rainyun/billing.go b/internal/provider/rainyun/billing.go index 6a48487..9c9e193 100644 --- a/internal/provider/rainyun/billing.go +++ b/internal/provider/rainyun/billing.go @@ -5,14 +5,14 @@ import ( "fmt" "time" - "git.yoresee.cc/nightstar/ry/internal/provider" + "git.yoresee.cc/nightstar/ry/internal/model" ) type BillingService struct { client *client } -func (b *BillingService) Orders(ctx context.Context) ([]provider.Order, error) { +func (b *BillingService) Orders(ctx context.Context) ([]model.Order, error) { type rainyunOrder struct { ID int `json:"id"` ProductName string `json:"product_name"` @@ -24,10 +24,10 @@ func (b *BillingService) Orders(ctx context.Context) ([]provider.Order, error) { if err := b.client.getList(ctx, "/expense/orders/list", &raw); err != nil { return nil, err } - orders := make([]provider.Order, len(raw)) + orders := make([]model.Order, len(raw)) for i, r := range raw { t, _ := time.Parse("2006-01-02 15:04:05", r.CreateDate) - orders[i] = provider.Order{ + orders[i] = model.Order{ ID: fmt.Sprintf("%d", r.ID), Product: r.ProductName, Amount: r.Amount, diff --git a/internal/provider/rainyun/domain.go b/internal/provider/rainyun/domain.go index 603a7ae..acaedf9 100644 --- a/internal/provider/rainyun/domain.go +++ b/internal/provider/rainyun/domain.go @@ -4,7 +4,7 @@ import ( "context" "fmt" - "git.yoresee.cc/nightstar/ry/internal/provider" + "git.yoresee.cc/nightstar/ry/internal/model" ) type DomainService struct { @@ -18,14 +18,14 @@ type rainyunDomain struct { ExpireDate string `json:"expire_date"` } -func (d *DomainService) List(ctx context.Context) ([]provider.Domain, error) { +func (d *DomainService) List(ctx context.Context) ([]model.Domain, error) { var raw []rainyunDomain if err := d.client.getList(ctx, "/product/domain/", &raw); err != nil { return nil, err } - domains := make([]provider.Domain, len(raw)) + domains := make([]model.Domain, len(raw)) for i, r := range raw { - domains[i] = provider.Domain{ + domains[i] = model.Domain{ ID: fmt.Sprintf("%d", r.ID), Name: r.DomainName, } @@ -33,7 +33,7 @@ func (d *DomainService) List(ctx context.Context) ([]provider.Domain, error) { return domains, nil } -func (d *DomainService) DNSList(ctx context.Context, domain string) ([]provider.DNSRecord, error) { +func (d *DomainService) DNSList(ctx context.Context, domain string) ([]model.DNSRecord, error) { type rainyunDNSRecord struct { ID int `json:"id"` RecordType string `json:"record_type"` @@ -46,9 +46,9 @@ func (d *DomainService) DNSList(ctx context.Context, domain string) ([]provider. if err := d.client.get(ctx, path, &raw); err != nil { return nil, err } - records := make([]provider.DNSRecord, len(raw)) + records := make([]model.DNSRecord, len(raw)) for i, r := range raw { - records[i] = provider.DNSRecord{ + records[i] = model.DNSRecord{ ID: fmt.Sprintf("%d", r.ID), Type: r.RecordType, Name: r.Host, @@ -59,7 +59,7 @@ func (d *DomainService) DNSList(ctx context.Context, domain string) ([]provider. return records, nil } -func (d *DomainService) DNSAdd(ctx context.Context, domain string, record provider.DNSRecord) error { +func (d *DomainService) DNSAdd(ctx context.Context, domain string, record model.DNSRecord) error { body := map[string]any{ "record_type": record.Type, "host": record.Name, diff --git a/internal/provider/rainyun/server.go b/internal/provider/rainyun/server.go index 28b257c..c956c1b 100644 --- a/internal/provider/rainyun/server.go +++ b/internal/provider/rainyun/server.go @@ -5,7 +5,7 @@ import ( "fmt" "time" - "git.yoresee.cc/nightstar/ry/internal/provider" + "git.yoresee.cc/nightstar/ry/internal/model" ) // ServerService implements provider.ServerService for 雨云. @@ -27,19 +27,19 @@ type rainyunServer struct { ExpireDate int64 `json:"ExpDate"` // Unix timestamp } -func (s *ServerService) List(ctx context.Context) ([]provider.Server, error) { +func (s *ServerService) List(ctx context.Context) ([]model.Server, error) { var raw []rainyunServer if err := s.client.getList(ctx, "/product/rcs/", &raw); err != nil { return nil, err } - servers := make([]provider.Server, len(raw)) + servers := make([]model.Server, len(raw)) for i, r := range raw { servers[i] = toServer(r) } return servers, nil } -func (s *ServerService) Get(ctx context.Context, id string) (*provider.Server, error) { +func (s *ServerService) Get(ctx context.Context, id string) (*model.Server, error) { var raw rainyunServer path := fmt.Sprintf("/product/rcs/%s/", id) if err := s.client.get(ctx, path, &raw); err != nil { @@ -85,10 +85,10 @@ func (s *ServerService) Upgrade(ctx context.Context, id, plan string) error { return s.client.post(ctx, fmt.Sprintf("/product/rcs/%s/upgrade", id), body, nil) } -// toServer maps a rainyunServer to the provider.Server model. -func toServer(r rainyunServer) provider.Server { +// toServer maps a rainyunServer to the model.Server model. +func toServer(r rainyunServer) model.Server { expire := time.Unix(r.ExpireDate, 0) - return provider.Server{ + return model.Server{ ID: fmt.Sprintf("%d", r.ID), Name: r.Name, Status: r.Status, diff --git a/internal/provider/rainyun/storage.go b/internal/provider/rainyun/storage.go index f7ba289..df55486 100644 --- a/internal/provider/rainyun/storage.go +++ b/internal/provider/rainyun/storage.go @@ -4,14 +4,14 @@ import ( "context" "fmt" - "git.yoresee.cc/nightstar/ry/internal/provider" + "git.yoresee.cc/nightstar/ry/internal/model" ) type StorageService struct { client *client } -func (s *StorageService) List(ctx context.Context) ([]provider.StorageInstance, error) { +func (s *StorageService) List(ctx context.Context) ([]model.StorageInstance, error) { var raw []struct { ID int `json:"id"` Name string `json:"name"` @@ -20,9 +20,9 @@ func (s *StorageService) List(ctx context.Context) ([]provider.StorageInstance, if err := s.client.getList(ctx, "/product/ros/instance", &raw); err != nil { return nil, err } - instances := make([]provider.StorageInstance, len(raw)) + instances := make([]model.StorageInstance, len(raw)) for i, r := range raw { - instances[i] = provider.StorageInstance{ + instances[i] = model.StorageInstance{ ID: fmt.Sprintf("%d", r.ID), Name: r.Name, Raw: r, @@ -31,7 +31,7 @@ func (s *StorageService) List(ctx context.Context) ([]provider.StorageInstance, return instances, nil } -func (s *StorageService) BucketList(ctx context.Context, instanceID string) ([]provider.Bucket, error) { +func (s *StorageService) BucketList(ctx context.Context, instanceID string) ([]model.Bucket, error) { type rainyunBucket struct { ID int `json:"id"` Name string `json:"name"` @@ -42,9 +42,9 @@ func (s *StorageService) BucketList(ctx context.Context, instanceID string) ([]p if err := s.client.get(ctx, path, &raw); err != nil { return nil, err } - buckets := make([]provider.Bucket, len(raw)) + buckets := make([]model.Bucket, len(raw)) for i, r := range raw { - buckets[i] = provider.Bucket{ + buckets[i] = model.Bucket{ ID: fmt.Sprintf("%d", r.ID), Name: r.Name, }