/
struct.go
63 lines (54 loc) · 1.57 KB
/
struct.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
package ecomapp
import (
"plugin"
"github.com/codedv8/go-ecom-app/urihandler"
ecomdb "github.com/codedv8/go-ecom-db"
"github.com/gin-gonic/gin"
)
// Application - Struct that defines the application
type Application struct {
DB *ecomdb.DBConnector
Hooks map[string][]Hook
SystemModules []Module
UserModules []Module
Router *gin.Engine
URIHandler *urihandler.URIHandler
}
// HookCallback - type definition for a HookCallback
type HookCallback func(*func(interface{}) (bool, error)) (bool, error)
// Hook - Struct to define a hook
type Hook struct {
Callback func(interface{}) (bool, error)
}
// ListenToHook - Register a hook listener by name and with its callback function
func (app *Application) ListenToHook(name string, callback func(interface{}) (bool, error)) {
hook := &Hook{
Callback: callback,
}
app.Hooks[name] = append(app.Hooks[name], *hook)
}
// CallHook - Call and handle hokks by the given name and execute them with the supplied payload
func (app *Application) CallHook(name string, payload interface{}) (bool, bool, error) {
handled := false
for _, hook := range app.Hooks[name] {
next, err := hook.Callback(payload)
if err != nil {
return handled, next, err
}
handled = true
if next == false {
return handled, next, err
}
}
return handled, true, nil
}
// type Plugin struct {
// }
// Module - Struct that defined a module (plugin)
type Module struct {
SysInit func(app *Application) error
Init func(app *Application) error
Done func(app *Application) error
Plugin *plugin.Plugin
Filename string
}