Skip to content

Commit

Permalink
Add a host-functions example
Browse files Browse the repository at this point in the history
  • Loading branch information
knqyf263 committed Aug 27, 2022
1 parent c014c65 commit 1feeaa1
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 99 deletions.
6 changes: 4 additions & 2 deletions examples/host-functions/greeting/greet.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ message GreetRequest {
}

// The response message containing the greetings
message GreetReply { string message = 1; }
message GreetReply {
string message = 1;
}

// The host functions embedded into the plugin
// go:plugin type=host
Expand All @@ -25,7 +27,7 @@ service HostFunctions {
rpc HttpGet(HttpGetRequest) returns (HttpGetResponse) {}
}

message HttpGetRequest{
message HttpGetRequest {
string url = 1;
}

Expand Down
109 changes: 43 additions & 66 deletions examples/host-functions/greeting/greet_host.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 5 additions & 21 deletions examples/host-functions/greeting/greet_plugin.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 8 additions & 10 deletions examples/host-functions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,21 @@ import (
"github.com/knqyf263/go-plugin/examples/host-functions/greeting"
)

func init() {
greeting.RegisterHostFunctions(hostFunctions{})
}

func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}

func run() error {
p, err := greeting.NewGreeterPlugin()
ctx := context.Background()
p, err := greeting.NewGreeterPlugin(ctx, greeting.GreeterPluginOption{})
if err != nil {
return err
}

ctx := context.Background()

greetingPlugin, err := p.Load(ctx, "plugin/greeting.wasm")
// Pass my host functions that are embedded into the plugin.
greetingPlugin, err := p.Load(ctx, "examples/host-functions/plugin/plugin.wasm", myHostFunctions{})
if err != nil {
return err
}
Expand All @@ -45,9 +41,11 @@ func run() error {
return nil
}

type hostFunctions struct{}
// myHostFunctions implements greeting.HostFunctions
type myHostFunctions struct{}

func (hostFunctions) HttpGet(ctx context.Context, request greeting.HttpGetRequest) (greeting.HttpGetResponse, error) {
// HttpGet is embedded into the plugin and can be called by the plugin.
func (myHostFunctions) HttpGet(ctx context.Context, request greeting.HttpGetRequest) (greeting.HttpGetResponse, error) {
resp, err := http.Get(request.Url)
if err != nil {
return greeting.HttpGetResponse{}, err
Expand Down
File renamed without changes.

0 comments on commit 1feeaa1

Please sign in to comment.