0.1 5d00907

http.go RawHistoryFolder

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}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close