Skip to content

Commit

Permalink
Merge pull request #36 from supabase-community/development
Browse files Browse the repository at this point in the history
Golang version Update and Create Ping Check
  • Loading branch information
muratmirgun committed Oct 3, 2023
2 parents 04509c0 + 409646e commit 2253328
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -16,7 +16,7 @@

#Ide Folder Ignore
.idea

pgdata
#VSCode Folder Ignore
.vscode

Expand Down
30 changes: 26 additions & 4 deletions client.go
Expand Up @@ -3,6 +3,7 @@ package postgrest
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
Expand All @@ -13,6 +14,12 @@ var (
version = "v0.0.6"
)

type Client struct {
ClientError error
session http.Client
clientTransport transport
}

// NewClient constructs a new client given a URL to a Postgrest instance.
func NewClient(rawURL, schema string, headers map[string]string) *Client {
// Create URL from rawURL
Expand Down Expand Up @@ -50,10 +57,25 @@ func NewClient(rawURL, schema string, headers map[string]string) *Client {
return &c
}

type Client struct {
ClientError error
session http.Client
clientTransport transport
func (c *Client) Ping() bool {
req, err := http.NewRequest("GET", path.Join(c.clientTransport.baseURL.Path, ""), nil)
if err != nil {
c.ClientError = err
return false
}

resp, err := c.session.Do(req)
if err != nil {
c.ClientError = err
return false
}

if resp.Status != "200 OK" {
c.ClientError = errors.New("ping failed")
return false
}

return true
}

// TokenAuth sets authorization headers for subsequent requests.
Expand Down
16 changes: 8 additions & 8 deletions docker-compose.yaml
Expand Up @@ -5,9 +5,9 @@ services:
ports:
- "3000:3000"
environment:
PGRST_DB_URI: postgres://app_user:password@db:5432/app_db
PGRST_DB_URI: postgres://postgres:postgres@db:5432/postgres
PGRST_DB_SCHEMA: public
PGRST_DB_ANON_ROLE: app_user # In production this role should not be the same as the one used for connection
PGRST_DB_ANON_ROLE: postgres # In production this role should not be the same as the one used for connection
PGRST_OPENAPI_SERVER_PROXY_URI: "http://127.0.0.1:3000"
depends_on:
- db
Expand All @@ -16,10 +16,10 @@ services:
ports:
- "5432:5432"
environment:
POSTGRES_DB: app_db
POSTGRES_USER: app_user
POSTGRES_PASSWORD: password
# Uncomment this if you want to persist the data. Create your boostrap SQL file in the project root
# volumes:
# - "./pgdata:/var/lib/postgresql/data"
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
# Uncomment this if you want to persist the data. Create your boostrap SQL file in the project root
volumes:
- "./pgdata:/var/lib/postgresql/data"
# - "./init.sql:/docker-entrypoint-initdb.d/init.sql"
12 changes: 9 additions & 3 deletions go.mod
@@ -1,8 +1,14 @@
module github.com/supabase/postgrest-go

go 1.16
go 1.19

require (
github.com/jarcoal/httpmock v1.1.0
github.com/stretchr/testify v1.7.0
github.com/jarcoal/httpmock v1.3.1
github.com/stretchr/testify v1.8.4
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
18 changes: 9 additions & 9 deletions go.sum
@@ -1,13 +1,13 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jarcoal/httpmock v1.1.0 h1:F47ChZj1Y2zFsCXxNkBPwNNKnAyOATcdQibk0qEdVCE=
github.com/jarcoal/httpmock v1.1.0/go.mod h1:ATjnClrvW/3tijVmpL/va5Z3aAyGvqU3gCT8nX0Txik=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww=
github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg=
github.com/maxatome/go-testdeep v1.12.0 h1:Ql7Go8Tg0C1D/uMMX59LAoYK7LffeJQ6X2T04nTH68g=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
8 changes: 4 additions & 4 deletions test/basic/main.go
Expand Up @@ -11,13 +11,13 @@ import (
)

var (
REST_URL = `http://localhost:3000`
headers = map[string]string{}
schema = "public"
RestUrl = `http://localhost:3000`
headers = map[string]string{}
schema = "public"
)

func main() {
client := postgrest.NewClient(REST_URL, schema, headers)
client := postgrest.NewClient(RestUrl, schema, headers)

res, count, err := client.From("todos").Select("id,task,done", "", false).Eq("task", "that created from postgrest-go").Execute()
if err != nil {
Expand Down

0 comments on commit 2253328

Please sign in to comment.