Skip to content

Commit

Permalink
Create a const for X-OpenFaaS-Internal header key
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
  • Loading branch information
alexellis committed Nov 2, 2023
1 parent 8d5a98b commit 88d664d
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ import (
)

const (
watchdogPort = "8080"
defaultContentType = "text/plain"
watchdogPort = "8080"
defaultContentType = "text/plain"
openFaaSInternalHeader = "X-OpenFaaS-Internal"
)

// BaseURLResolver URL resolver for proxy requests
Expand Down Expand Up @@ -139,15 +140,15 @@ func proxyRequest(w http.ResponseWriter, originalReq *http.Request, proxyClient
pathVars := mux.Vars(originalReq)
functionName := pathVars["name"]
if functionName == "" {
w.Header().Add("X-OpenFaaS-Internal", "proxy")
w.Header().Add(openFaaSInternalHeader, "proxy")

httputil.Errorf(w, http.StatusBadRequest, "Provide function name in the request path")
return
}

functionAddr, err := resolver.Resolve(functionName)
if err != nil {
w.Header().Add("X-OpenFaaS-Internal", "proxy")
w.Header().Add(openFaaSInternalHeader, "proxy")

// TODO: Should record the 404/not found error in Prometheus.
log.Printf("resolver error: no endpoints for %s: %s\n", functionName, err.Error())
Expand All @@ -158,7 +159,7 @@ func proxyRequest(w http.ResponseWriter, originalReq *http.Request, proxyClient
proxyReq, err := buildProxyRequest(originalReq, functionAddr, pathVars["params"])
if err != nil {

w.Header().Add("X-OpenFaaS-Internal", "proxy")
w.Header().Add(openFaaSInternalHeader, "proxy")

httputil.Errorf(w, http.StatusInternalServerError, "Failed to resolve service: %s.", functionName)
return
Expand All @@ -175,7 +176,7 @@ func proxyRequest(w http.ResponseWriter, originalReq *http.Request, proxyClient
if err != nil {
log.Printf("error with proxy request to: %s, %s\n", proxyReq.URL.String(), err.Error())

w.Header().Add("X-OpenFaaS-Internal", "proxy")
w.Header().Add(openFaaSInternalHeader, "proxy")

httputil.Errorf(w, http.StatusInternalServerError, "Can't reach service for: %s.", functionName)
return
Expand Down

0 comments on commit 88d664d

Please sign in to comment.