/
handler.go
144 lines (130 loc) · 3.77 KB
/
handler.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
package main
import (
"bytes"
"database/sql"
"encoding/json"
"io"
"net/http"
"strings"
"github.com/alecthomas/units"
"github.com/bwmarrin/discordgo"
"github.com/icco/relay/database/model"
"github.com/icco/relay/parse"
"go.uber.org/zap"
)
func healthcheckHandler(w http.ResponseWriter, r *http.Request) {
out := map[string]string{"status": "ok"}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(out); err != nil {
log.Errorw("could not encode json", zap.Error(err))
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func hookHandler(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ct := r.Header.Get("content-type")
log.Debugw("got content-type", "content-type", ct)
buf, err := io.ReadAll(r.Body)
if err != nil {
log.Errorw("could not read buffer", zap.Error(err))
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rdr2 := io.NopCloser(bytes.NewBuffer(buf))
r.Body = rdr2
defer r.Body.Close()
var msg string
var parseMethod string
var raw []byte
switch ct {
case "application/json":
parseMethod = "json"
raw = buf
msg = parse.BufferToMessage(buf)
case "plain/text":
parseMethod = "plaintext"
raw = buf
msg = string(buf)
case "application/x-www-form-urlencoded":
parseMethod = "urlencoded"
if err := r.ParseForm(); err != nil {
log.Errorw("could not parse form", "body", string(buf), zap.Error(err))
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
values := r.Form
str, err := json.Marshal(values)
if err != nil {
log.Errorw("could not marshal values", "body", string(buf), "values", values, zap.Error(err))
http.Error(w, err.Error(), http.StatusInternalServerError)
}
raw = str
msg = parse.BufferToMessage(str)
case "":
http.Error(w, "empty content type recieved", http.StatusBadRequest)
return
default:
parseMethod = "form"
if err := r.ParseMultipartForm(int64(20 * units.Megabyte)); err != nil {
log.Errorw("could not parse multipart form", "body", string(buf), zap.Error(err))
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
parts := strings.Split(ct, ";")
log.Debugw("parsing form", "parts", parts)
if len(parts) >= 1 && parts[0] == "multipart/form-data" {
val := r.FormValue("payload")
log.Debugw("attempting form parse", "payload", val)
raw = []byte(val)
msg = parse.BufferToMessage([]byte(val))
}
}
data := map[string]any{
"content-type": ct,
"raw": string(raw),
"parse-method": parseMethod,
}
jsonData, err := json.Marshal(data)
if err != nil {
log.Errorw("could not marshal data", "data", data, zap.Error(err))
}
m := &model.Message{
Content: msg,
Message: jsonData,
Published: false,
}
if err := m.Create(ctx, db); err != nil {
log.Errorw("could not save message", "parse_method", parseMethod, zap.Error(err))
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write([]byte(""))
}
}
func cronHandler(dg *discordgo.Session, db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if dg == nil {
log.Errorw("discord session is nil")
w.Write([]byte(""))
return
}
dbo, err := model.OpenDatabase(db)
if err != nil {
log.Errorw("could not open database", zap.Error(err))
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var messages []model.Message
dbo.WithContext(ctx).Where("published = false").Find(&messages)
for _, msg := range messages {
if err := msg.Send(ctx, dg, db); err != nil {
log.Errorw("could not send message", zap.Error(err))
http.Error(w, err.Error(), 500)
return
}
}
}
}