Remove non-documentation lint
Because of extensive nature of changes, tested all three non-memstore backends - passed.
This commit is contained in:
parent
6614466d23
commit
484bf145a8
35 changed files with 277 additions and 284 deletions
19
http/http.go
19
http/http.go
|
|
@ -47,9 +47,8 @@ func findAssetsPath() string {
|
|||
if *assetsPath != "" {
|
||||
if hasAssets(*assetsPath) {
|
||||
return *assetsPath
|
||||
} else {
|
||||
glog.Fatalln("Cannot find assets at", *assetsPath, ".")
|
||||
}
|
||||
glog.Fatalln("Cannot find assets at", *assetsPath, ".")
|
||||
}
|
||||
|
||||
if hasAssets(".") {
|
||||
|
|
@ -61,7 +60,7 @@ func findAssetsPath() string {
|
|||
return gopathPath
|
||||
}
|
||||
glog.Fatalln("Cannot find assets in any of the default search paths. Please run in the same directory, in a Go workspace, or set --assets .")
|
||||
return ""
|
||||
panic("cannot reach")
|
||||
}
|
||||
|
||||
func LogRequest(handler ResponseHandler) httprouter.Handle {
|
||||
|
|
@ -81,11 +80,7 @@ func LogRequest(handler ResponseHandler) httprouter.Handle {
|
|||
}
|
||||
}
|
||||
|
||||
func FormatJson400(w http.ResponseWriter, err interface{}) int {
|
||||
return FormatJsonError(w, 400, err)
|
||||
}
|
||||
|
||||
func FormatJsonError(w http.ResponseWriter, code int, err interface{}) int {
|
||||
func jsonResponse(w http.ResponseWriter, code int, err interface{}) int {
|
||||
http.Error(w, fmt.Sprintf("{\"error\" : \"%s\"}", err), code)
|
||||
return code
|
||||
}
|
||||
|
|
@ -105,12 +100,12 @@ func (h *TemplateRequestHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
|
|||
}
|
||||
}
|
||||
|
||||
type Api struct {
|
||||
type API struct {
|
||||
config *config.Config
|
||||
handle *graph.Handle
|
||||
}
|
||||
|
||||
func (api *Api) ApiV1(r *httprouter.Router) {
|
||||
func (api *API) APIv1(r *httprouter.Router) {
|
||||
r.POST("/api/v1/query/:query_lang", LogRequest(api.ServeV1Query))
|
||||
r.POST("/api/v1/shape/:query_lang", LogRequest(api.ServeV1Shape))
|
||||
r.POST("/api/v1/write", LogRequest(api.ServeV1Write))
|
||||
|
|
@ -129,8 +124,8 @@ func SetupRoutes(handle *graph.Handle, cfg *config.Config) {
|
|||
templates.ParseGlob(fmt.Sprint(assets, "/templates/*.html"))
|
||||
root := &TemplateRequestHandler{templates: templates}
|
||||
docs := &DocRequestHandler{assets: assets}
|
||||
api := &Api{config: cfg, handle: handle}
|
||||
api.ApiV1(r)
|
||||
api := &API{config: cfg, handle: handle}
|
||||
api.APIv1(r)
|
||||
|
||||
//m.Use(martini.Static("static", martini.StaticOptions{Prefix: "/static", SkipLogging: true}))
|
||||
//r.Handler("GET", "/static", http.StripPrefix("/static", http.FileServer(http.Dir("static/"))))
|
||||
|
|
|
|||
|
|
@ -56,13 +56,13 @@ var parseTests = []struct {
|
|||
{"subject": "foo", "predicate": "bar"}
|
||||
]`,
|
||||
expect: nil,
|
||||
err: fmt.Errorf("Invalid quad at index %d. %v", 0, quad.Quad{"foo", "bar", "", ""}),
|
||||
err: fmt.Errorf("invalid quad at index %d. %v", 0, quad.Quad{"foo", "bar", "", ""}),
|
||||
},
|
||||
}
|
||||
|
||||
func TestParseJSON(t *testing.T) {
|
||||
for _, test := range parseTests {
|
||||
got, err := ParseJsonToQuadList([]byte(test.input))
|
||||
got, err := ParseJSONToQuadList([]byte(test.input))
|
||||
if fmt.Sprint(err) != fmt.Sprint(test.err) {
|
||||
t.Errorf("Failed to %v with unexpected error, got:%v expected %v", test.message, err, test.err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,18 +47,18 @@ func WrapResult(result interface{}) ([]byte, error) {
|
|||
return json.MarshalIndent(wrap, "", " ")
|
||||
}
|
||||
|
||||
func RunJsonQuery(query string, ses query.HttpSession) (interface{}, error) {
|
||||
func Run(q string, ses query.HTTP) (interface{}, error) {
|
||||
c := make(chan interface{}, 5)
|
||||
go ses.ExecInput(query, c, 100)
|
||||
go ses.ExecInput(q, c, 100)
|
||||
for res := range c {
|
||||
ses.BuildJson(res)
|
||||
ses.BuildJSON(res)
|
||||
}
|
||||
return ses.GetJson()
|
||||
return ses.GetJSON()
|
||||
}
|
||||
|
||||
func GetQueryShape(query string, ses query.HttpSession) ([]byte, error) {
|
||||
func GetQueryShape(q string, ses query.HTTP) ([]byte, error) {
|
||||
c := make(chan map[string]interface{}, 5)
|
||||
go ses.GetQuery(query, c)
|
||||
go ses.GetQuery(q, c)
|
||||
var data map[string]interface{}
|
||||
for res := range c {
|
||||
data = res
|
||||
|
|
@ -67,19 +67,19 @@ func GetQueryShape(query string, ses query.HttpSession) ([]byte, error) {
|
|||
}
|
||||
|
||||
// TODO(barakmich): Turn this into proper middleware.
|
||||
func (api *Api) ServeV1Query(w http.ResponseWriter, r *http.Request, params httprouter.Params) int {
|
||||
var ses query.HttpSession
|
||||
func (api *API) ServeV1Query(w http.ResponseWriter, r *http.Request, params httprouter.Params) int {
|
||||
var ses query.HTTP
|
||||
switch params.ByName("query_lang") {
|
||||
case "gremlin":
|
||||
ses = gremlin.NewSession(api.handle.QuadStore, api.config.Timeout, false)
|
||||
case "mql":
|
||||
ses = mql.NewSession(api.handle.QuadStore)
|
||||
default:
|
||||
return FormatJson400(w, "Need a query language.")
|
||||
return jsonResponse(w, 400, "Need a query language.")
|
||||
}
|
||||
bodyBytes, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
return FormatJson400(w, err)
|
||||
return jsonResponse(w, 400, err)
|
||||
}
|
||||
code := string(bodyBytes)
|
||||
result, err := ses.InputParses(code)
|
||||
|
|
@ -88,7 +88,7 @@ func (api *Api) ServeV1Query(w http.ResponseWriter, r *http.Request, params http
|
|||
var output interface{}
|
||||
var bytes []byte
|
||||
var err error
|
||||
output, err = RunJsonQuery(code, ses)
|
||||
output, err = Run(code, ses)
|
||||
if err != nil {
|
||||
bytes, err = WrapErrResult(err)
|
||||
http.Error(w, string(bytes), 400)
|
||||
|
|
@ -98,33 +98,33 @@ func (api *Api) ServeV1Query(w http.ResponseWriter, r *http.Request, params http
|
|||
bytes, err = WrapResult(output)
|
||||
if err != nil {
|
||||
ses = nil
|
||||
return FormatJson400(w, err)
|
||||
return jsonResponse(w, 400, err)
|
||||
}
|
||||
fmt.Fprint(w, string(bytes))
|
||||
ses = nil
|
||||
return 200
|
||||
case query.ParseFail:
|
||||
ses = nil
|
||||
return FormatJson400(w, err)
|
||||
return jsonResponse(w, 400, err)
|
||||
default:
|
||||
ses = nil
|
||||
return FormatJsonError(w, 500, "Incomplete data?")
|
||||
return jsonResponse(w, 500, "Incomplete data?")
|
||||
}
|
||||
}
|
||||
|
||||
func (api *Api) ServeV1Shape(w http.ResponseWriter, r *http.Request, params httprouter.Params) int {
|
||||
var ses query.HttpSession
|
||||
func (api *API) ServeV1Shape(w http.ResponseWriter, r *http.Request, params httprouter.Params) int {
|
||||
var ses query.HTTP
|
||||
switch params.ByName("query_lang") {
|
||||
case "gremlin":
|
||||
ses = gremlin.NewSession(api.handle.QuadStore, api.config.Timeout, false)
|
||||
case "mql":
|
||||
ses = mql.NewSession(api.handle.QuadStore)
|
||||
default:
|
||||
return FormatJson400(w, "Need a query language.")
|
||||
return jsonResponse(w, 400, "Need a query language.")
|
||||
}
|
||||
bodyBytes, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
return FormatJson400(w, err)
|
||||
return jsonResponse(w, 400, err)
|
||||
}
|
||||
code := string(bodyBytes)
|
||||
result, err := ses.InputParses(code)
|
||||
|
|
@ -134,13 +134,13 @@ func (api *Api) ServeV1Shape(w http.ResponseWriter, r *http.Request, params http
|
|||
var err error
|
||||
output, err = GetQueryShape(code, ses)
|
||||
if err != nil {
|
||||
return FormatJson400(w, err)
|
||||
return jsonResponse(w, 400, err)
|
||||
}
|
||||
fmt.Fprint(w, string(output))
|
||||
return 200
|
||||
case query.ParseFail:
|
||||
return FormatJson400(w, err)
|
||||
return jsonResponse(w, 400, err)
|
||||
default:
|
||||
return FormatJsonError(w, 500, "Incomplete data?")
|
||||
return jsonResponse(w, 500, "Incomplete data?")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ import (
|
|||
"github.com/google/cayley/quad/cquads"
|
||||
)
|
||||
|
||||
func ParseJsonToQuadList(jsonBody []byte) ([]quad.Quad, error) {
|
||||
func ParseJSONToQuadList(jsonBody []byte) ([]quad.Quad, error) {
|
||||
var quads []quad.Quad
|
||||
err := json.Unmarshal(jsonBody, &quads)
|
||||
if err != nil {
|
||||
|
|
@ -37,38 +37,38 @@ func ParseJsonToQuadList(jsonBody []byte) ([]quad.Quad, error) {
|
|||
}
|
||||
for i, q := range quads {
|
||||
if !q.IsValid() {
|
||||
return nil, fmt.Errorf("Invalid quad at index %d. %s", i, q)
|
||||
return nil, fmt.Errorf("invalid quad at index %d. %s", i, q)
|
||||
}
|
||||
}
|
||||
return quads, nil
|
||||
}
|
||||
|
||||
func (api *Api) ServeV1Write(w http.ResponseWriter, r *http.Request, _ httprouter.Params) int {
|
||||
func (api *API) ServeV1Write(w http.ResponseWriter, r *http.Request, _ httprouter.Params) int {
|
||||
if api.config.ReadOnly {
|
||||
return FormatJson400(w, "Database is read-only.")
|
||||
return jsonResponse(w, 400, "Database is read-only.")
|
||||
}
|
||||
bodyBytes, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
return FormatJson400(w, err)
|
||||
return jsonResponse(w, 400, err)
|
||||
}
|
||||
quads, err := ParseJsonToQuadList(bodyBytes)
|
||||
quads, err := ParseJSONToQuadList(bodyBytes)
|
||||
if err != nil {
|
||||
return FormatJson400(w, err)
|
||||
return jsonResponse(w, 400, err)
|
||||
}
|
||||
api.handle.QuadWriter.AddQuadSet(quads)
|
||||
fmt.Fprintf(w, "{\"result\": \"Successfully wrote %d quads.\"}", len(quads))
|
||||
return 200
|
||||
}
|
||||
|
||||
func (api *Api) ServeV1WriteNQuad(w http.ResponseWriter, r *http.Request, params httprouter.Params) int {
|
||||
func (api *API) ServeV1WriteNQuad(w http.ResponseWriter, r *http.Request, params httprouter.Params) int {
|
||||
if api.config.ReadOnly {
|
||||
return FormatJson400(w, "Database is read-only.")
|
||||
return jsonResponse(w, 400, "Database is read-only.")
|
||||
}
|
||||
|
||||
formFile, _, err := r.FormFile("NQuadFile")
|
||||
if err != nil {
|
||||
glog.Errorln(err)
|
||||
return FormatJsonError(w, 500, "Couldn't read file: "+err.Error())
|
||||
return jsonResponse(w, 500, "Couldn't read file: "+err.Error())
|
||||
}
|
||||
|
||||
defer formFile.Close()
|
||||
|
|
@ -108,17 +108,17 @@ func (api *Api) ServeV1WriteNQuad(w http.ResponseWriter, r *http.Request, params
|
|||
return 200
|
||||
}
|
||||
|
||||
func (api *Api) ServeV1Delete(w http.ResponseWriter, r *http.Request, params httprouter.Params) int {
|
||||
func (api *API) ServeV1Delete(w http.ResponseWriter, r *http.Request, params httprouter.Params) int {
|
||||
if api.config.ReadOnly {
|
||||
return FormatJson400(w, "Database is read-only.")
|
||||
return jsonResponse(w, 400, "Database is read-only.")
|
||||
}
|
||||
bodyBytes, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
return FormatJson400(w, err)
|
||||
return jsonResponse(w, 400, err)
|
||||
}
|
||||
quads, err := ParseJsonToQuadList(bodyBytes)
|
||||
quads, err := ParseJSONToQuadList(bodyBytes)
|
||||
if err != nil {
|
||||
return FormatJson400(w, err)
|
||||
return jsonResponse(w, 400, err)
|
||||
}
|
||||
count := 0
|
||||
for _, q := range quads {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue