forked from wasmCloud/wasmCloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider.go
77 lines (67 loc) · 2.28 KB
/
provider.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
package main
import (
"context"
"errors"
"runtime"
// Go provider SDK
sdk "go.wasmcloud.dev/provider"
wrpcnats "wrpc.io/go/nats"
// Generated bindings from the wit world
system_info "github.com/wasmCloud/wasmCloud/examples/go/providers/custom-template/bindings/exports/wasmcloud/example/system_info"
"github.com/wasmCloud/wasmCloud/examples/go/providers/custom-template/bindings/wasmcloud/example/process_data"
)
// / Your Handler struct is where you can store any state or configuration that your provider needs to keep track of.
type Handler struct {
// The provider instance
provider *sdk.WasmcloudProvider
// All components linked to this provider and their config.
linkedFrom map[string]map[string]string
// All components this provider is linked to and their config
linkedTo map[string]map[string]string
}
// Request information about the system the provider is running on
func (h *Handler) RequestInfo(ctx context.Context, kind system_info.Kind) (string, error) {
// Only allow requests from a lattice source
header, ok := wrpcnats.HeaderFromContext(ctx)
if !ok {
h.provider.Logger.Warn("Received request from unknown origin")
return "", nil
}
// Only allow requests from a linked component
sourceId := header.Get("source-id")
if h.linkedFrom[sourceId] == nil {
h.provider.Logger.Warn("Received request from unlinked source", "sourceId", sourceId)
return "", nil
}
h.provider.Logger.Debug("Received request for system information", "sourceId", sourceId)
switch kind {
case system_info.Kind_Os:
return runtime.GOOS, nil
case system_info.Kind_Arch:
return runtime.GOARCH, nil
default:
return "", errors.New("invalid system info request")
}
}
// Example export to call from the provider for testing
func (h *Handler) Call(ctx context.Context) (string, error) {
var lastResponse string
for target := range h.linkedTo {
data := process_data.Data{
Count: 3,
Name: "sup",
}
// Get the outgoing RPC client for the target
client := h.provider.OutgoingRpcClient(target)
// Send the data to the target for processing
res, err := process_data.Process(ctx, client, &data)
if err != nil {
return "", err
}
lastResponse = res
}
if lastResponse == "" {
lastResponse = "Provider received call but was not linked to any components"
}
return lastResponse, nil
}