Skip to content

Commit 97f79ea

Browse files
committed
Add middleware to interface
1 parent 03b7fa8 commit 97f79ea

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

middleware/processor.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package middleware
2+
3+
var processorInstance Processor
4+
5+
func SetProcessor(processor Processor) {
6+
processorInstance = processor
7+
}
8+
9+
func GetProcessor() Processor {
10+
if processorInstance == nil {
11+
panic("middleware processor is nil")
12+
}
13+
14+
return processorInstance
15+
}

middleware/types.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package middleware
2+
3+
type Processor interface {
4+
// Validate request
5+
// If returns nil, it will pass the request onwards
6+
// If returns a pointer, request will get replied to with provided response
7+
Request(resource string, headers map[string][]string) *RequestResponse
8+
9+
// Validate and filter response
10+
// If returns nil, it will pass the request onwards with the returned data
11+
// If returns a pointer, request will get replied to with provided response without data
12+
Response(resource string, headers map[string][]string, data []map[string]interface{}) ([]map[string]interface{}, *RequestResponse)
13+
}
14+
15+
type RequestResponse struct {
16+
// Return status code (http or otherwise)
17+
Code int
18+
19+
// Return message
20+
Message string
21+
}

plugins/types.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package plugins
22

33
import (
44
"github.com/StratoAPI/Interface/filter"
5+
"github.com/StratoAPI/Interface/middleware"
56
)
67

78
type Plugin interface {
@@ -63,6 +64,27 @@ type Filter interface {
6364
CreateFilter(filter string) (interface{}, error)
6465
}
6566

67+
type Middleware interface {
68+
// Initialize the middleware.
69+
Initialize() error
70+
71+
// Start the middleware. Does not have to be blocking.
72+
Start() error
73+
74+
// Graceful stopping of the middleware with a 30s timeout.
75+
Stop() error
76+
77+
// Validate request
78+
// If returns nil, it will pass the request onwards
79+
// If returns a pointer, request will get replied to with provided response
80+
Request(resource string, headers map[string][]string) *middleware.RequestResponse
81+
82+
// Validate and filter response
83+
// If returns nil, it will pass the request onwards with the returned data
84+
// If returns a pointer, request will get replied to with provided response without data
85+
Response(resource string, headers map[string][]string, data []map[string]interface{}) ([]map[string]interface{}, *middleware.RequestResponse)
86+
}
87+
6688
type Registry interface {
6789
// Register a facade
6890
RegisterFacade(name string, facade Facade) error

0 commit comments

Comments
 (0)