https://github.com/google/cayley
Raw File
Tip revision: 4d1b871fb152e08f77fa9d37f1a7701091b60dc3 authored by Luis Fung on 11 February 2019, 08:03:44 UTC
Add Badger as a kv store
Tip revision: 4d1b871
common.go
package cayleyhttp

import (
	"encoding/json"
	"fmt"
	"net/http"

	"github.com/cayleygraph/cayley/graph"
	"github.com/cayleygraph/cayley/graph/http"
)

func jsonResponse(w http.ResponseWriter, code int, err interface{}) {
	w.Header().Set("Content-Type", contentTypeJSON)
	w.WriteHeader(code)
	w.Write([]byte(`{"error": `))
	var s string
	switch err := err.(type) {
	case string:
		s = err
	case error:
		s = err.Error()
	default:
		s = fmt.Sprint(err)
	}
	data, _ := json.Marshal(s)
	w.Write(data)
	w.Write([]byte(`}`))
}

func HandleForRequest(h *graph.Handle, wtyp string, wopt graph.Options, r *http.Request) (*graph.Handle, error) {
	g, ok := h.QuadStore.(httpgraph.QuadStore)
	if !ok {
		return h, nil
	}
	qs, err := g.ForRequest(r)
	if err != nil {
		return nil, err
	}
	qw, err := graph.NewQuadWriter(wtyp, qs, wopt)
	if err != nil {
		qs.Close()
		return nil, err
	}
	return &graph.Handle{QuadStore: qs, QuadWriter: qw}, nil
}
back to top