payload.go RawHistoryFolder
1package common
2
3import (
4 "encoding/json"
5 "fmt"
6 "io"
7)
8
9// RequestPayload represents the structure of incoming requests
10type RequestPayload struct {
11 Type string `json:"type"`
12 Payload Payload `json:"payload"`
13}
14
15// Payload is an interface for different types of payloads
16type Payload interface {
17 GetType() string
18}
19
20// DeployResponse represents the response structure for deployment requests
21type DeployResponse struct {
22 Success bool `json:"success"`
23 Message string `json:"message"`
24 Domain string `json:"domain,omitempty"`
25 ContainerID string `json:"container_id,omitempty"`
26}
27
28// PushResponse represents the response structure for push requests
29type PushResponse struct {
30 Success bool `json:"success"`
31 Message string `json:"message"`
32 CreateContainerURL string `json:"create_container_url,omitempty"`
33}
34
35// DeploymentError represents an error that occurred during deployment
36type DeploymentError struct {
37 StatusCode int `json:"status"`
38 Message string `json:"message"`
39 RawResponse string `json:"raw_response"`
40}
41
42// Error implements the error interface for DeploymentError
43func (e *DeploymentError) Error() string {
44 return fmt.Sprintf("Deployment failed (status %d): %s", e.StatusCode, e.Message)
45}
46
47// DeployPayload represents the payload for deployment requests
48type DeployPayload struct {
49 Port string `json:"port"`
50 TargetDomain string `json:"targetdomain"`
51 ImageName string `json:"imagename"`
52 ImageID string `json:"imageid"`
53 Data io.ReadCloser
54}
55
56// PushPayload represents the payload for push requests
57type PushPayload struct {
58 ImageName string `json:"imagename"`
59 ImageID string `json:"imageid"`
60 Data io.ReadCloser
61}
62
63// UnmarshalJSON custom unmarshaler for RequestPayload
64func (p *RequestPayload) UnmarshalJSON(data []byte) error {
65 var raw map[string]json.RawMessage
66 err := json.Unmarshal(data, &raw)
67 if err != nil {
68 return err
69 }
70
71 if err := json.Unmarshal(raw["type"], &p.Type); err != nil {
72 return err
73 }
74
75 // Unmarshal the payload based on the type
76 switch p.Type {
77 case "ping":
78 var payload PingPayload
79 if err := json.Unmarshal(raw["payload"], &payload); err != nil {
80 return err
81 }
82 p.Payload = payload
83 case "deploy":
84 var payload DeployPayload
85 if err := json.Unmarshal(raw["payload"], &payload); err != nil {
86 return err
87 }
88 p.Payload = payload
89 case "push":
90 var payload PushPayload
91 if err := json.Unmarshal(raw["payload"], &payload); err != nil {
92 return err
93 }
94 p.Payload = payload
95 case "stop":
96 var payload StopPayload
97 if err := json.Unmarshal(raw["payload"], &payload); err != nil {
98 return err
99 }
100 p.Payload = payload
101 case "start":
102 var payload StartPayload
103 if err := json.Unmarshal(raw["payload"], &payload); err != nil {
104 return err
105 }
106 p.Payload = payload
107 case "remove":
108 var payload RemovePayload
109 if err := json.Unmarshal(raw["payload"], &payload); err != nil {
110 return err
111 }
112 p.Payload = payload
113 default:
114 return fmt.Errorf("invalid type: %s", p.Type)
115 }
116
117 return nil
118}
119
120// PingPayload represents the payload for ping requests
121type PingPayload struct {
122 Message string `json:"message"`
123}
124
125// GetType returns the type of the PingPayload
126func (p PingPayload) GetType() string {
127 return "ping"
128}
129
130// GetType returns the type of the DeployPayload
131func (p DeployPayload) GetType() string {
132 return "deploy"
133}
134
135// GetType returns the type of the PushPayload
136func (p PushPayload) GetType() string {
137 return "push"
138}
139
140// NewPingPayload creates a new PingPayload from a map of data
141func NewPingPayload(data map[string]interface{}) (PingPayload, error) {
142 message, ok := data["message"].(string)
143 if !ok {
144 return PingPayload{}, fmt.Errorf("invalid data for message")
145 }
146 return PingPayload{Message: message}, nil
147}
148
149// StopPayload represents the payload for stop requests
150type StopPayload struct {
151 ContainerID string `json:"container_id"`
152}
153
154// GetType returns the type of the StopPayload
155func (p StopPayload) GetType() string {
156 return "stop"
157}
158
159// StartPayload represents the payload for start requests
160type StartPayload struct {
161 ContainerID string `json:"container_id"`
162}
163
164// GetType returns the type of the StartPayload
165func (p StartPayload) GetType() string {
166 return "start"
167}
168
169// RemovePayload represents the payload for remove requests
170type RemovePayload struct {
171 ContainerID string `json:"container_id"`
172}
173
174// GetType returns the type of the RemovePayload
175func (p RemovePayload) GetType() string {
176 return "remove"
177}