Skip to content

Commit 68c41e6

Browse files
committed
remove direct dependency to eventhorizon
1 parent 2433215 commit 68c41e6

File tree

5 files changed

+76
-20
lines changed

5 files changed

+76
-20
lines changed

codegen/codegentemplates/backend.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ const BackendEventDefinitions = `package {{.Module.Id}}
9595
import (
9696
{{if .EventsImports.DateTime}} "time"
9797
{{end}}{{if .EventsImports.Date}} "github.com/function61/eventkit/guts"
98-
{{end}} "github.com/function61/eventhorizon/pkg/ehevent"
98+
{{end}} "github.com/function61/eventkit/eventlog"
9999
)
100100
101101
// WARNING: generated file
102102
103-
var EventTypes = ehevent.Allocators{
103+
var EventTypes = eventlog.Allocators{
104104
{{range .EventDefs}}
105-
"{{.EventKey}}": func() ehevent.Event { return &{{.GoStructName}}{meta: &ehevent.EventMeta{}} },{{end}}
105+
"{{.EventKey}}": func() eventlog.Event { return &{{.GoStructName}}{meta: &eventlog.EventMeta{}} },{{end}}
106106
}
107107
108108
@@ -121,7 +121,7 @@ func New{{.GoStructName}}({{.CtorArgs}}) *{{.GoStructName}} {
121121
{{end}}
122122
123123
{{range .EventDefs}}
124-
func (e *{{.GoStructName}}) Meta() *ehevent.EventMeta { return e.meta }{{end}}
124+
func (e *{{.GoStructName}}) Meta() *eventlog.EventMeta { return e.meta }{{end}}
125125
126126
{{range .EventDefs}}
127127
func (e *{{.GoStructName}}) MetaType() string { return "{{.EventKey}}" }{{end}}
@@ -131,10 +131,10 @@ func (e *{{.GoStructName}}) MetaType() string { return "{{.EventKey}}" }{{end}}
131131
type EventListener interface { {{range .EventDefs}}
132132
Apply{{.GoStructName}}(*{{.GoStructName}}) error{{end}}
133133
134-
HandleUnknownEvent(event ehevent.Event) error
134+
HandleUnknownEvent(event eventlog.Event) error
135135
}
136136
137-
func DispatchEvent(event ehevent.Event, listener EventListener) error {
137+
func DispatchEvent(event eventlog.Event, listener EventListener) error {
138138
switch e := event.(type) { {{range .EventDefs}}
139139
case *{{.GoStructName}}:
140140
return listener.Apply{{.GoStructName}}(e){{end}}

command/command.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"context"
66
"net/http"
77

8-
"github.com/function61/eventhorizon/pkg/ehevent"
8+
"github.com/function61/eventkit/eventlog"
99
)
1010

1111
type Command interface {
@@ -27,12 +27,12 @@ type Allocators map[string]func() Command
2727
type Ctx struct {
2828
Ctx context.Context // Go's cancellation context
2929

30-
Meta ehevent.EventMeta
30+
Meta eventlog.EventMeta
3131

3232
RemoteAddr string
3333
UserAgent string
3434

35-
raisedEvents []ehevent.Event
35+
raisedEvents []eventlog.Event
3636

3737
cookies []*http.Cookie
3838

@@ -42,7 +42,7 @@ type Ctx struct {
4242

4343
func NewCtx(
4444
ctx context.Context,
45-
meta ehevent.EventMeta,
45+
meta eventlog.EventMeta,
4646
remoteAddr string,
4747
userAgent string,
4848
) *Ctx {
@@ -51,16 +51,16 @@ func NewCtx(
5151
Meta: meta,
5252
RemoteAddr: remoteAddr,
5353
UserAgent: userAgent,
54-
raisedEvents: []ehevent.Event{},
54+
raisedEvents: []eventlog.Event{},
5555
cookies: []*http.Cookie{},
5656
}
5757
}
5858

59-
func (c *Ctx) GetRaisedEvents() []ehevent.Event {
59+
func (c *Ctx) GetRaisedEvents() []eventlog.Event {
6060
return c.raisedEvents
6161
}
6262

63-
func (c *Ctx) RaisesEvent(event ehevent.Event) {
63+
func (c *Ctx) RaisesEvent(event eventlog.Event) {
6464
c.raisedEvents = append(c.raisedEvents, event)
6565
}
6666

eventlog/event.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package eventlog
2+
3+
import (
4+
"time"
5+
)
6+
7+
func Meta(timestamp time.Time, userId string) EventMeta {
8+
return EventMeta{
9+
Timestamp: timestamp.UTC(),
10+
UserId: userId,
11+
}
12+
}
13+
14+
func MetaSystemUser(timestamp time.Time) EventMeta {
15+
return EventMeta{
16+
Timestamp: timestamp.UTC(),
17+
}
18+
}
19+
20+
func MetaWithImpersonator(timestamp time.Time, userId string, impersonatingUserId string) EventMeta {
21+
if impersonatingUserId == userId {
22+
// save some space, since this can be deduced
23+
impersonatingUserId = ""
24+
}
25+
26+
return EventMeta{
27+
Timestamp: timestamp.UTC(),
28+
UserId: userId,
29+
ImpersonatingUserId: impersonatingUserId,
30+
}
31+
}
32+
33+
type Allocators map[string]func() Event
34+
35+
// event has type (event's name) and metadata (common attributes shared by all events)
36+
type Event interface {
37+
MetaType() string
38+
Meta() *EventMeta
39+
}
40+
41+
type EventMeta struct {
42+
Timestamp time.Time
43+
TimestampOfRecording time.Time
44+
// whose resource was acted upon?
45+
// - user changes her password => target=<self> and actor=<self> (though actor omitted then)
46+
// - when admin changes another users password => target=<someone> and actor=<admin>
47+
UserId string
48+
// "who done this"? most times user acts on her own resources, but sometimes
49+
// e.g. a super user can do stuff to another user's (the target) resources
50+
ImpersonatingUserId string
51+
}
52+
53+
// returns "who acted on someones resource" (most times same as user id, but could be impersonator)
54+
func (m *EventMeta) ActingUserOrDefaultToTarget() string {
55+
if m.ImpersonatingUserId != "" {
56+
return m.ImpersonatingUserId
57+
} else {
58+
// no special "actor" recorded => user actioned her own resource
59+
return m.UserId
60+
}
61+
}

eventlog/interface.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package eventlog
22

3-
import (
4-
"github.com/function61/eventhorizon/pkg/ehevent"
5-
)
6-
73
// DEPRECATED: will soon be removed
84
type Log interface {
9-
Append(events []ehevent.Event) error
5+
Append(events []Event) error
106
}

httpcommand/httpcommand.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"net/http"
88
"time"
99

10-
"github.com/function61/eventhorizon/pkg/ehevent"
1110
"github.com/function61/eventkit/command"
1211
"github.com/function61/eventkit/eventlog"
1312
"github.com/function61/gokit/net/http/httpauth"
@@ -85,7 +84,7 @@ func Serve(
8584

8685
ctx := command.NewCtx(
8786
r.Context(),
88-
ehevent.Meta(time.Now(), userId),
87+
eventlog.Meta(time.Now(), userId),
8988
r.RemoteAddr,
9089
r.Header.Get("User-Agent"))
9190

0 commit comments

Comments
 (0)