forked from OSNVR/OS-NVR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addon.go
175 lines (153 loc) · 4.73 KB
/
addon.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// SPDX-License-Identifier: GPL-2.0-or-later
package nvr
import (
"context"
stdLog "log"
"nvr/pkg/monitor"
"nvr/pkg/storage"
"nvr/pkg/web"
"nvr/pkg/web/auth"
)
type appRunHook func(context.Context, *App) error
type hookList struct {
newAuthenticator auth.NewAuthenticatorFunc
onAppRun []appRunHook
template []web.TemplateHook
templateSub []web.TemplateHook
templateData []web.TemplateDataFunc
monitorStart []monitor.StartHook
monitorInputProcess []monitor.StartInputHook
monitorEvent []monitor.EventHook
monitorRecSave []monitor.RecSaveHook
monitorRecSaved []monitor.RecSavedHook
migrationMonitor []monitor.MigationHook
logSource []string
}
var hooks = &hookList{}
// SetAuthenticator is used to set the authenticator.
func SetAuthenticator(a auth.NewAuthenticatorFunc) {
if hooks.newAuthenticator != nil {
stdLog.Fatalf("\n\nERROR: Only a single autentication addon is allowed.\n\n")
}
hooks.newAuthenticator = a
}
// RegisterAppRunHook registers hook that's called when app runs.
func RegisterAppRunHook(h appRunHook) {
hooks.onAppRun = append(hooks.onAppRun, h)
}
// RegisterTplHook registers function used to modify templates.
func RegisterTplHook(h web.TemplateHook) {
hooks.template = append(hooks.template, h)
}
// RegisterTplSubHook registers function used to modify sub templates.
func RegisterTplSubHook(h web.TemplateHook) {
hooks.templateSub = append(hooks.templateSub, h)
}
// RegisterTplDataHook registers function thats called
// on page render. Used to modify template data.
func RegisterTplDataHook(h web.TemplateDataFunc) {
hooks.templateData = append(hooks.templateData, h)
}
// RegisterMonitorStartHook registers hook that's called when the monitor starts.
func RegisterMonitorStartHook(h monitor.StartHook) {
hooks.monitorStart = append(hooks.monitorStart, h)
}
// RegisterMonitorInputProcessHook registers hook that's
// called when the monitor input process starts.
func RegisterMonitorInputProcessHook(h monitor.StartInputHook) {
hooks.monitorInputProcess = append(hooks.monitorInputProcess, h)
}
// RegisterMonitorEventHook registers hook that's called on every event.
func RegisterMonitorEventHook(h monitor.EventHook) {
hooks.monitorEvent = append(hooks.monitorEvent, h)
}
// RegisterMonitorRecSaveHook registers hook that's called when monitor saves recording.
func RegisterMonitorRecSaveHook(h monitor.RecSaveHook) {
hooks.monitorRecSave = append(hooks.monitorRecSave, h)
}
// RegisterMonitorRecSavedHook registers hook that's called after monitor have saved recording.
func RegisterMonitorRecSavedHook(h monitor.RecSavedHook) {
hooks.monitorRecSaved = append(hooks.monitorRecSaved, h)
}
// RegisterMigrationMonitorHook is called when each monitor config is loaded.
func RegisterMigrationMonitorHook(h monitor.MigationHook) {
hooks.migrationMonitor = append(hooks.migrationMonitor, h)
}
// RegisterLogSource adds log source.
func RegisterLogSource(s []string) {
hooks.logSource = append(hooks.logSource, s...)
}
func (h *hookList) appRun(ctx context.Context, app *App) error {
for _, hook := range h.onAppRun {
if err := hook(ctx, app); err != nil {
return err
}
}
return nil
}
func (h *hookList) tplHooks() web.TemplateHooks {
tplHook := func(pageFiles map[string]string) error {
for _, hook := range h.template {
if err := hook(pageFiles); err != nil {
return err
}
}
return nil
}
tplSubHook := func(pageFiles map[string]string) error {
for _, hook := range h.templateSub {
if err := hook(pageFiles); err != nil {
return err
}
}
return nil
}
return web.TemplateHooks{
Tpl: tplHook,
Sub: tplSubHook,
}
}
func (h *hookList) monitor() *monitor.Hooks {
startHook := func(ctx context.Context, m *monitor.Monitor) {
for _, hook := range h.monitorStart {
hook(ctx, m)
}
}
startInputHook := func(ctx context.Context, i *monitor.InputProcess, args *[]string) {
for _, hook := range h.monitorInputProcess {
hook(ctx, i, args)
}
}
eventHook := func(r *monitor.Recorder, event *storage.Event) {
for _, hook := range h.monitorEvent {
hook(r, event)
}
}
recSaveHook := func(r *monitor.Recorder, args *string) {
for _, hook := range h.monitorRecSave {
hook(r, args)
}
}
recSavedHook := func(r *monitor.Recorder, recPath string, recData storage.RecordingData) {
for _, hook := range h.monitorRecSaved {
hook(r, recPath, recData)
}
}
migrateHook := func(conf monitor.RawConfig) error {
for _, hook := range h.migrationMonitor {
err := hook(conf)
if err != nil {
return err
}
}
return nil
}
return &monitor.Hooks{
Start: startHook,
StartInput: startInputHook,
Event: eventHook,
RecSave: recSaveHook,
RecSaved: recSavedHook,
Migrate: migrateHook,
}
}