Skip to content

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 3 commits into from
May 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 54 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@
[prune]
go-tests = true
unused-packages = true

[[constraint]]
name = "github.com/go-sql-driver/mysql"
branch = "master"
19 changes: 17 additions & 2 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
package main

import (
"database/sql"
"fmt"
"net/http"

"github.com/src-d/gitbase-playground/server"
"github.com/src-d/gitbase-playground/server/handler"
"github.com/src-d/gitbase-playground/server/service"

_ "github.com/go-sql-driver/mysql"
"github.com/kelseyhightower/envconfig"
)

// version will be replaced automatically by the CI build.
// See https://github.com/src-d/ci/blob/v1/Makefile.main#L56
var version = "dev"

// Note: maxAllowedPacket must be explicitly set for go-sql-driver/mysql v1.3.
// Otherwise gitbase will be asked for the max_allowed_packet column and the
// query will fail.
// The next release should make this parameter optional for us:
// https://github.com/go-sql-driver/mysql/pull/680
type appConfig struct {
Env string `envconfig:"ENV" default:"production"`
Host string `envconfig:"HOST" default:"0.0.0.0"`
Copy link
Contributor

@bzz bzz Apr 27, 2018

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

Copy link
Contributor

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

Copy link
Contributor

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.

Port int `envconfig:"PORT" default:"8080"`
ServerURL string `envconfig:"SERVER_URL"`
DBConn string `envconfig:"DB_CONNECTION" default:"root@tcp(localhost:3306)/none?maxAllowedPacket=4194304"`
}

func main() {
Expand All @@ -33,11 +41,18 @@ func main() {
// logger
logger := service.NewLogger(conf.Env)

// database
db, err := sql.Open("mysql", conf.DBConn)
if err != nil {
logger.Fatalf("error opening the database: %s", err)
}
defer db.Close()

static := handler.NewStatic("build", conf.ServerURL)

// start the router
router := server.Router(logger, static, version)
router := server.Router(logger, static, version, db)
logger.Infof("listening on %s:%d", conf.Host, conf.Port)
err := http.ListenAndServe(fmt.Sprintf("%s:%d", conf.Host, conf.Port), router)
err = http.ListenAndServe(fmt.Sprintf("%s:%d", conf.Host, conf.Port), router)
logger.Fatal(err)
}
176 changes: 176 additions & 0 deletions docs/rest-api.md
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"
]
}
}
```

Loading