-
Notifications
You must be signed in to change notification settings - Fork 2
/
router.go
137 lines (121 loc) · 3.52 KB
/
router.go
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package restate
import (
"github.com/restatedev/sdk-go/encoding"
"github.com/restatedev/sdk-go/internal"
"github.com/restatedev/sdk-go/internal/options"
"github.com/restatedev/sdk-go/internal/state"
)
// ServiceDefinition is the set of methods implemented by both services and virtual objects
type ServiceDefinition interface {
Name() string
Type() internal.ServiceType
// Set of handlers associated with this service definition
Handlers() map[string]state.Handler
}
// serviceDefinition stores a list of handlers under a named service
type serviceDefinition struct {
name string
handlers map[string]state.Handler
options options.ServiceDefinitionOptions
typ internal.ServiceType
}
var _ ServiceDefinition = &serviceDefinition{}
// Name returns the name of the service described in this definition
func (r *serviceDefinition) Name() string {
return r.name
}
// Handlers returns the list of handlers in this service definition
func (r *serviceDefinition) Handlers() map[string]state.Handler {
return r.handlers
}
// Type returns the type of this service definition (Service or Virtual Object)
func (r *serviceDefinition) Type() internal.ServiceType {
return r.typ
}
type service struct {
serviceDefinition
}
// NewService creates a new named Service
func NewService(name string, opts ...options.ServiceDefinitionOption) *service {
o := options.ServiceDefinitionOptions{}
for _, opt := range opts {
opt.BeforeServiceDefinition(&o)
}
if o.DefaultCodec == nil {
o.DefaultCodec = encoding.JSONCodec
}
return &service{
serviceDefinition: serviceDefinition{
name: name,
handlers: make(map[string]state.Handler),
options: o,
typ: internal.ServiceType_SERVICE,
},
}
}
// Handler registers a new Service handler by name
func (r *service) Handler(name string, handler state.Handler) *service {
if handler.GetOptions().Codec == nil {
handler.GetOptions().Codec = r.options.DefaultCodec
}
r.handlers[name] = handler
return r
}
type object struct {
serviceDefinition
}
// NewObject creates a new named Virtual Object
func NewObject(name string, opts ...options.ServiceDefinitionOption) *object {
o := options.ServiceDefinitionOptions{}
for _, opt := range opts {
opt.BeforeServiceDefinition(&o)
}
if o.DefaultCodec == nil {
o.DefaultCodec = encoding.JSONCodec
}
return &object{
serviceDefinition: serviceDefinition{
name: name,
handlers: make(map[string]state.Handler),
options: o,
typ: internal.ServiceType_VIRTUAL_OBJECT,
},
}
}
// Handler registers a new Virtual Object handler by name
func (r *object) Handler(name string, handler state.Handler) *object {
if handler.GetOptions().Codec == nil {
handler.GetOptions().Codec = r.options.DefaultCodec
}
r.handlers[name] = handler
return r
}
type workflow struct {
serviceDefinition
}
// NewWorkflow creates a new named Workflow
func NewWorkflow(name string, opts ...options.ServiceDefinitionOption) *workflow {
o := options.ServiceDefinitionOptions{}
for _, opt := range opts {
opt.BeforeServiceDefinition(&o)
}
if o.DefaultCodec == nil {
o.DefaultCodec = encoding.JSONCodec
}
return &workflow{
serviceDefinition: serviceDefinition{
name: name,
handlers: make(map[string]state.Handler),
options: o,
typ: internal.ServiceType_WORKFLOW,
},
}
}
// Handler registers a new Workflow handler by name
func (r *workflow) Handler(name string, handler state.Handler) *workflow {
if handler.GetOptions().Codec == nil {
handler.GetOptions().Codec = r.options.DefaultCodec
}
r.handlers[name] = handler
return r
}