-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathmain.go
More file actions
51 lines (44 loc) · 1.36 KB
/
main.go
File metadata and controls
51 lines (44 loc) · 1.36 KB
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
package main
import (
"encoding/json"
"log"
"net/http"
)
type webhookPayload struct {
Action string `json:"action"`
IP string `json:"ip"`
BearerToken string `json:"bearerToken"`
QueryParams map[string]string `json:"queryParams"`
UserAgent string `json:"userAgent"`
}
type webhookResponse struct {
StreamKey string `json:"streamKey"`
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Only POST method is accepted", http.StatusMethodNotAllowed)
return
}
var payload webhookPayload
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
if payload.BearerToken == "broadcastBoxRulez" {
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(webhookResponse{StreamKey: payload.BearerToken}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
} else {
w.WriteHeader(http.StatusForbidden)
if err := json.NewEncoder(w).Encode(webhookResponse{}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
})
log.Println("Server listening on port 8081")
if err := http.ListenAndServe("127.0.0.1:8081", nil); err != nil {
log.Fatalf("Could not start server: %s\n", err)
}
}