-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
64 lines (49 loc) · 1.26 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
package main
import (
"fmt"
"log"
"net/http"
"strconv"
"github.com/PratikMahajan/Twitter-Knative-Serverless-Video-Download/config"
ce "github.com/cloudevents/sdk-go"
)
func main() {
port, err := strconv.Atoi(config.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())
}
// wire handler for CE
t.SetReceiver(&eventReceiver{})
// Health Handler
mux.HandleFunc("/health", func(w http.ResponseWriter, _ *http.Request) {
_, err = fmt.Fprint(w, "ok")
if err != nil{
log.Printf("failed to write /health : %s", err.Error())
}
})
// 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
}
_, err := fmt.Fprint(w, "Nothing to see here. Use POST to send CloudEvents")
if err != nil{
log.Panicf("failed to write / : %s", err.Error())
}
})
a := fmt.Sprintf(":%d", port)
log.Fatal(http.ListenAndServe(a, mux))
}