-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
66 lines (62 loc) · 1.48 KB
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package srv
import (
"io"
"net/http"
"github.com/murtaza-u/fleet/internal/pb"
)
// runHTTP starts the HTTP server, blocking until an error occurs.
func (s Srv) runHTTP() error {
http.HandleFunc("/", s.httpHandler)
return http.ListenAndServe(s.httpPort, nil)
}
// httpHandler proxies incoming requests to the connected gRPC client.
func (s Srv) httpHandler(w http.ResponseWriter, r *http.Request) {
subdomain := subdomain(r.Host)
if subdomain == "" {
http.Error(w, "page not found", http.StatusNotFound)
return
}
v := s.store.Get(subdomain)
if v == nil {
http.Error(w, "page not found", http.StatusNotFound)
return
}
queue, ok := v.(chan request)
if !ok {
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
body, err := io.ReadAll(io.LimitReader(r.Body, s.maxRequestBodySize))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
r.Body.Close()
header := make(map[string]*pb.HeaderFields, len(r.Header))
for k, v := range r.Header {
header[k] = &pb.HeaderFields{Fields: v}
}
req := &pb.Request{
Id: newID(),
Method: r.Method,
Url: r.URL.String(),
Body: body,
Header: header,
}
reply := make(chan *pb.Response, 1)
queue <- request{
Request: req,
reply: reply,
}
res := <-reply
close(reply)
for k, values := range res.GetHeader() {
for _, v := range values.GetFields() {
w.Header().Add(k, v)
}
}
if res.GetStatus() != 0 {
w.WriteHeader(int(res.GetStatus()))
}
w.Write(res.GetData())
}