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