Skip to content

New endpoint get /tables #11

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 1 commit 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
45 changes: 44 additions & 1 deletion docs/rest-api.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,49 @@
# Rest API

## GET /tables

Returns the list of tables.

Alias for `/query` with `SHOW TABLES` query. See below for more details.

```bash
curl -X GET http://localhost:8080/tables
```

```json
{
"status": 200,
"data": [
{
"table": "blobs"
},
{
"table": "commits"
},
{
"table": "refs"
},
{
"table": "remotes"
},
{
"table": "repositories"
},
{
"table": "tree_entries"
}
],
"meta": {
"headers": [
"table"
],
"types": [
"TEXT"
]
}
}
```

## POST /query

Receives an SQL query and forwards it to the `gitbase` server.
Expand Down Expand Up @@ -173,4 +217,3 @@ curl -X POST \
}
}
```

16 changes: 11 additions & 5 deletions server/handler/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,23 @@ type QuerySuite struct {
handler http.Handler
}

func (suite *QuerySuite) SetupSuite() {
func setupDB(require *require.Assertions) *sql.DB {
var conf appConfig
envconfig.MustProcess("GITBASEPG", &conf)

// db
var err error
suite.db, err = sql.Open("mysql", conf.DBConn)
suite.Require().Nil(err)
db, err := sql.Open("mysql", conf.DBConn)
require.Nil(err)

err = suite.db.Ping()
suite.Require().Nil(err)
err = db.Ping()
require.Nil(err)

return db
}

func (suite *QuerySuite) SetupSuite() {
suite.db = setupDB(suite.Require())

// logger
logger := logrus.New()
Expand Down
19 changes: 19 additions & 0 deletions server/handler/tables.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package handler

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

"github.com/src-d/gitbase-playground/server/serializer"
)

// Tables returns a function that calls /query with the SQL "SHOW TABLES"
func Tables(db *sql.DB) RequestProcessFunc {
return func(r *http.Request) (*serializer.Response, error) {
req, _ := http.NewRequest("POST", "/query",
strings.NewReader(`{ "query": "SHOW TABLES" }`))

return Query(db)(req)
}
}
59 changes: 59 additions & 0 deletions server/handler/tables_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package handler_test

import (
"database/sql"
"net/http"
"net/http/httptest"
"testing"

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

"github.com/pressly/lg"
"github.com/stretchr/testify/suite"
)

// Suite setup
// -----------------------------------------------------------------------------

type TablesSuite struct {
suite.Suite
db *sql.DB
handler http.Handler
}

func (suite *TablesSuite) SetupSuite() {
suite.db = setupDB(suite.Require())

// logger
logger := logrus.New()

// handler
tablesHandler := handler.APIHandlerFunc(handler.Tables(suite.db))
suite.handler = lg.RequestLogger(logger)(tablesHandler)
}

func (suite *TablesSuite) TearDownSuite() {
suite.db.Close()
}

// Tests
// -----------------------------------------------------------------------------

func (suite *TablesSuite) TestGet() {
req, _ := http.NewRequest("GET", "/tables", nil)

res := httptest.NewRecorder()
suite.handler.ServeHTTP(res, req)

okResponse(suite.Require(), res)

firstRow := firstRow(suite.Require(), res)
suite.IsType("string", firstRow["table"])
}

// Main test to run the suite

func TestTablesSuite(t *testing.T) {
suite.Run(t, new(TablesSuite))
}
1 change: 1 addition & 0 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func Router(
r.Use(lg.RequestLogger(logger))

r.Post("/query", handler.APIHandlerFunc(handler.Query(db)))
r.Get("/tables", handler.APIHandlerFunc(handler.Tables(db)))

r.Get("/version", handler.APIHandlerFunc(handler.Version(version)))

Expand Down