-
Notifications
You must be signed in to change notification settings - Fork 28
Query endpoint #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,176 @@ | ||
# Rest API | ||
|
||
## POST /query | ||
|
||
Receives an SQL query and forwards it to the `gitbase` server. | ||
|
||
The request body can have: | ||
|
||
* `query`: An SQL statement string. Do not include `LIMIT` here. | ||
* `limit`: Number, will be added as SQL `LIMIT` to the query. Optional. | ||
|
||
The success response will contain: | ||
|
||
* `status`: HTTP status code. | ||
* `data`: Rows, array of JSON objects. | ||
* `meta`: JSON object, with these fields: | ||
* `headers`: Array of strings with the names of the requested columns. | ||
* `types`: Array of strings with the types of each column. Note: these are the types reported by MySQL, so for example a type `BIT` will be a boolean in the `data` JSON. | ||
|
||
A failure response will contain: | ||
|
||
* `status`: HTTP status code. | ||
* `errors`: Array of JSON objects, with the fields below: | ||
* `status`: HTTP status code. | ||
* `title`: Error description. | ||
* `mysqlCode`: Error code reported by MySQL. May not be present for some errors. | ||
|
||
|
||
Some examples follow. A basic query: | ||
|
||
```bash | ||
curl -X POST \ | ||
http://localhost:8080/query \ | ||
-H 'content-type: application/json' \ | ||
-d '{ | ||
"query": "SELECT name, hash, is_remote(name) AS is_remote FROM refs", | ||
"limit": 20 | ||
}' | ||
``` | ||
|
||
```json | ||
{ | ||
"status": 200, | ||
"data": [ | ||
{ | ||
"hash": "66fd81178abfa342f873df5ab66639cca43f5104", | ||
"is_remote": false, | ||
"name": "HEAD" | ||
}, | ||
{ | ||
"hash": "66fd81178abfa342f873df5ab66639cca43f5104", | ||
"is_remote": false, | ||
"name": "refs/heads/master" | ||
}, | ||
{ | ||
"hash": "66fd81178abfa342f873df5ab66639cca43f5104", | ||
"is_remote": true, | ||
"name": "refs/remotes/origin/master" | ||
} | ||
], | ||
"meta": { | ||
"headers": [ | ||
"name", | ||
"hash", | ||
"is_remote" | ||
], | ||
"types": [ | ||
"TEXT", | ||
"TEXT", | ||
"BIT" | ||
] | ||
} | ||
} | ||
``` | ||
|
||
A failure: | ||
|
||
```bash | ||
curl -X POST \ | ||
http://localhost:8080/query \ | ||
-H 'content-type: application/json' \ | ||
-d '{ | ||
"query": "SELECT * FROM somewhere", | ||
"limit": 20 | ||
}' | ||
``` | ||
|
||
```json | ||
{ | ||
"status": 400, | ||
"errors": [ | ||
{ | ||
"status": 400, | ||
"title": "unknown error: table not found: somewhere", | ||
"mysqlCode": 1105 | ||
} | ||
] | ||
} | ||
``` | ||
|
||
For a query with uast nodes the protobuf response is unmarshalled and the json is returned: | ||
|
||
```bash | ||
curl -X POST \ | ||
http://localhost:8080/query \ | ||
-H 'content-type: application/json' \ | ||
-d '{ | ||
"query": "SELECT hash, content, uast(blobs.content, 'go') FROM blobs WHERE hash='fd30cea52792da5ece9156eea4022bdd87565633'", | ||
"limit": 20 | ||
}' | ||
``` | ||
|
||
```json | ||
{ | ||
"status": 200, | ||
"data": [ | ||
{ | ||
"content": "package server\n\nimport (\n\t\"net/http\"\n\n\t\"github.com/src-d/gitbase-playground/server/handler\"\n\n\t\"github.com/go-chi/chi\"\n\t\"github.com/go-chi/chi/middleware\"\n\t\"github.com/pressly/lg\"\n\t\"github.com/rs/cors\"\n\t\"github.com/sirupsen/logrus\"\n)\n\n// Router returns a Handler to serve the backend\nfunc Router(\n\tlogger *logrus.Logger,\n\tstatic *handler.Static,\n\tversion string,\n) http.Handler {\n\n\t// cors options\n\tcorsOptions := cors.Options{\n\t\tAllowedOrigins: []string{\"*\"},\n\t\tAllowedMethods: []string{\"GET\", \"POST\", \"PUT\", \"OPTIONS\"},\n\t\tAllowedHeaders: []string{\"Location\", \"Authorization\", \"Content-Type\"},\n\t\tAllowCredentials: true,\n\t}\n\n\tr := chi.NewRouter()\n\n\tr.Use(middleware.Recoverer)\n\tr.Use(cors.New(corsOptions).Handler)\n\tr.Use(lg.RequestLogger(logger))\n\n\tr.Get(\"/version\", handler.APIHandlerFunc(handler.Version(version)))\n\n\tr.Get(\"/static/*\", static.ServeHTTP)\n\tr.Get(\"/*\", static.ServeHTTP)\n\n\treturn r\n}\n", | ||
"hash": "fd30cea52792da5ece9156eea4022bdd87565633", | ||
"uast(blobs.content, \"go\")": [ | ||
{ | ||
"InternalType": "File", | ||
"Properties": { | ||
"Package": "1" | ||
}, | ||
"Children": [ | ||
{ | ||
"InternalType": "Ident", | ||
"Properties": { | ||
"internalRole": "Name" | ||
}, | ||
"Token": "server", | ||
"StartPosition": { | ||
"Offset": 9, | ||
"Line": 1, | ||
"Col": 10 | ||
}, | ||
"Roles": [ | ||
18, | ||
1 | ||
] | ||
}, | ||
{ | ||
"InternalType": "GenDecl", | ||
"Properties": { | ||
"Lparen": "24", | ||
"Tok": "import", | ||
"internalRole": "Decls" | ||
}, | ||
"Children": [ | ||
{ | ||
"InternalType": "ImportSpec", | ||
"Properties": { | ||
"EndPos": "0", | ||
"internalRole": "Specs" | ||
}, | ||
|
||
[...] | ||
|
||
} | ||
], | ||
"meta": { | ||
"headers": [ | ||
"hash", | ||
"content", | ||
"uast(blobs.content, \"go\")" | ||
], | ||
"types": [ | ||
"TEXT", | ||
"TEXT", | ||
"JSON" | ||
] | ||
} | ||
} | ||
``` | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry if I missed that somehow, not very related to the change, but while we are here - did not we have issues with this before, being not a valid IP on some OSes and decided that it safer to have default as localhost or 127.0.0.1 instead?
Remember discussing it with @smacker
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is not
0.0.0.0
the right one?That's how it was configured in CAT, because that way the container is listening "everywhere"
https://github.com/src-d/code-annotation/pull/153/files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
0.0.0.0
is a correct one. We use this variable for the server. And server should listen on all IPs.