-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
110 lines (89 loc) · 2.97 KB
/
main.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
/**
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/google/go-github/v39/github"
"github.com/joho/godotenv"
"github.com/julienschmidt/httprouter"
"github.com/kelseyhightower/envconfig"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
type appConfig struct {
AppSecret string `required:"true" split_words:"true"`
Host string
Port int `default:"5000"`
Token string `required:"true"`
}
var (
receivedUpdatesMutex sync.RWMutex
receivedUpdates = make([]map[string]interface{}, 0)
config appConfig
)
func main() {
environment := strings.ToLower(os.Getenv("ENVIRONMENT"))
if strings.EqualFold(environment, "development") {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339})
}
godotenv.Load(".env", ".env."+environment)
if err := envconfig.Process("", &config); err != nil {
log.Fatal().Err(err).Msg("Error loading configuration")
}
router := httprouter.New()
router.HandlerFunc(http.MethodGet, "/", handleGetIndex)
router.HandlerFunc(http.MethodGet, "/facebook", handleGetWebHook)
router.HandlerFunc(http.MethodPost, "/facebook", handlePostWebHook)
addr := fmt.Sprintf("%s:%d", config.Host, config.Port)
log.Info().Str("address", addr).Msg("Starting HTTP server")
if err := http.ListenAndServe(addr, router); err != nil && err != http.ErrServerClosed {
log.Fatal().Str("address", addr).Err(err).Msg("Error starting HTTP server")
}
}
func handleGetIndex(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
receivedUpdatesMutex.RLock()
defer receivedUpdatesMutex.RUnlock()
json.NewEncoder(w).Encode(receivedUpdates)
}
func handleGetWebHook(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
if query.Get("hub.mode") != "subscribe" || query.Get("hub.verify_token") != config.Token {
log.Warn().Msg("Invalid subscribe token")
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
w.Write([]byte(query.Get("hub.challenge")))
}
func handlePostWebHook(w http.ResponseWriter, r *http.Request) {
payload, err := github.ValidatePayload(r, []byte(config.AppSecret))
if err != nil {
log.Warn().Err(err).Msg("Invalid payload signature")
return
}
var update map[string]interface{}
err = json.Unmarshal(payload, &update)
if err != nil {
log.Warn().Err(err).Msg("Error decoding payload")
if serr, ok := err.(*json.SyntaxError); ok {
http.Error(w, serr.Error(), http.StatusBadRequest)
return
}
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
// Process the Facebook updates here
receivedUpdatesMutex.Lock()
defer receivedUpdatesMutex.Unlock()
receivedUpdates = append(receivedUpdates, nil)
copy(receivedUpdates[1:], receivedUpdates)
receivedUpdates[0] = update
}