works with browser

This commit is contained in:
Stas Kelvich
2023-04-15 02:18:52 +03:00
parent 1a3a7f14dd
commit 1233923c3a
3 changed files with 11 additions and 9 deletions
+1 -1
View File
@@ -55,7 +55,7 @@ hmac = "0.12.1"
hostname = "0.3.1"
humantime = "2.1"
humantime-serde = "1.1.1"
hyper = "0.14"
hyper = { version = "0.14", features = ["http2", "tcp", "runtime", "http1"]}
hyper-tungstenite = "0.9"
itertools = "0.10"
jsonwebtoken = "8"
+1 -1
View File
@@ -23,7 +23,7 @@ hmac.workspace = true
hostname.workspace = true
humantime.workspace = true
hyper-tungstenite.workspace = true
hyper.workspace = true
hyper = { workspace = true, features = ["http2", "http1", "tcp", "runtime"] }
itertools.workspace = true
md5.workspace = true
metrics.workspace = true
+9 -7
View File
@@ -15,6 +15,7 @@ use hyper_tungstenite::{tungstenite::Message, HyperWebsocket, WebSocketStream};
use pin_project_lite::pin_project;
use pq_proto::StartupMessageParams;
use percent_encoding::percent_decode;
use std::{
convert::Infallible,
future::ready,
@@ -31,7 +32,6 @@ use tokio_util::sync::CancellationToken;
use tracing::{error, info, info_span, warn, Instrument};
use url::form_urlencoded;
use utils::http::{error::ApiError, json::json_response};
use percent_encoding::percent_decode;
// TODO: use `std::sync::Exclusive` once it's stabilized.
// Tracking issue: https://github.com/rust-lang/rust/issues/98407.
@@ -190,7 +190,13 @@ async fn ws_handler(
Ok(response)
} else if request.uri().path() == "/sql" {
match handle_sql(config, request).await {
Ok(resp) => json_response(StatusCode::OK, resp),
Ok(resp) => json_response(StatusCode::OK, resp).map(|mut r| {
r.headers_mut().insert(
"Access-Control-Allow-Origin",
hyper::http::HeaderValue::from_static("*"),
);
r
}),
Err(e) => json_response(StatusCode::BAD_REQUEST, format!("error: {e:?}")),
}
} else {
@@ -272,11 +278,7 @@ async fn handle_sql(
info!("!!!! connecting to: {}", conn_string);
let (client, connection) = tokio_postgres::connect(
conn_string,
tokio_postgres::NoTls,
)
.await?;
let (client, connection) = tokio_postgres::connect(conn_string, tokio_postgres::NoTls).await?;
tokio::spawn(async move {
if let Err(e) = connection.await {