Include example backend implementation for the connect feature

This commit is contained in:
Dan Sosedoff
2022-12-01 16:31:02 -06:00
parent 6c3bd96606
commit 9b8cbb05e3
7 changed files with 166 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
source "https://rubygems.org"
gem "sinatra"
gem "json"
gem "puma"

View File

@@ -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

View File

@@ -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
```

View File

@@ -0,0 +1,4 @@
require "bundler/setup"
require "./main"
run Sinatra::Application

View File

@@ -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