lt-1
This commit is contained in:
commit
723db007ee
|
|
@ -0,0 +1,13 @@
|
||||||
|
.git
|
||||||
|
.gitignore
|
||||||
|
.vscode
|
||||||
|
.idea
|
||||||
|
/bin
|
||||||
|
/build
|
||||||
|
/dist
|
||||||
|
/vendor
|
||||||
|
Dockerfile~
|
||||||
|
*.swp
|
||||||
|
*.swo
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
# Binaries for programs and plugins
|
||||||
|
*.exe
|
||||||
|
*.exe~
|
||||||
|
*.dll
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
|
||||||
|
# Test binary, build outputs
|
||||||
|
*.test
|
||||||
|
/bin/
|
||||||
|
/build/
|
||||||
|
/dist/
|
||||||
|
|
||||||
|
# Dependency directories (go mod or vendoring)
|
||||||
|
/vendor/
|
||||||
|
|
||||||
|
# IDE/editor settings
|
||||||
|
.vscode/
|
||||||
|
.idea/
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
# syntax=docker/dockerfile:1
|
||||||
|
|
||||||
|
FROM golang:1.24-alpine AS builder
|
||||||
|
WORKDIR /app
|
||||||
|
ENV CGO_ENABLED=0 GOOS=linux
|
||||||
|
RUN apk add --no-cache ca-certificates git
|
||||||
|
COPY go.mod go.sum ./
|
||||||
|
RUN go mod download
|
||||||
|
COPY . .
|
||||||
|
RUN go build -ldflags="-s -w" -o /out/app .
|
||||||
|
|
||||||
|
FROM gcr.io/distroless/base-debian12:nonroot
|
||||||
|
WORKDIR /app
|
||||||
|
COPY --from=builder /out/app /app/app
|
||||||
|
EXPOSE 8080
|
||||||
|
USER nonroot:nonroot
|
||||||
|
ENTRYPOINT ["/app/app"]
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
# tables-proxy-2
|
||||||
|
|
||||||
|
Minimal Go project scaffold.
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
- Go 1.22+ installed
|
||||||
|
|
||||||
|
## Run
|
||||||
|
```bash
|
||||||
|
go run .
|
||||||
|
```
|
||||||
|
|
||||||
|
## Build
|
||||||
|
```bash
|
||||||
|
go build .
|
||||||
|
```
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
- Uses `pgx` stdlib driver. If you're behind PgBouncer (transaction pooling), the DSN includes `prefer_simple_protocol=true` to avoid prepared statements.
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
module tables-proxy-2
|
||||||
|
|
||||||
|
go 1.24.0
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/google/uuid v1.6.0
|
||||||
|
github.com/jackc/pgx/v5 v5.8.0
|
||||||
|
github.com/jmoiron/sqlx v1.4.0
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||||
|
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||||
|
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||||
|
golang.org/x/sync v0.17.0 // indirect
|
||||||
|
golang.org/x/text v0.29.0 // indirect
|
||||||
|
)
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||||
|
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
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/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
|
||||||
|
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
|
||||||
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||||
|
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||||
|
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||||
|
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||||
|
github.com/jackc/pgx/v5 v5.8.0 h1:TYPDoleBBme0xGSAX3/+NujXXtpZn9HBONkQC7IEZSo=
|
||||||
|
github.com/jackc/pgx/v5 v5.8.0/go.mod h1:QVeDInX2m9VyzvNeiCJVjCkNFqzsNb43204HshNSZKw=
|
||||||
|
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
|
||||||
|
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||||
|
github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o=
|
||||||
|
github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY=
|
||||||
|
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||||
|
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||||
|
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.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||||
|
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||||
|
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
|
||||||
|
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
||||||
|
golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk=
|
||||||
|
golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
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=
|
||||||
|
|
@ -0,0 +1,258 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
_ "github.com/jackc/pgx/v5/stdlib"
|
||||||
|
"github.com/jmoiron/sqlx"
|
||||||
|
)
|
||||||
|
|
||||||
|
const insertSQL = `
|
||||||
|
INSERT INTO public."Gateways" (
|
||||||
|
id, "name", description, routes, state, meta, account_id,
|
||||||
|
"domain", created_by, modified_by, created_at, modified_at, domains
|
||||||
|
) VALUES (
|
||||||
|
$1::uuid, 'lalit gateway', 'Untitled Gateway Description',
|
||||||
|
'[{"id": "f8263694-245a-4c5f-8028-769bc6bd6487", "path": "/", "type": "integration", "method": "get", "description": "", "connected_services": {"id": "50b1d1a8-5fe4-4fa1-919e-99255455ba9d", "url": "https://asia-south1.api.fcz0.de/service/webhook/temporal/v1.0/c7d5096b-e8dd-4520-b1b9-0e4e01b839ab/workflows/execute/50b1d1a8-5fe4-4fa1-919e-99255455ba9d/0.0.1/webhook", "meta": {"is_async": true}, "type": "workflow"}}, {"id": "f1347d24-2dc8-44c5-94c8-6d5b74fd96bb", "path": "/products", "type": "integration", "method": "get", "description": "", "connected_services": {"id": "50b1d1a8-5fe4-4fa1-919e-99255455ba9d", "url": "https://asia-south1.api.fcz0.de/service/webhook/temporal/v1.0/c7d5096b-e8dd-4520-b1b9-0e4e01b839ab/workflows/execute/50b1d1a8-5fe4-4fa1-919e-99255455ba9d/0.0.1/webhook", "meta": {"is_async": true}, "type": "workflow"}}, {"id": "5a85d0c4-db79-4f02-91d4-17c29a68d243", "path": "/brands", "type": "integration", "method": "post", "description": "", "connected_services": {"id": "f595ed0f-dd6a-4b13-b74c-1f7a575d02e8", "url": "https://asia-south1.api.fcz0.de/service/webhook/temporal/v1.0/c7d5096b-e8dd-4520-b1b9-0e4e01b839ab/workflows/execute/f595ed0f-dd6a-4b13-b74c-1f7a575d02e8/0.0.9/webhook", "meta": {"rewrite": false, "merge_params": true, "rewrite_target": ""}, "type": "serverless"}}]'::jsonb,
|
||||||
|
'active', '{}'::jsonb, 'f32bd51e-89c1-4a29-bb37-cc25d7aeb07c',
|
||||||
|
'anujsahu7.app.fcz0.de', 'anujsahu@gofynd.com', 'anujsahu@gofynd.com',
|
||||||
|
'2025-04-24 17:16:47.172', '2025-04-24 17:16:47.172',
|
||||||
|
'{"self": "anujsahu7.app.fcz0.de", "test": null}'::jsonb
|
||||||
|
)
|
||||||
|
`
|
||||||
|
|
||||||
|
const selectSQL = `
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
|
"name",
|
||||||
|
description,
|
||||||
|
routes,
|
||||||
|
state,
|
||||||
|
meta,
|
||||||
|
account_id,
|
||||||
|
"domain",
|
||||||
|
created_by,
|
||||||
|
modified_by,
|
||||||
|
created_at,
|
||||||
|
modified_at,
|
||||||
|
domains
|
||||||
|
FROM public."Gateways"
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
LIMIT $1
|
||||||
|
`
|
||||||
|
|
||||||
|
type Server struct {
|
||||||
|
db *sqlx.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Example DSN:
|
||||||
|
// export DATABASE_URL="postgres://user:pass@localhost:5432/dbname?sslmode=disable"
|
||||||
|
dsn := getenv("DATABASE_URL")
|
||||||
|
if dsn == "" {
|
||||||
|
dsn = "postgresql://boltic_097b913d-c1c0-48f1-9a95-d44308e19981_e124aef0:35d2564f125a4f94@asia-south1.boltic-tables.fcz0.de/rashmi_external_db9_fce9ea6a08db?sslmode=disable"
|
||||||
|
}
|
||||||
|
// dsn = addDSNParam(dsn, "prefer_simple_protocol", "true")
|
||||||
|
|
||||||
|
db, err := sqlx.Connect("pgx", dsn)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("db connect failed: %v", err)
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
s := &Server{db: db}
|
||||||
|
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.HandleFunc("/api/insert", s.handleInsertGateways)
|
||||||
|
mux.HandleFunc("/api/select", s.handleSelectGateways)
|
||||||
|
mux.HandleFunc("/api/health", s.handleHealth)
|
||||||
|
|
||||||
|
addr := ":8080"
|
||||||
|
log.Printf("listening on %s", addr)
|
||||||
|
if err := http.ListenAndServe(addr, withJSONHeaders(mux)); err != nil {
|
||||||
|
log.Fatalf("server failed: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleInsertGateways(w http.ResponseWriter, r *http.Request) {
|
||||||
|
count := parseIntQuery(r, "count", 1)
|
||||||
|
if count < 1 {
|
||||||
|
count = 1
|
||||||
|
}
|
||||||
|
if count > 2000 {
|
||||||
|
count = 2000
|
||||||
|
}
|
||||||
|
|
||||||
|
resp := map[string]any{
|
||||||
|
"requestedCount": count,
|
||||||
|
"successCount": 0,
|
||||||
|
"failureCount": 0,
|
||||||
|
"message": "Insert operation completed",
|
||||||
|
}
|
||||||
|
|
||||||
|
success := 0
|
||||||
|
failure := 0
|
||||||
|
|
||||||
|
// Optional: wrap in a transaction for speed when count is large
|
||||||
|
tx, err := s.db.BeginTxx(r.Context(), &sql.TxOptions{})
|
||||||
|
if err != nil {
|
||||||
|
writeJSON(w, http.StatusInternalServerError, map[string]any{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
_ = tx.Rollback()
|
||||||
|
}()
|
||||||
|
|
||||||
|
for i := 0; i < count; i++ {
|
||||||
|
id := uuid.New().String()
|
||||||
|
if _, err := tx.ExecContext(r.Context(), insertSQL, id); err != nil {
|
||||||
|
failure++
|
||||||
|
// keep going like your Java code (commented try/catch)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
success++
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tx.Commit(); err != nil {
|
||||||
|
writeJSON(w, http.StatusInternalServerError, map[string]any{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
resp["successCount"] = success
|
||||||
|
resp["failureCount"] = failure
|
||||||
|
writeJSON(w, http.StatusOK, resp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleSelectGateways(w http.ResponseWriter, r *http.Request) {
|
||||||
|
count := parseIntQuery(r, "count", 10)
|
||||||
|
if count < 1 {
|
||||||
|
count = 1
|
||||||
|
}
|
||||||
|
if count > 200 {
|
||||||
|
count = 200
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := s.db.QueryxContext(r.Context(), selectSQL, count)
|
||||||
|
if err != nil {
|
||||||
|
writeJSON(w, http.StatusInternalServerError, map[string]any{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
out := make([]map[string]any, 0, count)
|
||||||
|
for rows.Next() {
|
||||||
|
m := map[string]any{}
|
||||||
|
if err := rows.MapScan(m); err != nil {
|
||||||
|
writeJSON(w, http.StatusInternalServerError, map[string]any{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Optional: make JSON nicer (pq returns []byte for some types)
|
||||||
|
for k, v := range m {
|
||||||
|
if b, ok := v.([]byte); ok {
|
||||||
|
m[k] = string(b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
out = append(out, m)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
writeJSON(w, http.StatusInternalServerError, map[string]any{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
resp := map[string]any{
|
||||||
|
"requestedCount": count,
|
||||||
|
"returnedCount": len(out),
|
||||||
|
"rows": out,
|
||||||
|
}
|
||||||
|
writeJSON(w, http.StatusOK, resp)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (s *Server) handleHealth(w http.ResponseWriter, r *http.Request) {
|
||||||
|
writeJSON(w, http.StatusOK, map[string]string{
|
||||||
|
"status": "UP",
|
||||||
|
"message": "Service is running",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- helpers ---
|
||||||
|
|
||||||
|
func withJSONHeaders(next http.Handler) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeJSON(w http.ResponseWriter, status int, v any) {
|
||||||
|
w.WriteHeader(status)
|
||||||
|
enc := json.NewEncoder(w)
|
||||||
|
enc.SetEscapeHTML(false)
|
||||||
|
_ = enc.Encode(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseIntQuery(r *http.Request, key string, def int) int {
|
||||||
|
raw := r.URL.Query().Get(key)
|
||||||
|
if raw == "" {
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
n, err := strconv.Atoi(raw)
|
||||||
|
if err != nil {
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustGetEnv(key string) string {
|
||||||
|
v := getenv(key)
|
||||||
|
if v == "" {
|
||||||
|
log.Fatalf("missing env var: %s", key)
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
var getenv = os.Getenv
|
||||||
|
|
||||||
|
func addDSNParam(dsn, key, value string) string {
|
||||||
|
u, err := url.Parse(dsn)
|
||||||
|
if err != nil {
|
||||||
|
return dsn
|
||||||
|
}
|
||||||
|
q := u.Query()
|
||||||
|
if q.Get(key) == "" {
|
||||||
|
q.Set(key, value)
|
||||||
|
}
|
||||||
|
u.RawQuery = q.Encode()
|
||||||
|
return u.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// normalizeSQLTypes makes JSON output nicer for common DB driver types
|
||||||
|
// (e.g., []byte -> string, time.Time stays time.Time which json encodes as RFC3339).
|
||||||
|
func normalizeSQLTypes(rows []map[string]any) []map[string]any {
|
||||||
|
out := make([]map[string]any, 0, len(rows))
|
||||||
|
for _, row := range rows {
|
||||||
|
nrow := make(map[string]any, len(row))
|
||||||
|
for k, v := range row {
|
||||||
|
switch t := v.(type) {
|
||||||
|
case []byte:
|
||||||
|
nrow[k] = string(t)
|
||||||
|
case time.Time:
|
||||||
|
nrow[k] = t
|
||||||
|
default:
|
||||||
|
nrow[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out = append(out, nrow)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user