Skip to content

Commit

Permalink
Checkpoing commit.
Browse files Browse the repository at this point in the history
Evil commit. Things are kinda working.
  • Loading branch information
tobert committed Oct 29, 2013
1 parent fdf8d30 commit a8fa4f0
Show file tree
Hide file tree
Showing 26 changed files with 19,595 additions and 135 deletions.
2 changes: 2 additions & 0 deletions dump.cql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
use skeezy;
select * from posts;
5 changes: 4 additions & 1 deletion insert.cql
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!');

92 changes: 92 additions & 0 deletions main.go
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
}
Loading

0 comments on commit a8fa4f0

Please sign in to comment.