/
main.go
60 lines (48 loc) · 1.17 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
package main
import (
"context"
"fmt"
"log"
"net/http"
"strconv"
ce "github.com/cloudevents/sdk-go"
ev "github.com/mchmarny/gcputil/env"
)
func main() {
port, err := strconv.Atoi(ev.MustGetEnvVar("PORT", "8080"))
if err != nil {
log.Fatalf("failed to parse port, %s", err.Error())
}
// Handler Mux
mux := http.NewServeMux()
// Ingres API Handler
t, err := ce.NewHTTPTransport(
ce.WithMethod("POST"),
ce.WithPath("/"),
ce.WithPort(port),
)
if err != nil {
log.Fatalf("failed to create CloudEvents transport, %s", err.Error())
}
er, err := newEventReceiver(context.Background())
if err != nil {
log.Fatalf("failed to create event receiver, %s", err.Error())
}
// wire handler for CE
t.SetReceiver(er)
// Health Handler
mux.HandleFunc("/health", func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprint(w, "ok")
})
// Events or UI Handlers
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Printf("Method, %s", r.Method)
if r.Method == "POST" {
t.ServeHTTP(w, r)
return
}
fmt.Fprint(w, "Nothing to see here. Use POST to send CloudEvents")
})
a := fmt.Sprintf(":%d", port)
log.Fatal(http.ListenAndServe(a, mux))
}