-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
config_inlet.go
137 lines (117 loc) · 3.32 KB
/
config_inlet.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
package inlets
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/muety/telepush/config"
"github.com/muety/telepush/model"
"github.com/muety/telepush/resolvers"
"github.com/muety/telepush/util"
"io"
"net/http"
"strings"
"text/template"
)
var templateFuncs = template.FuncMap{
"escapemd": util.EscapeMarkdown,
"div": util.Div,
}
type InletConfig struct {
Name string `yaml:"name,omitempty"`
ContentType string `yaml:"content_type,omitempty"`
Template string `yaml:"template,omitempty"`
HeaderVars map[string]string `yaml:"header_vars"`
}
type ConfigInlet struct {
Config *InletConfig
tpl *template.Template
}
func NewConfigInlet(inletConfig *InletConfig) (*ConfigInlet, error) {
inletConfig.Name = strings.ToLower(inletConfig.Name)
tpl, err := template.New(inletConfig.Name).Funcs(templateFuncs).Parse(inletConfig.Template)
if err != nil {
return nil, fmt.Errorf("failed to parse template for %s: %v\n", inletConfig.Name, err)
}
return &ConfigInlet{
Config: inletConfig,
tpl: tpl,
}, nil
}
func (c *ConfigInlet) SupportedMethods() []string {
return []string{http.MethodPost} // config-based inlets only support POST atm.
}
type templateVars struct {
Message interface{}
Vars map[string]string
}
func (c *ConfigInlet) Name() string {
return c.Config.Name
}
func (c *ConfigInlet) Handler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
payload, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
responseText, err := c.getTextResponse(payload, r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
message := &model.Message{
Text: responseText,
Type: resolvers.TextType,
Origin: c.getOrigin(payload, r),
}
ctx := r.Context()
ctx = context.WithValue(ctx, config.KeyMessage, message)
ctx = context.WithValue(ctx, config.KeyParams, &model.MessageOptions{
DisableLinkPreviews: strings.ToLower(r.URL.Query().Get("disable_link_previews")) == "true",
DisableMarkdown: strings.ToLower(r.URL.Query().Get("disable_markdown")) == "true",
})
h.ServeHTTP(w, r.WithContext(ctx))
})
}
func (c *ConfigInlet) getTextResponse(bodyBytes []byte, r *http.Request) (string, error) {
var payload interface{} = string(bodyBytes)
if c.Config.ContentType == "application/json" {
if err := json.Unmarshal(bodyBytes, &payload); err != nil {
return "", err
}
}
var buf bytes.Buffer
if err := c.tpl.Execute(&buf, templateVars{
Message: payload,
Vars: c.getHeaderVars(r),
}); err != nil {
return "", err
}
return buf.String(), nil
}
func (c *ConfigInlet) getOrigin(bodyBytes []byte, r *http.Request) string {
if r.Header.Get("content-type") == "application/json" {
var payload map[string]interface{}
if err := json.Unmarshal(bodyBytes, &payload); err != nil {
return model.DefaultOrigin
}
if v, ok := payload["origin"]; ok {
switch v.(type) {
case string:
return v.(string)
}
}
return model.DefaultOrigin
}
return model.DefaultOrigin
}
func (c *ConfigInlet) getHeaderVars(r *http.Request) map[string]string {
vars := make(map[string]string)
for k, v := range c.Config.HeaderVars {
vars[k] = r.Header.Get(v)
}
return vars
}