-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
51 lines (42 loc) · 1.01 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
package main
import (
"bytes"
"fmt"
"log"
"net/http"
)
type Event struct {
Key string
Value string
}
var buf bytes.Buffer
func Handler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodOptions {
// Respond with allowed methods information
w.Header().Set("Allow", http.MethodPost)
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Methods allowed: POST\n")
return
} else if r.Method == http.MethodPost {
var event Event
if err := parseRequest(r.Body, &event); err != nil {
log.Print(err)
http.Error(w, "Error parsing request", http.StatusBadRequest)
return
}
if err := saveEventToBuffer(event); err != nil {
log.Print(err)
http.Error(w, "Error saving event to buffer", http.StatusInternalServerError)
return
}
} else {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
}
func main() {
http.HandleFunc("/", Handler)
// start the http server
log.Println("Localhost running at port 8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}