-
Notifications
You must be signed in to change notification settings - Fork 43
/
serve.go
143 lines (112 loc) · 5.08 KB
/
serve.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (c) Alex Ellis 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package bootstrap
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/openfaas/faas-provider/auth"
"github.com/openfaas/faas-provider/types"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// NameExpression for a function / service
const NameExpression = "-a-zA-Z_0-9."
var r *mux.Router
// Mark this as a Golang "package"
func init() {
r = mux.NewRouter()
}
// Router gives access to the underlying router for when new routes need to be added.
func Router() *mux.Router {
return r
}
// Serve load your handlers into the correct OpenFaaS route spec. This function is blocking.
func Serve(ctx context.Context, handlers *types.FaaSHandlers, config *types.FaaSConfig) {
if config.EnableBasicAuth {
reader := auth.ReadBasicAuthFromDisk{
SecretMountPath: config.SecretMountPath,
}
credentials, err := reader.Read()
if err != nil {
log.Fatal(err)
}
handlers.FunctionLister = auth.DecorateWithBasicAuth(handlers.FunctionLister, credentials)
handlers.DeployFunction = auth.DecorateWithBasicAuth(handlers.DeployFunction, credentials)
handlers.DeleteFunction = auth.DecorateWithBasicAuth(handlers.DeleteFunction, credentials)
handlers.UpdateFunction = auth.DecorateWithBasicAuth(handlers.UpdateFunction, credentials)
handlers.FunctionStatus = auth.DecorateWithBasicAuth(handlers.FunctionStatus, credentials)
handlers.ScaleFunction = auth.DecorateWithBasicAuth(handlers.ScaleFunction, credentials)
handlers.Info = auth.DecorateWithBasicAuth(handlers.Info, credentials)
handlers.Secrets = auth.DecorateWithBasicAuth(handlers.Secrets, credentials)
handlers.Logs = auth.DecorateWithBasicAuth(handlers.Logs, credentials)
if handlers.Telemetry != nil {
handlers.Telemetry = auth.DecorateWithBasicAuth(handlers.Telemetry, credentials)
}
}
hm := newHttpMetrics()
// System (auth) endpoints
r.HandleFunc("/system/functions", hm.InstrumentHandler(handlers.FunctionLister, "")).Methods(http.MethodGet)
r.HandleFunc("/system/functions", hm.InstrumentHandler(handlers.DeployFunction, "")).Methods(http.MethodPost)
r.HandleFunc("/system/functions", hm.InstrumentHandler(handlers.DeleteFunction, "")).Methods(http.MethodDelete)
r.HandleFunc("/system/functions", hm.InstrumentHandler(handlers.UpdateFunction, "")).Methods(http.MethodPut)
r.HandleFunc("/system/function/{name:["+NameExpression+"]+}",
hm.InstrumentHandler(handlers.FunctionStatus, "/system/function")).Methods(http.MethodGet)
r.HandleFunc("/system/scale-function/{name:["+NameExpression+"]+}",
hm.InstrumentHandler(handlers.ScaleFunction, "/system/scale-function")).Methods(http.MethodPost)
r.HandleFunc("/system/info",
hm.InstrumentHandler(handlers.Info, "")).Methods(http.MethodGet)
r.HandleFunc("/system/secrets",
hm.InstrumentHandler(handlers.Secrets, "")).Methods(http.MethodGet, http.MethodPut, http.MethodPost, http.MethodDelete)
r.HandleFunc("/system/logs",
hm.InstrumentHandler(handlers.Logs, "")).Methods(http.MethodGet)
r.HandleFunc("/system/namespaces", hm.InstrumentHandler(handlers.ListNamespaces, "")).Methods(http.MethodGet)
// Only register the mutate namespace handler if it is defined
if handlers.MutateNamespace != nil {
r.HandleFunc("/system/namespace/{name:["+NameExpression+"]*}",
hm.InstrumentHandler(handlers.MutateNamespace, "")).Methods(http.MethodPost, http.MethodDelete, http.MethodPut, http.MethodGet)
} else {
r.HandleFunc("/system/namespace/{name:["+NameExpression+"]*}",
hm.InstrumentHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Feature not implemented in this version of OpenFaaS", http.StatusNotImplemented)
}), "")).Methods(http.MethodGet)
}
proxyHandler := handlers.FunctionProxy
// Open endpoints
r.HandleFunc("/function/{name:["+NameExpression+"]+}", proxyHandler)
r.HandleFunc("/function/{name:["+NameExpression+"]+}/", proxyHandler)
r.HandleFunc("/function/{name:["+NameExpression+"]+}/{params:.*}", proxyHandler)
if handlers.Health != nil {
r.HandleFunc("/healthz", handlers.Health).Methods(http.MethodGet)
}
if handlers.Telemetry != nil {
r.HandleFunc("/system/telemetry", hm.InstrumentHandler(handlers.Telemetry, "")).Methods(http.MethodGet)
}
r.HandleFunc("/metrics", promhttp.Handler().ServeHTTP)
readTimeout := config.ReadTimeout
writeTimeout := config.WriteTimeout
port := 8080
if config.TCPPort != nil {
port = *config.TCPPort
}
s := &http.Server{
Addr: fmt.Sprintf(":%d", port),
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
MaxHeaderBytes: http.DefaultMaxHeaderBytes, // 1MB - can be overridden by setting Server.MaxHeaderBytes.
Handler: r,
}
// Start server in a goroutine
go func() {
if err := s.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}
}()
// Shutdown server when context is done.
<-ctx.Done()
if err := s.Shutdown(context.Background()); err != nil {
log.Printf("Failed to shut down provider gracefully: %s", err)
}
}