-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
81 lines (63 loc) · 1.6 KB
/
command.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
// Command running interface => command gets some context, raises some events when it runs
package command
import (
"context"
"net/http"
"github.com/function61/eventhorizon/pkg/ehevent"
)
type Command interface {
Key() string
Validate() error
MiddlewareChain() string
}
// can invoke any command in typesafe manner
type Invoker interface {
Invoke(cmdGeneric Command, ctx *Ctx) error
}
// map keyed by command name (command.Key()) values are functions that allocates a new
// specific command struct
type Allocators map[string]func() Command
// context for invoking command
type Ctx struct {
Ctx context.Context // Go's cancellation context
Meta ehevent.EventMeta
RemoteAddr string
UserAgent string
raisedEvents []ehevent.Event
cookies []*http.Cookie
// if you need to return to the client an ID of the record that was created
createdRecordId string
}
func NewCtx(
ctx context.Context,
meta ehevent.EventMeta,
remoteAddr string,
userAgent string,
) *Ctx {
return &Ctx{
Ctx: ctx,
Meta: meta,
RemoteAddr: remoteAddr,
UserAgent: userAgent,
raisedEvents: []ehevent.Event{},
cookies: []*http.Cookie{},
}
}
func (c *Ctx) GetRaisedEvents() []ehevent.Event {
return c.raisedEvents
}
func (c *Ctx) RaisesEvent(event ehevent.Event) {
c.raisedEvents = append(c.raisedEvents, event)
}
func (c *Ctx) AddCookie(cookie *http.Cookie) {
c.cookies = append(c.cookies, cookie)
}
func (c *Ctx) CreatedRecordId(id string) {
c.createdRecordId = id
}
func (c *Ctx) GetCreatedRecordId() string {
return c.createdRecordId
}
func (c *Ctx) Cookies() []*http.Cookie {
return c.cookies
}