-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
131 lines (113 loc) · 3.29 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"./src/skeezy"
"encoding/json"
"fmt"
"github.com/gocql/gocql"
"net/http"
)
func main() {
// connect to Cassandra
cluster := gocql.NewCluster("127.0.0.1")
cluster.Keyspace = "skeezy"
cluster.Consistency = gocql.Quorum
cass, err := cluster.CreateSession()
if err != nil {
panic(fmt.Sprintf("Error creating Cassandra session: %v", err))
}
defer cass.Close()
// serve up static content
http.Handle("/js/", http.StripPrefix("/js/", http.FileServer(http.Dir("./public/js/"))))
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("./public/css/"))))
http.Handle("/img/", http.StripPrefix("/img/", http.FileServer(http.Dir("./public/img/"))))
http.Handle("/fonts/", http.StripPrefix("/fonts/", http.FileServer(http.Dir("./public/fonts/"))))
// front page
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./public/index.html")
})
http.HandleFunc("/posts/", func(w http.ResponseWriter, r *http.Request) {
posts := skeezy.ListPosts(cass)
js, err := json.Marshal(posts)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(js)
})
// deal with single posts, action depends on HTTP method
http.HandleFunc("/post/", func(w http.ResponseWriter, r *http.Request) {
id := r.URL.Path[len("/post/"):]
switch r.Method {
case "GET":
skeezy.GetPost(cass, id, w, r)
case "PUT":
skeezy.NewPost(cass, id, w, r)
case "DELETE":
skeezy.DelPost(cass, id, w, r)
default:
fmt.Fprintf(w, "Invalid method: '%s'\n", r.Method)
}
})
// user management routes
http.HandleFunc("/users/", func(w http.ResponseWriter, r *http.Request) {
users := skeezy.ListUsers(cass)
sep := []byte{'['}
for _, user := range users {
w.Write(sep)
sep = []byte{',', '\n'}
js, _ := json.Marshal(user)
w.Write(js)
}
w.Write([]byte{']', '\n'})
})
http.HandleFunc("/user/", func(w http.ResponseWriter, r *http.Request) {
id := r.URL.Path[len("/users/"):]
switch r.Method {
case "GET":
skeezy.GetUser(cass, id, w, r)
case "PUT":
skeezy.NewUser(cass, id, w, r)
case "POST":
skeezy.UpdateUser(cass, id, w, r)
default:
fmt.Fprintf(w, "Invalid method: '%s'\n", r.Method)
}
})
// a list of comment ids
http.HandleFunc("/comments/", func(w http.ResponseWriter, r *http.Request) {
cc := skeezy.ListComments(cass, getId(r, "/comments/"))
// return a JSON list, avoid making extra copies of the data in memory
sep := []byte{'['}
for comment := range cc {
w.Write(sep)
sep = []byte{',', '\n'}
js, _ := json.Marshal(comment)
w.Write(js)
}
w.Write([]byte{']', '\n'})
})
// deal with single comments, action depends on HTTP method
http.HandleFunc("/comment/", func(w http.ResponseWriter, r *http.Request) {
id := r.URL.Path[len("/comment/"):]
switch r.Method {
case "GET":
skeezy.GetComment(cass, id, w, r)
case "PUT":
skeezy.NewComment(cass, id, w, r)
case "DELETE":
skeezy.DelComment(cass, id, w, r)
default:
fmt.Fprintf(w, "Invalid method: '%s'\n", r.Method)
}
})
// start the show
http.ListenAndServe(":8080", nil)
}
func getId(r *http.Request, prefix string) gocql.UUID {
idarg := r.URL.Path[len(prefix):]
id, err := gocql.ParseUUID(idarg)
if err != nil {
fmt.Printf("Invalid ID: '%s'\n", idarg)
}
return id
}