Skip to content

Commit 81fb7bb

Browse files
authored
Merge pull request #11 from carlosms/get-tables
New endpoint get /tables
2 parents 1b0c406 + 3c33efe commit 81fb7bb

File tree

5 files changed

+134
-6
lines changed

5 files changed

+134
-6
lines changed

docs/rest-api.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,49 @@
11
# Rest API
22

3+
## GET /tables
4+
5+
Returns the list of tables.
6+
7+
Alias for `/query` with `SHOW TABLES` query. See below for more details.
8+
9+
```bash
10+
curl -X GET http://localhost:8080/tables
11+
```
12+
13+
```json
14+
{
15+
"status": 200,
16+
"data": [
17+
{
18+
"table": "blobs"
19+
},
20+
{
21+
"table": "commits"
22+
},
23+
{
24+
"table": "refs"
25+
},
26+
{
27+
"table": "remotes"
28+
},
29+
{
30+
"table": "repositories"
31+
},
32+
{
33+
"table": "tree_entries"
34+
}
35+
],
36+
"meta": {
37+
"headers": [
38+
"table"
39+
],
40+
"types": [
41+
"TEXT"
42+
]
43+
}
44+
}
45+
```
46+
347
## POST /query
448

549
Receives an SQL query and forwards it to the `gitbase` server.
@@ -173,4 +217,3 @@ curl -X POST \
173217
}
174218
}
175219
```
176-

server/handler/query_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,23 @@ type QuerySuite struct {
3232
handler http.Handler
3333
}
3434

35-
func (suite *QuerySuite) SetupSuite() {
35+
func setupDB(require *require.Assertions) *sql.DB {
3636
var conf appConfig
3737
envconfig.MustProcess("GITBASEPG", &conf)
3838

3939
// db
4040
var err error
41-
suite.db, err = sql.Open("mysql", conf.DBConn)
42-
suite.Require().Nil(err)
41+
db, err := sql.Open("mysql", conf.DBConn)
42+
require.Nil(err)
4343

44-
err = suite.db.Ping()
45-
suite.Require().Nil(err)
44+
err = db.Ping()
45+
require.Nil(err)
46+
47+
return db
48+
}
49+
50+
func (suite *QuerySuite) SetupSuite() {
51+
suite.db = setupDB(suite.Require())
4652

4753
// logger
4854
logger := logrus.New()

server/handler/tables.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package handler
2+
3+
import (
4+
"database/sql"
5+
"net/http"
6+
"strings"
7+
8+
"github.com/src-d/gitbase-playground/server/serializer"
9+
)
10+
11+
// Tables returns a function that calls /query with the SQL "SHOW TABLES"
12+
func Tables(db *sql.DB) RequestProcessFunc {
13+
return func(r *http.Request) (*serializer.Response, error) {
14+
req, _ := http.NewRequest("POST", "/query",
15+
strings.NewReader(`{ "query": "SHOW TABLES" }`))
16+
17+
return Query(db)(req)
18+
}
19+
}

server/handler/tables_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package handler_test
2+
3+
import (
4+
"database/sql"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/sirupsen/logrus"
10+
"github.com/src-d/gitbase-playground/server/handler"
11+
12+
"github.com/pressly/lg"
13+
"github.com/stretchr/testify/suite"
14+
)
15+
16+
// Suite setup
17+
// -----------------------------------------------------------------------------
18+
19+
type TablesSuite struct {
20+
suite.Suite
21+
db *sql.DB
22+
handler http.Handler
23+
}
24+
25+
func (suite *TablesSuite) SetupSuite() {
26+
suite.db = setupDB(suite.Require())
27+
28+
// logger
29+
logger := logrus.New()
30+
31+
// handler
32+
tablesHandler := handler.APIHandlerFunc(handler.Tables(suite.db))
33+
suite.handler = lg.RequestLogger(logger)(tablesHandler)
34+
}
35+
36+
func (suite *TablesSuite) TearDownSuite() {
37+
suite.db.Close()
38+
}
39+
40+
// Tests
41+
// -----------------------------------------------------------------------------
42+
43+
func (suite *TablesSuite) TestGet() {
44+
req, _ := http.NewRequest("GET", "/tables", nil)
45+
46+
res := httptest.NewRecorder()
47+
suite.handler.ServeHTTP(res, req)
48+
49+
okResponse(suite.Require(), res)
50+
51+
firstRow := firstRow(suite.Require(), res)
52+
suite.IsType("string", firstRow["table"])
53+
}
54+
55+
// Main test to run the suite
56+
57+
func TestTablesSuite(t *testing.T) {
58+
suite.Run(t, new(TablesSuite))
59+
}

server/router.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func Router(
3636
r.Use(lg.RequestLogger(logger))
3737

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

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

0 commit comments

Comments
 (0)