-
Notifications
You must be signed in to change notification settings - Fork 63
/
registry.go
52 lines (45 loc) · 1.42 KB
/
registry.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
package registry
import (
"context"
"fmt"
"net/http"
cloudevents "github.com/cloudevents/sdk-go/v2"
)
// A declaratively registered function
type RegisteredFunction struct {
Name string // The name of the function
CloudEventFn func(context.Context, cloudevents.Event) error // Optional: The user's CloudEvent function
HTTPFn func(http.ResponseWriter, *http.Request) // Optional: The user's HTTP function
}
var (
function_registry = map[string]RegisteredFunction{}
)
// Registers a HTTP function with a given name
func RegisterHTTP(name string, fn func(http.ResponseWriter, *http.Request)) error {
if _, ok := function_registry[name]; ok {
return fmt.Errorf("function name already registered: %s", name)
}
function_registry[name] = RegisteredFunction{
Name: name,
CloudEventFn: nil,
HTTPFn: fn,
}
return nil
}
// Registers a CloudEvent function with a given name
func RegisterCloudEvent(name string, fn func(context.Context, cloudevents.Event) error) error {
if _, ok := function_registry[name]; ok {
return fmt.Errorf("function name already registered: %s", name)
}
function_registry[name] = RegisteredFunction{
Name: name,
CloudEventFn: fn,
HTTPFn: nil,
}
return nil
}
// Gets a registered function by name
func GetRegisteredFunction(name string) (RegisteredFunction, bool) {
fn, ok := function_registry[name]
return fn, ok
}