Skip to content

Commit

Permalink
use local graph struct with etags
Browse files Browse the repository at this point in the history
  • Loading branch information
deiu committed May 25, 2017
1 parent 62b5c82 commit 5ecb3d4
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 17 deletions.
6 changes: 3 additions & 3 deletions GET.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (c *Context) getRDF(w web.ResponseWriter, req *web.Request, mime string) {
w.Write([]byte(err.Error()))
return
}
// TODO replace with something better for ETag generation
w.Header().Add("ETag", newETag([]byte(graph.String())))
graph.Serialize(w, mime)
w.Header().Add("ETag", graph.Etag)
w.Header().Set("Content-Type", mime)
graph.Graph.Serialize(w, mime)
}
12 changes: 9 additions & 3 deletions GET_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ import (

func Test_GET_RDF(t *testing.T) {
mime := "text/turtle"
URI := testServer.URL + "/rdf"
URI := testServer.URL + "/foo.ttl"
graph := rdf.NewGraph(URI)
graph.AddTriple(rdf.NewResource(URI), rdf.NewResource("pred"), rdf.NewLiteral("obj"))

graph.AddTriple(rdf.NewResource(URI), rdf.NewResource("http://test.com/foo"), rdf.NewLiteral("obj"))
buf := new(bytes.Buffer)
graph.Serialize(buf, mime)

Expand All @@ -34,6 +33,13 @@ func Test_GET_RDF(t *testing.T) {
res, err = testClient.Do(req)
assert.NoError(t, err)
assert.Equal(t, 200, res.StatusCode)
assert.Equal(t, mime, res.Header.Get("Content-Type"))

graph = rdf.NewGraph(URI)
graph.Parse(res.Body, res.Header.Get("Content-Type"))
res.Body.Close()
assert.Equal(t, 1, graph.Len())
assert.NotNil(t, graph.One(rdf.NewResource(URI), rdf.NewResource("http://test.com/foo"), rdf.NewLiteral("obj")))
}

func Test_GET_Static(t *testing.T) {
Expand Down
14 changes: 10 additions & 4 deletions POST.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ func (c *Context) PostHandler(w web.ResponseWriter, req *web.Request) {

func (c *Context) postRDF(w web.ResponseWriter, req *web.Request) {
URI := absoluteURI(req.Request)
graph := rdf.NewGraph(URI)
graph.Parse(req.Body, req.Header.Get("Content-Type"))
if graph.Len() == 0 {
g := rdf.NewGraph(URI)
g.Parse(req.Body, req.Header.Get("Content-Type"))
if g.Len() == 0 {
w.WriteHeader(400)
w.Write([]byte("Empty request body"))
return
Expand All @@ -30,10 +30,16 @@ func (c *Context) postRDF(w web.ResponseWriter, req *web.Request) {
w.Write([]byte("Cannot create new graph if it aready exists"))
return
}

// add graph
// TODO: move this into a go routine
graph := NewGraph()
graph.Graph = g
graph.Etag = newETag([]byte(g.String()))
c.addGraph(URI, graph)

// add ETag
w.Header().Add("ETag", newETag([]byte(graph.String())))
w.Header().Add("ETag", graph.Etag)

w.WriteHeader(201)
}
13 changes: 11 additions & 2 deletions rdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ var rdfMimes = []string{
"application/ld+json",
}

type Graph struct {
*rdf.Graph
Etag string
}

func NewGraph() *Graph {
return &Graph{}
}

func canParse(ctype string) bool {
if len(mimeParser[ctype]) > 0 {
return true
Expand All @@ -35,11 +44,11 @@ func canSerialize(ctype string) bool {
return false
}

func (c *Context) addGraph(URI string, graph *rdf.Graph) {
func (c *Context) addGraph(URI string, graph *Graph) {
c.Store[URI] = graph
}

func (c *Context) getGraph(URI string) (*rdf.Graph, error) {
func (c *Context) getGraph(URI string) (*Graph, error) {
if c.Store[URI] == nil {
return nil, errors.New("Cannot find graph that matches URI: " + URI)
}
Expand Down
6 changes: 4 additions & 2 deletions rdf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ func Test_RDF_AddRemoveGraph(t *testing.T) {
var err error
c := NewContext()
URI := "https://example.org"
graph := rdf.NewGraph(URI)
g := rdf.NewGraph(URI)
graph := NewGraph()
graph.Graph = g
c.addGraph(URI, graph)
graph, err = c.getGraph(URI)
assert.NoError(t, err)
assert.Equal(t, URI, graph.URI())
assert.Equal(t, URI, graph.Graph.URI())
err = c.delGraph(URI)
assert.NoError(t, err)
graph, err = c.getGraph(URI)
Expand Down
10 changes: 7 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"path"

"github.com/boltdb/bolt"
rdf "github.com/deiu/rdf2go"
"github.com/gocraft/web"
"github.com/rs/zerolog"
)
Expand All @@ -26,15 +25,20 @@ var (
type (
Context struct {
Config *Config
Store map[string]*rdf.Graph
Store map[string]*Graph
BoltDB *bolt.DB
}

APIContext struct {
*Context
AccessToken string
}
)

func NewContext() *Context {
return &Context{
Config: NewConfig(),
Store: make(map[string]*rdf.Graph),
Store: make(map[string]*Graph),
BoltDB: &bolt.DB{},
}
}
Expand Down

0 comments on commit 5ecb3d4

Please sign in to comment.