-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallback_server.go
63 lines (52 loc) · 1.18 KB
/
callback_server.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 main
import (
"errors"
"log"
"net/http"
"strings"
"time"
"github.com/gorilla/schema"
)
var decoder = schema.NewDecoder()
func spawnCbkServer(addr string) (*http.Server, error) {
mux := http.NewServeMux()
mux.HandleFunc("/", handleCallback)
srv := &http.Server{
Addr: addr,
Handler: mux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 30 * time.Second,
MaxHeaderBytes: 1 << 13,
}
go func() {
if err := srv.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("srv.ListenAndServe() failed: %v", err)
}
}()
return srv, nil
}
func handleCallback(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
log.Printf("r.ParseForm() failed: %v", err)
return
}
var resp FrontendResponse
_ = decoder.Decode(&resp, r.PostForm)
if resp.MessageUUID == "" || resp.MessageState == "" {
return
}
switch resp.MessageState {
case "sent", "failed", "delivered", "undelivered":
l := strings.Split(r.URL.Path, "/")
reqID := l[len(l)-1]
if entry, ok := store.Get(reqID); ok {
select {
case entry.CbkCh <- resp:
default:
}
}
case "queued":
default:
}
}