diff --git a/config/examples/connect_backend_go/README.md b/config/examples/connect_backend_go/README.md new file mode 100644 index 0000000..40b9f6e --- /dev/null +++ b/config/examples/connect_backend_go/README.md @@ -0,0 +1,17 @@ +# connect-backend-go + +Example Golang backend for Pgweb Connect feature + +## Usage + +Run the backend: + +```bash +go run main.go +``` + +Configure pgweb: + +```bash +pgweb --sessions --connect-backend=http://localhost:4567 --connect-token=test +``` diff --git a/config/examples/connect_backend_go/main.go b/config/examples/connect_backend_go/main.go new file mode 100644 index 0000000..bdcac01 --- /dev/null +++ b/config/examples/connect_backend_go/main.go @@ -0,0 +1,52 @@ +package main + +import ( + "encoding/json" + "fmt" + "log" + "net/http" +) + +type BackendRequest struct { + Resource string `json:"resource"` + Token string `json:"token"` + Headers map[string]string `json:"headers"` +} + +type BackendResponse struct { + DatabaseURL string `json:"database_url"` +} + +func main() { + resources := map[string]string{ + "id1": "postgres://localhost:5432/db1?sslmode=disable", + "id2": "postgres://localhost:5432/db2?sslmode=disable", + "id3": "postgres://localhost:5432/db3?sslmode=disable", + } + + http.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) { + backendReq := BackendRequest{} + + if err := json.NewDecoder(req.Body).Decode(&backendReq); err != nil { + rw.WriteHeader(400) + fmt.Fprintf(rw, "error while parsing request: %v", err) + return + } + + res, ok := resources[backendReq.Resource] + if !ok { + rw.WriteHeader(404) + return + } + + resp := BackendResponse{ + DatabaseURL: res, + } + + json.NewEncoder(rw).Encode(resp) + }) + + if err := http.ListenAndServe(":4567", nil); err != nil { + log.Fatal(err) + } +} diff --git a/config/examples/connect_backend_ruby/Gemfile b/config/examples/connect_backend_ruby/Gemfile new file mode 100644 index 0000000..725adc1 --- /dev/null +++ b/config/examples/connect_backend_ruby/Gemfile @@ -0,0 +1,5 @@ +source "https://rubygems.org" + +gem "sinatra" +gem "json" +gem "puma" diff --git a/config/examples/connect_backend_ruby/Gemfile.lock b/config/examples/connect_backend_ruby/Gemfile.lock new file mode 100644 index 0000000..edabb36 --- /dev/null +++ b/config/examples/connect_backend_ruby/Gemfile.lock @@ -0,0 +1,30 @@ +GEM + remote: https://rubygems.org/ + specs: + json (2.6.2) + mustermann (3.0.0) + ruby2_keywords (~> 0.0.1) + nio4r (2.5.8) + puma (6.0.0) + nio4r (~> 2.0) + rack (2.2.4) + rack-protection (3.0.4) + rack + ruby2_keywords (0.0.5) + sinatra (3.0.4) + mustermann (~> 3.0) + rack (~> 2.2, >= 2.2.4) + rack-protection (= 3.0.4) + tilt (~> 2.0) + tilt (2.0.11) + +PLATFORMS + arm64-darwin-21 + +DEPENDENCIES + json + puma + sinatra + +BUNDLED WITH + 2.3.9 diff --git a/config/examples/connect_backend_ruby/README.md b/config/examples/connect_backend_ruby/README.md new file mode 100644 index 0000000..b1ea4dd --- /dev/null +++ b/config/examples/connect_backend_ruby/README.md @@ -0,0 +1,18 @@ +# connect-backend-ruby + +Example Ruby backend for Pgweb Connect feature + +## Usage + +Install and run the backend: + +```bash +bundle install +ruby main.rb +``` + +Configure pgweb: + +```bash +pgweb --sessions --connect-backend=http://localhost:4567 --connect-token=test +``` diff --git a/config/examples/connect_backend_ruby/config.ru b/config/examples/connect_backend_ruby/config.ru new file mode 100644 index 0000000..b3ab0e4 --- /dev/null +++ b/config/examples/connect_backend_ruby/config.ru @@ -0,0 +1,4 @@ +require "bundler/setup" +require "./main" + +run Sinatra::Application diff --git a/config/examples/connect_backend_ruby/main.rb b/config/examples/connect_backend_ruby/main.rb new file mode 100644 index 0000000..6fd6fd7 --- /dev/null +++ b/config/examples/connect_backend_ruby/main.rb @@ -0,0 +1,40 @@ +require "sinatra" + +# Authentication token +$token = "test" + +# List of all availble resources +$resources = { + "id1" => "postgres://localhost:5432/db1?sslmode=disable", + "id2" => "postgres://localhost:5432/db2?sslmode=disable", + "id3" => "postgres://localhost:5432/db3?sslmode=disable" +} + +helpers do + def error(code, message) + halt(code, JSON.dump(error: message)) + end +end + +before do + content_type :json +end + +post "/" do + req = JSON.load(request.body) || {} + + unless req["resource"] + halt 404, "Resource ID required" + end + + # Check the resource + resource = $resources[req["resource"]] + if !resource + halt 404, "Invalid resource ID" + end + + # Return connection credentials + JSON.dump( + database_url: resource + ) +end