Files
archipelago/sdk/forge/serve.go
T

57 lines
1.5 KiB
Go

// Package forge is what a Go forge extension builds on: the
// generated forge.v1 contract and the handshake archie-core launches
// extensions with.
package forge
import (
"context"
"fmt"
"os"
goplugin "github.com/hashicorp/go-plugin"
"google.golang.org/grpc"
forgev1 "github.com/samcharles93/archipelago/sdk/forge/v1"
"github.com/samcharles93/archipelago/sdk/remote"
)
const surface = "forge"
type plugin struct {
goplugin.NetRPCUnsupportedPlugin
impl forgev1.ForgeServiceServer
}
func (p *plugin) GRPCServer(_ *goplugin.GRPCBroker, s *grpc.Server) error {
forgev1.RegisterForgeServiceServer(s, p.impl)
return nil
}
func (*plugin) GRPCClient(context.Context, *goplugin.GRPCBroker, *grpc.ClientConn) (any, error) {
return nil, nil
}
// Serve runs impl 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) (Forge, error)) {
impl := &server{build: build}
if handled, err := remote.Serve(func(s *grpc.Server) {
forgev1.RegisterForgeServiceServer(s, impl)
}); handled {
if err != nil {
fmt.Fprintln(os.Stderr, "forge:", 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,
})
}