-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy_handler.go
More file actions
42 lines (34 loc) · 1.28 KB
/
proxy_handler.go
File metadata and controls
42 lines (34 loc) · 1.28 KB
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
package main
import (
"fmt"
"net/http"
)
type ProxyHandler struct {
serverScheme string
serverHost string
strategy ProxyStrategy
}
func (p *ProxyHandler) ServeHTTP(clientResponseWriter http.ResponseWriter, clientRequest *http.Request) {
serverResponse, serverResponseError := p.strategy.ForwardRequest(p.serverScheme, p.serverHost, clientRequest)
if nil != serverResponseError {
fmt.Printf("Error forwarding request: %s \n", serverResponseError.Error())
responseStatusCode := http.StatusServiceUnavailable
http.Error(clientResponseWriter, http.StatusText(responseStatusCode), responseStatusCode)
return
}
defer func() {
serverResponseBodyCloseError := serverResponse.Body.Close()
if nil != serverResponseBodyCloseError {
fmt.Printf("Error closing server response body: %s \n", serverResponseBodyCloseError.Error())
}
}()
serverResponseStatusCode, serverResponseHeader, serverResponseBodyReader := p.strategy.ProcessResponse(serverResponse)
p.strategy.ReturnResponse(clientResponseWriter, serverResponseStatusCode, serverResponseHeader, serverResponseBodyReader)
}
func NewProxyHandler(serverScheme, serverHost string, strategy ProxyStrategy) *ProxyHandler {
return &ProxyHandler{
serverScheme: serverScheme,
serverHost: serverHost,
strategy: strategy,
}
}