-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
70 lines (59 loc) · 1.48 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
package main
import (
"encoding/json"
"net/http"
"strconv"
"time"
"github.com/beme/abide/example/models"
)
func firstHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]string{
"foo": "bar",
}
body, err := json.Marshal(data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Etag", strconv.FormatInt(time.Now().UnixNano(), 10))
w.Write(body)
}
func secondHandler(w http.ResponseWriter, r *http.Request) {
post := &models.Post{
Title: "Hello World",
Body: "Foo Bar",
}
data := map[string]interface{}{
"post": post,
"stats": map[string]interface{}{
"updated_at": time.Now().Unix(),
"things": []map[string]interface{}{
{
"updated_at": time.Now().Unix(),
},
},
},
}
body, err := json.Marshal(data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Etag", strconv.FormatInt(time.Now().UnixNano(), 10))
w.Write(body)
}
func thirdHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
func fourthHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`Hello World.`))
}
func main() {
http.HandleFunc("/first", firstHandler)
http.HandleFunc("/second", secondHandler)
http.HandleFunc("/third", thirdHandler)
http.HandleFunc("/fourth", fourthHandler)
http.ListenAndServe(":8080", nil)
}