Skip to content
This repository was archived by the owner on May 14, 2022. It is now read-only.

Commit 738e84c

Browse files
committed
feat: try gRPC approach
1 parent 6134f38 commit 738e84c

File tree

11 files changed

+123
-201
lines changed

11 files changed

+123
-201
lines changed

README.md

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,9 @@
11
# StackHead Plugin API
22

3-
## Expected structure
3+
## Requirements
44

5-
```go
6-
package main
5+
...
76

8-
import "github.com/getstackhead/pluginlib"
7+
## Example
98

10-
type MyPlugin struct {
11-
}
12-
13-
var PluginConfig = pluginlib.PluginConfig{
14-
Name: "Test",
15-
Description: "This is an example StackHead Proxy plugin",
16-
Version: "0.0.0-dev",
17-
Authors: []string{"Your Name"},
18-
PluginType: pluginlib.PluginType.PROXY,
19-
}
20-
21-
func (p MyPlugin) Setup() {
22-
// implement software setup action
23-
}
24-
25-
func (p MyPlugin) Deploy(project pluginlib.Project) {
26-
// implement project deploy action
27-
}
28-
29-
func (p MyPlugin) Destroy(project pluginlib.Project) {
30-
// implement project destroy action
31-
}
32-
33-
func (p MyPlugin) TriggerHook(hookName string, project pluginlib.Project) {
34-
// triggered by hooks
35-
}
36-
37-
// Export plugin to StackHead. Must be named "Plugin"!
38-
var Plugin MyPlugin
39-
```
40-
41-
```shell
42-
go build -buildmode=plugin -o plugin.so main.go
43-
```
9+
See [./example](./example)

events.go

Lines changed: 0 additions & 17 deletions
This file was deleted.

example/main.go

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,47 @@
11
package main
22

33
import (
4-
"fmt"
4+
"log"
5+
"net"
56

6-
"github.com/getstackhead/pluginlib"
7-
)
8-
9-
var Plugin MyPlugin
10-
var PluginConfig = pluginlib.PluginConfig{
11-
Name: "Test",
12-
Description: "This is an example StackHead Proxy plugin",
13-
Version: "0.0.0-dev",
14-
Authors: []string{"Mario Lubenka"},
15-
PluginType: pluginlib.PluginType.PROXY,
16-
}
7+
"github.com/phayes/freeport"
8+
"google.golang.org/grpc"
179

18-
type MyPlugin struct {
19-
}
10+
pb "github.com/getstackhead/pluginlib/gen"
11+
)
2012

21-
func (p MyPlugin) Deploy(project pluginlib.Project) {
22-
fmt.Println("Hello " + project.Name)
23-
}
13+
var Plugin MyPluginWrapper
2414

25-
func (p MyPlugin) Destroy(project pluginlib.Project) {
26-
fmt.Println("Bye " + project.Name)
15+
type MyPluginWrapper struct {
2716
}
2817

29-
func (p MyPlugin) Setup() {
30-
fmt.Println("Setup...")
18+
type plugin struct {
19+
pb.UnimplementedStackHeadPlugin
3120
}
3221

33-
func (p MyPlugin) TriggerHook(hookName string, project pluginlib.Project) {
34-
fmt.Println("Hook " + hookName + " triggered for project " + project.Name + ".")
22+
func (p MyPluginWrapper) LaunchServer(callback func(int)) {
23+
// Set up connection with local StackHead server
24+
conn, err := grpc.Dial("localhost:1412", grpc.WithInsecure(), grpc.WithBlock())
25+
if err != nil {
26+
log.Fatalf("did not connect: %v", err)
27+
}
28+
defer conn.Close()
29+
StackHeadClient := pb.NewStackHeadServiceClient(conn)
30+
31+
port, err := freeport.GetFreePort()
32+
if err != nil {
33+
log.Fatal(err)
34+
return
35+
}
36+
lis, err := net.Listen("tcp", ":"+port)
37+
if err != nil {
38+
log.Fatalf("failed to listen: %v", err)
39+
}
40+
s := grpc.NewServer()
41+
pb.RegisterStackHeadPluginServer(s, &plugin{})
42+
log.Printf("server listening at %v", lis.Addr())
43+
if err := s.Serve(lis); err != nil {
44+
log.Fatalf("failed to serve: %v", err)
45+
}
46+
callback(port)
3547
}

go.mod

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
module github.com/getstackhead/pluginlib
22

33
go 1.17
4-
5-
require (
6-
github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef
7-
github.com/valyala/gorpc v0.0.0-20160519171614-908281bef774
8-
)

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +0,0 @@
1-
github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef h1:2JGTg6JapxP9/R33ZaagQtAM4EkkSYnIAlOG5EI8gkM=
2-
github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef/go.mod h1:JS7hed4L1fj0hXcyEejnW57/7LCetXggd+vwrRnYeII=
3-
github.com/valyala/gorpc v0.0.0-20160519171614-908281bef774 h1:SUHFQHAaySqF0YHCmmm0EIFooFZpDPpi5KTom7YJ07c=
4-
github.com/valyala/gorpc v0.0.0-20160519171614-908281bef774/go.mod h1:8uNqM1i7pr0jO7gdvbNCgsSa8Ki2vMh7JCQxO9BlF90=

install.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

plugin.go

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,5 @@
11
package pluginlib
22

3-
import (
4-
"plugin"
5-
)
6-
73
type Plugin interface {
8-
Setup() <-chan error
9-
Deploy(project Project) <-chan error
10-
Destroy(project Project) <-chan error
11-
TriggerHook(hookName string, project Project)
12-
}
13-
14-
func LoadPlugin(path string) (Plugin, *PluginConfig, error) {
15-
// load module
16-
plug, err := plugin.Open(path)
17-
if err != nil {
18-
return nil, nil, err
19-
}
20-
21-
// 2. look up a symbol (an exported function or variable)
22-
symPlugin, err := plug.Lookup("Plugin")
23-
if err != nil {
24-
return nil, nil, err
25-
}
26-
var ok bool
27-
var pluginObj Plugin
28-
pluginObj, ok = symPlugin.(Plugin)
29-
if !ok {
30-
return nil, nil, err
31-
}
32-
33-
// 3. look up a symbol (an exported function or variable)
34-
symPluginConfig, err := plug.Lookup("PluginConfig")
35-
if err != nil {
36-
return nil, nil, err
37-
}
38-
var pluginConfig *PluginConfig
39-
pluginConfig, ok = symPluginConfig.(*PluginConfig)
40-
if !ok {
41-
return nil, nil, err
42-
}
43-
44-
// 4. Assert that loaded symbol is of a desired type
45-
return pluginObj, pluginConfig, nil
4+
LaunchServer(callback func(int))
465
}

plugin_config.go

Lines changed: 0 additions & 31 deletions
This file was deleted.

project.go

Lines changed: 0 additions & 5 deletions
This file was deleted.

stackhead.go

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)