-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.go
More file actions
87 lines (69 loc) · 1.85 KB
/
plugin.go
File metadata and controls
87 lines (69 loc) · 1.85 KB
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
// Package protoreg provides a RoadRunner plugin for protobuf registry management.
// It parses .proto files and their dependencies, building a registry of types and
// services that can be used by other plugins such as gRPC for dynamic message handling.
package protoreg
import (
"sync"
"github.com/roadrunner-server/endure/v2/dep"
"github.com/roadrunner-server/errors"
"go.uber.org/zap"
_ "google.golang.org/genproto/protobuf/ptype"
)
const (
pluginName string = "protoreg"
rootPluginName string = "grpc"
)
type Configurer interface {
// UnmarshalKey takes a single key and unmarshal it into a Struct.
UnmarshalKey(name string, out any) error
// Has checks if a config section exists.
Has(name string) bool
// Experimental returns true if experimental mode is enabled.
Experimental() bool
}
type Logger interface {
NamedLogger(name string) *zap.Logger
}
type Plugin struct {
mu *sync.RWMutex
config *Config
log *zap.Logger
registry *ProtoRegistry
}
func (p *Plugin) Init(cfg Configurer, log Logger) error {
const op = errors.Op("protoreg_plugin_init")
if !cfg.Has(pluginName) {
return errors.E(errors.Disabled)
}
if !cfg.Has(rootPluginName) {
return errors.E(op, errors.Disabled)
}
err := cfg.UnmarshalKey(pluginName, &p.config)
if err != nil {
return errors.E(op, err)
}
err = p.config.InitDefaults()
if err != nil {
return errors.E(op, err)
}
p.log = log.NamedLogger(pluginName)
p.mu = &sync.RWMutex{}
p.registry, err = p.InitRegistry()
if err != nil {
return errors.E(op, err)
}
p.log.Info("protoreg initialized")
return nil
}
func (p *Plugin) Name() string {
return pluginName
}
func (p *Plugin) Provides() []*dep.Out {
return []*dep.Out{
dep.Bind((*Registry)(nil), p.ProtoRegistry),
}
}
// ProtoRegistry returns a protobuf registry
func (p *Plugin) ProtoRegistry() *ProtoRegistry {
return p.registry
}