-
Notifications
You must be signed in to change notification settings - Fork 1
/
callback.go
54 lines (45 loc) · 1.43 KB
/
callback.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
package immune
import (
"context"
"fmt"
"strings"
)
type CallbackConfiguration struct {
MaxWaitSeconds uint `json:"max_wait_seconds"`
Port uint `json:"port"`
Route string `json:"route"`
SSL bool `json:"ssl" envconfig:"IMMUNE_SSL"`
SSLKeyFile string `json:"ssl_key_file" envconfig:"IMMUNE_SSL_KEY_FILE"`
SSLCertFile string `json:"ssl_cert_file" envconfig:"IMMUNE_SSL_CERT_FILE"`
IDLocation string `json:"id_location"`
}
const CallbackIDFieldName = "immune_callback_id"
// InjectCallbackID injects a callback id into field(expected to be a map[string]interface{} in r)
// in r, using CallbackIDFieldName as the key and value as the value.
func InjectCallbackID(field string, value interface{}, r M) error {
// we may have separators referencing deeper fields in r e.g data.uid
parts := strings.Split(field, ".")
if len(parts) < 2 { // if it's less than 2, then there is no '.' in field
v, ok := r[field]
if !ok {
return fmt.Errorf("the field %s, does not exist", field)
}
m, ok := v.(map[string]interface{})
if !ok {
return fmt.Errorf("the field %s, is not an object in the request body", field)
}
m[CallbackIDFieldName] = value
return nil
}
nextLevel, err := getM(r, parts)
if err != nil {
return err
}
nextLevel[CallbackIDFieldName] = value
return nil
}
type CallbackServer interface {
ReceiveCallback(rc chan<- *Signal)
Start(ctx context.Context) error
Stop()
}