1package handler
2
3import (
4 "bytes"
5 "encoding/json"
6 "fmt"
7 "io"
8 "net/http"
9
10 "github.com/bnema/gordon/internal/cli"
11 "github.com/bnema/gordon/internal/common"
12)
13
14// PrepareJSONPayload marshals the payload into JSON
15func PrepareJSONPayload(payload common.Payload) ([]byte, error) {
16 return json.Marshal(payload)
17}
18
19// CreateNewRequest creates a new HTTP request
20func CreateNewRequest(method, url string, body []byte) (*http.Request, error) {
21
22 return http.NewRequest(method, url, bytes.NewBuffer(body))
23}
24
25// SetRequestHeaders sets the headers for the request
26func setAuthRequestHeaders(req *http.Request, token string) {
27 req.Header.Set("Authorization", "Bearer "+token)
28}
29
30// SendRequest sends the HTTP request and returns the response
31func SendRequest(req *http.Request) (*http.Response, error) {
32 client := &http.Client{}
33 return client.Do(req)
34}
35
36type Response struct {
37 Http *http.Response
38 Header http.Header
39 Body []byte
40 StatusCode int
41}
42
43// SendHTTPRequest sends the HTTP request
44func SendHTTPRequest(a *cli.App, rp *common.RequestPayload, method string, endpoint string) (*Response, error) {
45 apiUrl := a.Config.Http.BackendURL + "/api"
46 token := a.Config.General.Token
47
48 var req *http.Request
49 var err error
50
51 // Handle deploy type payload
52 if rp.Type == "deploy" {
53 deployPayload, ok := rp.Payload.(common.DeployPayload)
54 if !ok {
55 return nil, fmt.Errorf("invalid payload type for deploy")
56 }
57
58 req, err = http.NewRequest(method, apiUrl+endpoint, deployPayload.Data)
59 if err != nil {
60 return nil, fmt.Errorf("failed to create new streaming request: %w", err)
61 }
62
63 setAuthRequestHeaders(req, token)
64 req.Header.Set("Content-Type", "application/octet-stream")
65 req.Header.Set("X-Ports", deployPayload.Ports)
66 req.Header.Set("X-Target-Domain", deployPayload.TargetDomain)
67 req.Header.Set("X-Image-Name", deployPayload.ImageName)
68
69 client := &http.Client{}
70 resp, err := client.Do(req)
71 if err != nil {
72 return nil, fmt.Errorf("failed to send request: %w", err)
73 }
74 defer resp.Body.Close()
75
76 body, err := io.ReadAll(resp.Body)
77 if err != nil {
78 return nil, fmt.Errorf("failed to read response body: %w", err)
79 }
80 return &Response{Body: body}, nil
81 }
82
83 // Handle other types of payloads
84 jsonPayload, err := json.Marshal(rp)
85 if err != nil {
86 return nil, fmt.Errorf("failed to marshal payload: %w", err)
87 }
88
89 req, err = http.NewRequest(method, apiUrl+endpoint, bytes.NewBuffer(jsonPayload))
90 if err != nil {
91 return nil, fmt.Errorf("failed to create new JSON request: %w", err)
92 }
93
94 setAuthRequestHeaders(req, token)
95 req.Header.Set("Content-Type", "application/json")
96
97 client := &http.Client{}
98 resp, err := client.Do(req)
99 if err != nil {
100 return nil, fmt.Errorf("failed to send request: %w", err)
101 }
102 defer resp.Body.Close()
103
104 body, err := io.ReadAll(resp.Body)
105 if err != nil {
106 return nil, fmt.Errorf("failed to read response body: %w", err)
107 }
108 return &Response{Http: resp, Body: body, StatusCode: resp.StatusCode}, nil
109}