-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Evil commit. Things are kinda working.
- Loading branch information
Showing
26 changed files
with
19,595 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
use skeezy; | ||
select * from posts; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
insert into posts (id, authors, body, dttm, tags) values (ea4aba7d-9344-4d08-8ca5-873aa1214068, ['al'], 'first post!', 'now', []); | ||
use skeezy; | ||
insert into posts (id, authors, body, created, tags) values (ea4aba7d-9344-4d08-8ca5-873aa1214068, ['al'], 'first post!', 'now', []); | ||
insert into comments (id, postId, parentId, created, author, email, body) values (5ee89e7c-a725-4cfd-9531-7c73b606b51d, ea4aba7d-9344-4d08-8ca5-873aa1214068, NULL, 'now', 'al', '[email protected]', 'first post first post!'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package main | ||
|
||
import ( | ||
"./src/skeezy" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"tux21b.org/v1/gocql" | ||
"tux21b.org/v1/gocql/uuid" | ||
) | ||
|
||
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") | ||
}) | ||
|
||
// a list of post ids | ||
http.HandleFunc("/posts/", func(w http.ResponseWriter, r *http.Request) { | ||
skeezy.ListPosts(cass, w, r) | ||
}) | ||
|
||
// 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) | ||
} | ||
}) | ||
|
||
// a list of comment ids | ||
http.HandleFunc("/comments/", func(w http.ResponseWriter, r *http.Request) { | ||
cc := skeezy.ListComments(cass, getId(r, "/comments/")) | ||
for comment := range cc { | ||
js, _ := json.Marshal(comment) | ||
w.Write(js) | ||
} | ||
}) | ||
|
||
// 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) uuid.UUID { | ||
idarg := r.URL.Path[len(prefix):] | ||
id, err := uuid.ParseUUID(idarg) | ||
if err != nil { | ||
fmt.Printf("Invalid ID: '%s'\n", idarg) | ||
} | ||
return id | ||
} |
Oops, something went wrong.