Skip to content

Commit

Permalink
[Sidcar Proxy] Enrich sidecar host and populate request headers (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerShor authored Mar 10, 2024
1 parent d55a74d commit 6a985b8
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions proxy-to-sidecar/proxy-to-sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http/httputil"
"net/url"
"os"
"strings"

nuclio "github.com/nuclio/nuclio-sdk-go"
)
Expand All @@ -21,6 +22,12 @@ func Handler(context *nuclio.Context, event nuclio.Event) (interface{}, error) {
if err != nil {
return nil, err
}

// set headers
for k, v := range event.GetHeaders() {
httpRequest.Header[k] = []string{v.(string)}
}

recorder := httptest.NewRecorder()
reverseProxy.ServeHTTP(recorder, httpRequest)

Expand All @@ -32,6 +39,10 @@ func Handler(context *nuclio.Context, event nuclio.Event) (interface{}, error) {
for key, value := range response.Header {
headers[key] = value[0]
}

// let the processor calculate the content length
delete(headers, "Content-Length")

return nuclio.Response{
StatusCode: response.StatusCode,
Body: recorder.Body.Bytes(),
Expand All @@ -42,6 +53,12 @@ func Handler(context *nuclio.Context, event nuclio.Event) (interface{}, error) {
func InitContext(context *nuclio.Context) error {
sidecarHost := os.Getenv("SIDECAR_HOST")
sidecarPort := os.Getenv("SIDECAR_PORT")

// enrich sidecar host
if !strings.Contains(sidecarHost, "://") {
sidecarHost = fmt.Sprintf("http://%s", sidecarHost)
}

// url for request forwarding
sidecarUrl := fmt.Sprintf("%s:%s", sidecarHost, sidecarPort)
parsedURL, err := url.Parse(sidecarUrl)
Expand Down

0 comments on commit 6a985b8

Please sign in to comment.