Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

Commit da78c91

Browse files
authored
refactor packages structure to avoid cyclic dependecies (#358)
This commit changes the structure of the code to avoid cyclic dependencies. All domain objects are now in function, event and subscription package without external dependencies. Function and Subscription service backed by libkv is in libkv package.
1 parent 773adad commit da78c91

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1256
-1576
lines changed

api/config.go

Lines changed: 0 additions & 58 deletions
This file was deleted.

api/metrics.go

Lines changed: 0 additions & 31 deletions
This file was deleted.

cmd/event-gateway/main.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ import (
77
"strings"
88
"time"
99

10+
"github.com/serverless/event-gateway/router"
1011
"github.com/serverless/libkv"
1112
"github.com/serverless/libkv/store"
1213
etcd "github.com/serverless/libkv/store/etcd/v3"
1314
"go.uber.org/zap"
1415
"go.uber.org/zap/zapcore"
1516

16-
"github.com/serverless/event-gateway/api"
17+
"github.com/serverless/event-gateway/httpapi"
1718
"github.com/serverless/event-gateway/internal/cache"
1819
"github.com/serverless/event-gateway/internal/embedded"
19-
"github.com/serverless/event-gateway/internal/httpapi"
20+
intstore "github.com/serverless/event-gateway/internal/store"
2021
"github.com/serverless/event-gateway/internal/sync"
22+
eventgateway "github.com/serverless/event-gateway/libkv"
2123
"github.com/serverless/event-gateway/plugin"
22-
"github.com/serverless/event-gateway/router"
2324
)
2425

2526
var version = "dev"
@@ -67,7 +68,8 @@ func main() {
6768
embedded.EmbedEtcd(*embedDataDir, *embedPeerAddr, *embedCliAddr, shutdownGuard)
6869
}
6970

70-
kv, err := libkv.NewStore(
71+
// KV store
72+
kvstore, err := libkv.NewStore(
7173
store.ETCDV3,
7274
strings.Split(*dbHosts, ","),
7375
&store.Config{
@@ -78,28 +80,34 @@ func main() {
7880
log.Fatal("Cannot create KV client.", zap.Error(err))
7981
}
8082

83+
// Implementation of function and subscription services
84+
service := &eventgateway.Service{
85+
FunctionStore: intstore.NewPrefixed("/serverless-event-gateway/functions", kvstore),
86+
SubscriptionStore: intstore.NewPrefixed("/serverless-event-gateway/subscriptions", kvstore),
87+
EndpointStore: intstore.NewPrefixed("/serverless-event-gateway/endpoints", kvstore),
88+
Log: log,
89+
}
90+
91+
// Plugin manager
8192
pluginManager := plugin.NewManager(plugins, log)
8293
err = pluginManager.Connect()
8394
if err != nil {
8495
log.Fatal("Loading plugins failed.", zap.Error(err))
8596
}
8697

87-
targetCache := cache.NewTarget("/serverless-event-gateway", kv, log)
98+
// Router
99+
targetCache := cache.NewTarget("/serverless-event-gateway", kvstore, log)
88100
router := router.New(*workersNumber, *workersBacklog, targetCache, pluginManager, log)
89101
router.StartWorkers()
90102

91-
api.StartEventsAPI(httpapi.Config{
92-
KV: kv,
93-
Log: log,
103+
httpapi.StartEventsAPI(router, httpapi.ServerConfig{
94104
TLSCrt: eventsTLSCrt,
95105
TLSKey: eventsTLSKey,
96106
Port: *eventsPort,
97107
ShutdownGuard: shutdownGuard,
98-
}, router)
108+
})
99109

100-
api.StartConfigAPI(httpapi.Config{
101-
KV: kv,
102-
Log: log,
110+
httpapi.StartConfigAPI(service, service, httpapi.ServerConfig{
103111
TLSCrt: configTLSCrt,
104112
TLSKey: configTLSKey,
105113
Port: *configPort,

event/event.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import (
1010
uuid "github.com/satori/go.uuid"
1111
)
1212

13-
// Event is a default event structure. All data that passes through the Event Gateway is formatted as an Event, based on this schema.
13+
// Event is a default event structure. All data that passes through the Event Gateway is formatted as an Event, based on
14+
// this schema.
1415
type Event struct {
1516
Type Type `json:"event"`
1617
ID string `json:"id"`
@@ -19,8 +20,8 @@ type Event struct {
1920
DataType string `json:"dataType"`
2021
}
2122

22-
// NewEvent return new instance of Event.
23-
func NewEvent(eventType Type, mime string, payload interface{}) *Event {
23+
// New return new instance of Event.
24+
func New(eventType Type, mime string, payload interface{}) *Event {
2425
return &Event{
2526
Type: eventType,
2627
ID: uuid.NewV4().String(),

event/system.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package event
33
import (
44
"net/http"
55

6-
"github.com/serverless/event-gateway/functions"
6+
"github.com/serverless/event-gateway/function"
77
)
88

9-
// SystemEventReceivedType is a system event emmited when the Event Gateway receives an event.
9+
// SystemEventReceivedType is a system event emitted when the Event Gateway receives an event.
1010
const SystemEventReceivedType = Type("gateway.event.received")
1111

1212
// SystemEventReceivedData struct.
@@ -16,31 +16,31 @@ type SystemEventReceivedData struct {
1616
Headers http.Header `json:"header"`
1717
}
1818

19-
// SystemFunctionInvokingType is a system event emmited before invoking a function.
19+
// SystemFunctionInvokingType is a system event emitted before invoking a function.
2020
const SystemFunctionInvokingType = Type("gateway.function.invoking")
2121

2222
// SystemFunctionInvokingData struct.
2323
type SystemFunctionInvokingData struct {
24-
FunctionID functions.FunctionID `json:"functionId"`
25-
Event Event `json:"event"`
24+
FunctionID function.ID `json:"functionId"`
25+
Event Event `json:"event"`
2626
}
2727

28-
// SystemFunctionInvokedType is a system event emmited after successful function invocation.
28+
// SystemFunctionInvokedType is a system event emitted after successful function invocation.
2929
const SystemFunctionInvokedType = Type("gateway.function.invoked")
3030

3131
// SystemFunctionInvokedData struct.
3232
type SystemFunctionInvokedData struct {
33-
FunctionID functions.FunctionID `json:"functionId"`
34-
Event Event `json:"event"`
35-
Result []byte `json:"result"`
33+
FunctionID function.ID `json:"functionId"`
34+
Event Event `json:"event"`
35+
Result []byte `json:"result"`
3636
}
3737

38-
// SystemFunctionInvocationFailedType is a system event emmited after successful function invocation.
38+
// SystemFunctionInvocationFailedType is a system event emitted after successful function invocation.
3939
const SystemFunctionInvocationFailedType = Type("gateway.function.invocationFailed")
4040

4141
// SystemFunctionInvocationFailedData struct.
4242
type SystemFunctionInvocationFailedData struct {
43-
FunctionID functions.FunctionID `json:"functionId"`
44-
Event Event `json:"event"`
45-
Error []byte `json:"result"`
43+
FunctionID function.ID `json:"functionId"`
44+
Event Event `json:"event"`
45+
Error []byte `json:"result"`
4646
}
Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,59 @@
1-
package functions
1+
package function
22

33
import (
44
"fmt"
55
)
66

7-
// ErrNotFound occurs when function couldn't been found in the discovery.
8-
type ErrNotFound struct {
9-
ID FunctionID
7+
// ErrFunctionNotFound occurs when function couldn't been found in the discovery.
8+
type ErrFunctionNotFound struct {
9+
ID ID
1010
}
1111

12-
func (e ErrNotFound) Error() string {
12+
func (e ErrFunctionNotFound) Error() string {
1313
return fmt.Sprintf("Function %q not found.", string(e.ID))
1414
}
1515

16-
// ErrAlreadyRegistered occurs when function with specified name is already registered..
17-
type ErrAlreadyRegistered struct {
18-
ID FunctionID
16+
// ErrFunctionAlreadyRegistered occurs when function with specified name is already registered.
17+
type ErrFunctionAlreadyRegistered struct {
18+
ID ID
1919
}
2020

21-
func (e ErrAlreadyRegistered) Error() string {
21+
func (e ErrFunctionAlreadyRegistered) Error() string {
2222
return fmt.Sprintf("Function %q already registered.", string(e.ID))
2323
}
2424

25-
// ErrValidation occurs when function payload doesn't validate.
26-
type ErrValidation struct {
27-
message string
25+
// ErrFunctionValidation occurs when function payload doesn't validate.
26+
type ErrFunctionValidation struct {
27+
Message string
2828
}
2929

30-
func (e ErrValidation) Error() string {
31-
return fmt.Sprintf("Function doesn't validate. Validation error: %q", e.message)
30+
func (e ErrFunctionValidation) Error() string {
31+
return fmt.Sprintf("Function doesn't validate. Validation error: %q", e.Message)
3232
}
3333

3434
// ErrFunctionCallFailed occurs when function call failed because of provider error.
3535
type ErrFunctionCallFailed struct {
36-
original error
36+
Original error
3737
}
3838

3939
func (e ErrFunctionCallFailed) Error() string {
40-
return fmt.Sprintf("Function call failed. Error: %q", e.original)
40+
return fmt.Sprintf("Function call failed. Error: %q", e.Original)
4141
}
4242

4343
// ErrFunctionProviderError occurs when function call failed because of provider error.
4444
type ErrFunctionProviderError struct {
45-
original error
45+
Original error
4646
}
4747

4848
func (e ErrFunctionProviderError) Error() string {
49-
return fmt.Sprintf("Function call failed because of provider error. Error: %q", e.original)
49+
return fmt.Sprintf("Function call failed because of provider error. Error: %q", e.Original)
5050
}
5151

5252
// ErrFunctionError occurs when function call failed because of function error.
5353
type ErrFunctionError struct {
54-
original error
54+
Original error
5555
}
5656

5757
func (e ErrFunctionError) Error() string {
58-
return fmt.Sprintf("Function call failed because of runtime error. Error: %q", e.original)
58+
return fmt.Sprintf("Function call failed because of runtime error. Error: %q", e.Original)
5959
}

0 commit comments

Comments
 (0)