mirror of
https://github.com/openfaas/faas.git
synced 2025-06-19 12:36:40 +00:00
Refactor out handler file from server.go
Signed-off-by: Alex Ellis <alexellis2@gmail.com>
This commit is contained in:
49
gateway/handlers/forwarding_proxy.go
Normal file
49
gateway/handlers/forwarding_proxy.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httputil"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/alexellis/faas/gateway/metrics"
|
||||||
|
"github.com/alexellis/faas/gateway/types"
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MakeForwardingProxyHandler create a handler which forwards HTTP requests
|
||||||
|
func MakeForwardingProxyHandler(proxy *httputil.ReverseProxy, metrics *metrics.MetricOptions) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
uri := r.URL.String()
|
||||||
|
|
||||||
|
log.Printf("> Forwarding [%s] to %s", r.Method, r.URL.String())
|
||||||
|
start := time.Now()
|
||||||
|
|
||||||
|
writeAdapter := types.NewWriteAdapter(w)
|
||||||
|
proxy.ServeHTTP(writeAdapter, r)
|
||||||
|
|
||||||
|
seconds := time.Since(start).Seconds()
|
||||||
|
log.Printf("< [%s] - %d took %f seconds\n", r.URL.String(), writeAdapter.GetHeaderCode(), seconds)
|
||||||
|
|
||||||
|
forward := "/function/"
|
||||||
|
if startsWith(uri, forward) {
|
||||||
|
log.Printf("function=%s", uri[len(forward):])
|
||||||
|
|
||||||
|
service := uri[len(forward):]
|
||||||
|
|
||||||
|
metrics.GatewayFunctionsHistogram.
|
||||||
|
WithLabelValues(service).
|
||||||
|
Observe(seconds)
|
||||||
|
|
||||||
|
code := strconv.Itoa(writeAdapter.GetHeaderCode())
|
||||||
|
|
||||||
|
metrics.GatewayFunctionInvocation.With(prometheus.Labels{"function_name": service, "code": code}).Inc()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func startsWith(value, token string) bool {
|
||||||
|
return len(value) > len(token) && strings.Index(value, token) == 0
|
||||||
|
}
|
@ -8,8 +8,6 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -21,7 +19,6 @@ import (
|
|||||||
"github.com/alexellis/faas/gateway/plugin"
|
"github.com/alexellis/faas/gateway/plugin"
|
||||||
"github.com/alexellis/faas/gateway/types"
|
"github.com/alexellis/faas/gateway/types"
|
||||||
"github.com/docker/docker/client"
|
"github.com/docker/docker/client"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
@ -78,14 +75,13 @@ func main() {
|
|||||||
|
|
||||||
reverseProxy := httputil.NewSingleHostReverseProxy(config.FunctionsProviderURL)
|
reverseProxy := httputil.NewSingleHostReverseProxy(config.FunctionsProviderURL)
|
||||||
|
|
||||||
faasHandlers.Proxy = makeHandler(reverseProxy, &metricsOptions)
|
faasHandlers.Proxy = internalHandlers.MakeForwardingProxyHandler(reverseProxy, &metricsOptions)
|
||||||
faasHandlers.RoutelessProxy = makeHandler(reverseProxy, &metricsOptions)
|
faasHandlers.RoutelessProxy = internalHandlers.MakeForwardingProxyHandler(reverseProxy, &metricsOptions)
|
||||||
|
faasHandlers.ListFunctions = internalHandlers.MakeForwardingProxyHandler(reverseProxy, &metricsOptions)
|
||||||
faasHandlers.Alert = internalHandlers.MakeAlertHandler(plugin.NewExternalServiceQuery(*config.FunctionsProviderURL))
|
faasHandlers.DeployFunction = internalHandlers.MakeForwardingProxyHandler(reverseProxy, &metricsOptions)
|
||||||
|
faasHandlers.DeleteFunction = internalHandlers.MakeForwardingProxyHandler(reverseProxy, &metricsOptions)
|
||||||
faasHandlers.ListFunctions = makeHandler(reverseProxy, &metricsOptions)
|
alertHandler := plugin.NewExternalServiceQuery(*config.FunctionsProviderURL)
|
||||||
faasHandlers.DeployFunction = makeHandler(reverseProxy, &metricsOptions)
|
faasHandlers.Alert = internalHandlers.MakeAlertHandler(alertHandler)
|
||||||
faasHandlers.DeleteFunction = makeHandler(reverseProxy, &metricsOptions)
|
|
||||||
|
|
||||||
metrics.AttachExternalWatcher(*config.FunctionsProviderURL, metricsOptions, "func", time.Second*5)
|
metrics.AttachExternalWatcher(*config.FunctionsProviderURL, metricsOptions, "func", time.Second*5)
|
||||||
|
|
||||||
@ -94,10 +90,12 @@ func main() {
|
|||||||
|
|
||||||
faasHandlers.Proxy = internalHandlers.MakeProxy(metricsOptions, true, dockerClient, &logger)
|
faasHandlers.Proxy = internalHandlers.MakeProxy(metricsOptions, true, dockerClient, &logger)
|
||||||
faasHandlers.RoutelessProxy = internalHandlers.MakeProxy(metricsOptions, true, dockerClient, &logger)
|
faasHandlers.RoutelessProxy = internalHandlers.MakeProxy(metricsOptions, true, dockerClient, &logger)
|
||||||
faasHandlers.Alert = internalHandlers.MakeAlertHandler(internalHandlers.NewSwarmServiceQuery(dockerClient))
|
|
||||||
faasHandlers.ListFunctions = internalHandlers.MakeFunctionReader(metricsOptions, dockerClient)
|
faasHandlers.ListFunctions = internalHandlers.MakeFunctionReader(metricsOptions, dockerClient)
|
||||||
faasHandlers.DeployFunction = internalHandlers.MakeNewFunctionHandler(metricsOptions, dockerClient, maxRestarts)
|
faasHandlers.DeployFunction = internalHandlers.MakeNewFunctionHandler(metricsOptions, dockerClient, maxRestarts)
|
||||||
faasHandlers.DeleteFunction = internalHandlers.MakeDeleteFunctionHandler(metricsOptions, dockerClient)
|
faasHandlers.DeleteFunction = internalHandlers.MakeDeleteFunctionHandler(metricsOptions, dockerClient)
|
||||||
|
|
||||||
|
faasHandlers.Alert = internalHandlers.MakeAlertHandler(internalHandlers.NewSwarmServiceQuery(dockerClient))
|
||||||
|
|
||||||
// This could exist in a separate process - records the replicas of each swarm service.
|
// This could exist in a separate process - records the replicas of each swarm service.
|
||||||
functionLabel := "function"
|
functionLabel := "function"
|
||||||
metrics.AttachSwarmWatcher(dockerClient, metricsOptions, functionLabel)
|
metrics.AttachSwarmWatcher(dockerClient, metricsOptions, functionLabel)
|
||||||
@ -155,27 +153,3 @@ func main() {
|
|||||||
|
|
||||||
log.Fatal(s.ListenAndServe())
|
log.Fatal(s.ListenAndServe())
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeHandler(proxy *httputil.ReverseProxy, metrics *metrics.MetricOptions) http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
uri := r.URL.String()
|
|
||||||
|
|
||||||
log.Printf("Forwarding [%s] to %s", r.Method, r.URL.String())
|
|
||||||
start := time.Now()
|
|
||||||
|
|
||||||
writeAdapter := types.NewWriteAdapter(w)
|
|
||||||
proxy.ServeHTTP(writeAdapter, r)
|
|
||||||
|
|
||||||
seconds := time.Since(start).Seconds()
|
|
||||||
fmt.Printf("[%d] took %f seconds\n", writeAdapter.GetHeaderCode(), seconds)
|
|
||||||
|
|
||||||
forward := "/function/"
|
|
||||||
if len(uri) > len(forward) && strings.Index(uri, forward) == 0 {
|
|
||||||
fmt.Println("function=", uri[len(forward):])
|
|
||||||
service := uri[len(forward):]
|
|
||||||
metrics.GatewayFunctionsHistogram.WithLabelValues(service).Observe(seconds)
|
|
||||||
code := writeAdapter.GetHeaderCode()
|
|
||||||
metrics.GatewayFunctionInvocation.With(prometheus.Labels{"function_name": service, "code": strconv.Itoa(code)}).Inc()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user