Files

55 lines
1.3 KiB
Go

package guard
import (
"context"
"fmt"
"os"
goplugin "github.com/hashicorp/go-plugin"
"google.golang.org/grpc"
guardv1 "github.com/samcharles93/archipelago/sdk/guard/v1"
"github.com/samcharles93/archipelago/sdk/remote"
)
const surface = "guard"
type plugin struct {
goplugin.NetRPCUnsupportedPlugin
impl guardv1.GuardServiceServer
}
func (p *plugin) GRPCServer(_ *goplugin.GRPCBroker, s *grpc.Server) error {
guardv1.RegisterGuardServiceServer(s, p.impl)
return nil
}
func (*plugin) GRPCClient(context.Context, *goplugin.GRPCBroker, *grpc.ClientConn) (any, error) {
return nil, nil
}
// Serve runs the guard build returns as the extension process, either as a
// standalone remote gRPC server when ARCHIE_EXTENSION_LISTEN is set or as a
// go-plugin subprocess.
func Serve(build func(Config) (Guard, error)) {
impl := &server{build: build}
if handled, err := remote.Serve(func(s *grpc.Server) {
guardv1.RegisterGuardServiceServer(s, impl)
}); handled {
if err != nil {
fmt.Fprintln(os.Stderr, "guard:", err)
os.Exit(1)
}
return
}
goplugin.Serve(&goplugin.ServeConfig{
HandshakeConfig: goplugin.HandshakeConfig{
ProtocolVersion: 1,
MagicCookieKey: "ARCHIE_EXTENSION",
MagicCookieValue: "archie",
},
Plugins: goplugin.PluginSet{surface: &plugin{impl: impl}},
GRPCServer: goplugin.DefaultGRPCServer,
})
}