Include example backend implementation for the connect feature
This commit is contained in:
17
config/examples/connect_backend_go/README.md
Normal file
17
config/examples/connect_backend_go/README.md
Normal file
@@ -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
|
||||
```
|
||||
52
config/examples/connect_backend_go/main.go
Normal file
52
config/examples/connect_backend_go/main.go
Normal file
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user