mirror of
https://github.com/sosedoff/pgweb.git
synced 2024-12-15 11:52:12 +03:00
Merge branch 'master' into ui
This commit is contained in:
commit
9e89b4b9d2
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
.DS_Store
|
||||
pgweb
|
||||
pgweb
|
||||
bin
|
49
API.md
Normal file
49
API.md
Normal file
@ -0,0 +1,49 @@
|
||||
## API
|
||||
|
||||
Get current database tables:
|
||||
|
||||
```
|
||||
GET /tables
|
||||
```
|
||||
|
||||
Get table details:
|
||||
|
||||
```
|
||||
GET /tables/:name
|
||||
```
|
||||
|
||||
Execute select query:
|
||||
|
||||
```
|
||||
POST /select?query=SQL
|
||||
GET /select?query=SQL
|
||||
```
|
||||
|
||||
### Response formats
|
||||
|
||||
Successful response:
|
||||
|
||||
```json
|
||||
{
|
||||
"columns": [
|
||||
"column_name1",
|
||||
"column_name2",
|
||||
"column_name3"
|
||||
],
|
||||
"rows": [
|
||||
[
|
||||
"column 1 value",
|
||||
"column 2 value",
|
||||
"column 3 value"
|
||||
]
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
Error response:
|
||||
|
||||
```json
|
||||
{
|
||||
"error": "Error message"
|
||||
}
|
||||
```
|
66
README.md
66
README.md
@ -1,6 +1,6 @@
|
||||
# pgweb
|
||||
|
||||
Experiments with PostgreSQL and GO
|
||||
Web-based PostgreSQL database browser written in Go.
|
||||
|
||||
## Usage
|
||||
|
||||
@ -11,65 +11,23 @@ CLI options:
|
||||
-p, --port= Server port (5432)
|
||||
-u, --user= Database user (postgres)
|
||||
-d, --db= Database name (postgres)
|
||||
--url= Database connection string (postgresql://...)
|
||||
--ssl= SSL option (disable)
|
||||
```
|
||||
|
||||
## Compile
|
||||
## Compile from source
|
||||
|
||||
Go 1.3+ is required. To complire source execute:
|
||||
Go 1.3+ is required. You can install Go with `homebrew`:
|
||||
|
||||
```
|
||||
brew install go
|
||||
```
|
||||
|
||||
To compile source code run the following command:
|
||||
|
||||
```
|
||||
go get
|
||||
go build
|
||||
```
|
||||
|
||||
This will produce `pgweb` binary in the current workdir.
|
||||
|
||||
## API
|
||||
|
||||
Get current database tables:
|
||||
|
||||
```
|
||||
GET /tables
|
||||
```
|
||||
|
||||
Get table details:
|
||||
|
||||
```
|
||||
GET /tables/:name
|
||||
```
|
||||
|
||||
Execute select query:
|
||||
|
||||
```
|
||||
POST /select?query=SQL
|
||||
GET /select?query=SQL
|
||||
```
|
||||
|
||||
### Response formats
|
||||
|
||||
Successful response:
|
||||
|
||||
```json
|
||||
{
|
||||
"columns": [
|
||||
"column_name1",
|
||||
"column_name2",
|
||||
"column_name3"
|
||||
],
|
||||
"rows": [
|
||||
[
|
||||
"column 1 value",
|
||||
"column 2 value",
|
||||
"column 3 value"
|
||||
]
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
Error response:
|
||||
|
||||
```json
|
||||
{
|
||||
"error": "Error message"
|
||||
}
|
||||
```
|
||||
This will produce `pgweb` binary in the current directory.
|
16
api.go
16
api.go
@ -19,7 +19,6 @@ func API_RunQuery(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
history = append(history, query)
|
||||
API_HandleQuery(query, c)
|
||||
}
|
||||
|
||||
@ -46,7 +45,7 @@ func API_GetTable(c *gin.Context) {
|
||||
}
|
||||
|
||||
func API_History(c *gin.Context) {
|
||||
c.JSON(200, history)
|
||||
c.JSON(200, dbClient.history)
|
||||
}
|
||||
|
||||
func API_Info(c *gin.Context) {
|
||||
@ -70,14 +69,11 @@ func API_HandleQuery(query string, c *gin.Context) {
|
||||
|
||||
q := c.Request.URL.Query()
|
||||
|
||||
format := q["format"][0]
|
||||
if format == "" {
|
||||
format = "json"
|
||||
}
|
||||
|
||||
if format == "csv" {
|
||||
c.String(200, result.CSV())
|
||||
return
|
||||
if len(q["format"]) > 0 {
|
||||
if q["format"][0] == "csv" {
|
||||
c.String(200, result.CSV())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(200, result)
|
||||
|
Binary file not shown.
11
client.go
11
client.go
@ -15,7 +15,8 @@ const (
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
db *sqlx.DB
|
||||
db *sqlx.DB
|
||||
history []string
|
||||
}
|
||||
|
||||
type Result struct {
|
||||
@ -37,6 +38,10 @@ func NewClient() (*Client, error) {
|
||||
return &Client{db: db}, nil
|
||||
}
|
||||
|
||||
func (client *Client) recordQuery(query string) {
|
||||
client.history = append(client.history, query)
|
||||
}
|
||||
|
||||
func (client *Client) Tables() ([]string, error) {
|
||||
res, err := client.Query(SQL_TABLES)
|
||||
|
||||
@ -56,6 +61,8 @@ func (client *Client) Tables() ([]string, error) {
|
||||
func (client *Client) Query(query string) (*Result, error) {
|
||||
rows, err := client.db.Queryx(query)
|
||||
|
||||
client.recordQuery(query)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -115,6 +122,8 @@ func (res *Result) CSV() string {
|
||||
buff := &bytes.Buffer{}
|
||||
writer := csv.NewWriter(buff)
|
||||
|
||||
writer.Write(res.Columns)
|
||||
|
||||
for _, row := range res.Rows {
|
||||
record := make([]string, len(res.Columns))
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user