-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
73 lines (55 loc) · 1.61 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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
// UNCOMMENT the line below
// log "github.com/sirupsen/logrus"
)
var results []string
func main() {
// use PORT environment variable, or default to 7888
port := "7888"
if fromEnv := os.Getenv("PORT"); fromEnv != "" {
port = fromEnv
}
// register hello function to handle all requests
server := http.NewServeMux()
server.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Serving request: %s\n", r.URL.Path)
// UNCOMMENT the line below
// log.Printf("Serving request: %s", r.URL.Path)
fmt.Fprintf(w, "Hello")
})
server.HandleFunc("/ping", hello)
server.HandleFunc("/event", event)
// start the web server on port and accept requests
fmt.Printf("Server listening on port %s\n", port)
// UNCOMMENT the 3 lines below
// log.Printf("Server listening on port %s", port)
// err := http.ListenAndServe(":"+port, server)
// log.Fatal(err)
// COMMENT out the line below
_ = http.ListenAndServe(":"+port, server)
}
func event(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "Error reading request body", http.StatusInternalServerError)
}
results = append(results, string(body))
fmt.Fprint(w, "POST done")
} else if r.Method == "GET" {
fmt.Fprintf(w, "OK")
}
return
}
// ping responds to the request with a plain-text "Ok" message.
func hello(w http.ResponseWriter, r *http.Request) {
fmt.Print("Serving request: %s\n", r.URL.Path)
// UNCOMMENT the line below
// log.Printf("Serving request: %s", r.URL.Path)
fmt.Fprintf(w, "Ok")
}