faas-provider的Serve将路由与处理请求的Handlers绑定。faas-provider中规定好了这些FaaSHandlers,这些Handlers在OpenFaaS API Gateway路由处也有设置,由网关转发给具体的Provider处理。
// FaaSHandlers provide handlers for OpenFaaS
type FaaSHandlers struct {
FunctionReader http.HandlerFunc
DeployHandler http.HandlerFunc
// FunctionProxy provides the function invocation proxy logic. Use proxy.NewHandlerFunc to
// use the standard OpenFaaS proxy implementation or provide completely custom proxy logic.
FunctionProxy http.HandlerFunc
DeleteHandler http.HandlerFunc
ReplicaReader http.HandlerFunc
ReplicaUpdater http.HandlerFunc
SecretHandler http.HandlerFunc
// LogHandler provides streaming json logs of functions
LogHandler http.HandlerFunc
// Optional: Update an existing function
UpdateHandler http.HandlerFunc
HealthHandler http.HandlerFunc
InfoHandler http.HandlerFunc
}
// MakeExtractFunctionMiddleWare returns a middleware handler which validates
// the presence of the function name in the URI query.
func MakeExtractFunctionMiddleWare(
getVars func(*http.Request) map[string]string,
next http.HandlerFunc) http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) {
vars := getVars(r)
functionName := vars["name"]
if functionName == "" {
rw.WriteHeader(http.StatusBadRequest)
fmt.Fprint(rw, fmt.Errorf("No function name"))
return
}
r = r.WithContext(context.WithValue(r.Context(), FunctionNameCTXKey, functionName))
next(rw, r)
}
}
nomad.Job 中 Job接口定义的方法与完全相同,只是抽出来faas-nomad中需要的接口,屏蔽了真实的nomad jobs API的细节。这是Go语法的特点,刚看到的时候有点奇怪(我没有完整地系统学GO语法),要是Java的话得声明相同的接口吧,其中不用的方法,还占用内存;这种赋值,也是Go语言中“实现接口的结构体中所有方法,即是接口定义的类型”的运用吧。