-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
107 lines (89 loc) · 2.66 KB
/
main.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
//go:generate wit-bindgen-wrpc go -w server --out-dir bindings --package github.com/wasmCloud/provider-sdk-go/examples/keyvalue-inmemory/bindings wit
//go:generate wit-bindgen-wrpc go -w testing --out-dir bindings/testing --package github.com/wasmCloud/provider-sdk-go/examples/keyvalue-inmemory/bindings/testing wit
package main
import (
"io"
"log"
"os"
"os/signal"
"syscall"
server "github.com/wasmCloud/provider-sdk-go/examples/keyvalue-inmemory/bindings"
"go.opentelemetry.io/otel"
"go.wasmcloud.dev/provider"
)
func main() {
if err := run(os.Stdin); err != nil {
log.Fatal(err)
}
}
func run(source io.Reader) error {
p := &Provider{
sourceLinks: make(map[string]provider.InterfaceLinkDefinition),
targetLinks: make(map[string]provider.InterfaceLinkDefinition),
tracer: otel.Tracer("keyvalue-inmemory"),
}
wasmcloudprovider, err := provider.NewWithHostDataSource(
source,
provider.SourceLinkPut(p.handleNewSourceLink),
provider.TargetLinkPut(p.handleNewTargetLink),
provider.SourceLinkDel(p.handleDelSourceLink),
provider.TargetLinkDel(p.handleDelTargetLink),
provider.HealthCheck(p.handleHealthCheck),
provider.Shutdown(p.handleShutdown),
)
if err != nil {
return err
}
providerCh := make(chan error, 1)
signalCh := make(chan os.Signal, 1)
// Handle RPC operations
stopFunc, err := server.Serve(wasmcloudprovider.RPCClient, p)
if err != nil {
wasmcloudprovider.Shutdown()
return err
}
// Handle control interface operations
go func() {
err := wasmcloudprovider.Start()
providerCh <- err
}()
// Shutdown on SIGINT
signal.Notify(signalCh, syscall.SIGINT)
select {
case err = <-providerCh:
stopFunc()
return err
case <-signalCh:
wasmcloudprovider.Shutdown()
stopFunc()
}
return nil
}
func (p *Provider) handleNewSourceLink(link provider.InterfaceLinkDefinition) error {
log.Println("Handling new source link", link)
p.sourceLinks[link.Target] = link
return nil
}
func (p *Provider) handleNewTargetLink(link provider.InterfaceLinkDefinition) error {
log.Println("Handling new target link", link)
p.targetLinks[link.SourceID] = link
return nil
}
func (p *Provider) handleDelSourceLink(link provider.InterfaceLinkDefinition) error {
log.Println("Handling del source link", link)
delete(p.sourceLinks, link.Target)
return nil
}
func (p *Provider) handleDelTargetLink(link provider.InterfaceLinkDefinition) error {
log.Println("Handling del target link", link)
delete(p.targetLinks, link.SourceID)
return nil
}
func (p *Provider) handleHealthCheck() string {
log.Println("Handling health check")
return "provider healthy"
}
func (p *Provider) handleShutdown() error {
log.Println("Handling shutdown")
return nil
}