Merge pull request #918 from zed-industries/tokio
Switch collab server to Tokio/Axum
This commit is contained in:
Generated
+351
-286
File diff suppressed because it is too large
Load Diff
@@ -11,7 +11,7 @@ use async_tungstenite::tungstenite::{
|
|||||||
error::Error as WebsocketError,
|
error::Error as WebsocketError,
|
||||||
http::{Request, StatusCode},
|
http::{Request, StatusCode},
|
||||||
};
|
};
|
||||||
use futures::{future::LocalBoxFuture, FutureExt, StreamExt};
|
use futures::{future::LocalBoxFuture, FutureExt, SinkExt, StreamExt, TryStreamExt};
|
||||||
use gpui::{
|
use gpui::{
|
||||||
actions, AnyModelHandle, AnyViewHandle, AnyWeakModelHandle, AnyWeakViewHandle, AsyncAppContext,
|
actions, AnyModelHandle, AnyViewHandle, AnyWeakModelHandle, AnyWeakViewHandle, AsyncAppContext,
|
||||||
Entity, ModelContext, ModelHandle, MutableAppContext, Task, View, ViewContext, ViewHandle,
|
Entity, ModelContext, ModelHandle, MutableAppContext, Task, View, ViewContext, ViewHandle,
|
||||||
@@ -774,7 +774,7 @@ impl Client {
|
|||||||
"Authorization",
|
"Authorization",
|
||||||
format!("{} {}", credentials.user_id, credentials.access_token),
|
format!("{} {}", credentials.user_id, credentials.access_token),
|
||||||
)
|
)
|
||||||
.header("X-Zed-Protocol-Version", rpc::PROTOCOL_VERSION);
|
.header("x-zed-protocol-version", rpc::PROTOCOL_VERSION);
|
||||||
|
|
||||||
let http = self.http.clone();
|
let http = self.http.clone();
|
||||||
cx.background().spawn(async move {
|
cx.background().spawn(async move {
|
||||||
@@ -817,13 +817,21 @@ impl Client {
|
|||||||
let request = request.uri(rpc_url.as_str()).body(())?;
|
let request = request.uri(rpc_url.as_str()).body(())?;
|
||||||
let (stream, _) =
|
let (stream, _) =
|
||||||
async_tungstenite::async_tls::client_async_tls(request, stream).await?;
|
async_tungstenite::async_tls::client_async_tls(request, stream).await?;
|
||||||
Ok(Connection::new(stream))
|
Ok(Connection::new(
|
||||||
|
stream
|
||||||
|
.map_err(|error| anyhow!(error))
|
||||||
|
.sink_map_err(|error| anyhow!(error)),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
"http" => {
|
"http" => {
|
||||||
rpc_url.set_scheme("ws").unwrap();
|
rpc_url.set_scheme("ws").unwrap();
|
||||||
let request = request.uri(rpc_url.as_str()).body(())?;
|
let request = request.uri(rpc_url.as_str()).body(())?;
|
||||||
let (stream, _) = async_tungstenite::client_async(request, stream).await?;
|
let (stream, _) = async_tungstenite::client_async(request, stream).await?;
|
||||||
Ok(Connection::new(stream))
|
Ok(Connection::new(
|
||||||
|
stream
|
||||||
|
.map_err(|error| anyhow!(error))
|
||||||
|
.sink_map_err(|error| anyhow!(error)),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
_ => Err(anyhow!("invalid rpc url: {}", rpc_url))?,
|
_ => Err(anyhow!("invalid rpc url: {}", rpc_url))?,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,15 +16,17 @@ required-features = ["seed-support"]
|
|||||||
collections = { path = "../collections" }
|
collections = { path = "../collections" }
|
||||||
rpc = { path = "../rpc" }
|
rpc = { path = "../rpc" }
|
||||||
util = { path = "../util" }
|
util = { path = "../util" }
|
||||||
|
|
||||||
anyhow = "1.0.40"
|
anyhow = "1.0.40"
|
||||||
async-io = "1.3"
|
|
||||||
async-std = { version = "1.8.0", features = ["attributes"] }
|
|
||||||
async-trait = "0.1.50"
|
async-trait = "0.1.50"
|
||||||
async-tungstenite = "0.16"
|
async-tungstenite = "0.16"
|
||||||
|
axum = { version = "0.5", features = ["json", "headers", "ws"] }
|
||||||
base64 = "0.13"
|
base64 = "0.13"
|
||||||
envy = "0.4.2"
|
envy = "0.4.2"
|
||||||
|
env_logger = "0.8"
|
||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
json_env_logger = "0.1"
|
json_env_logger = "0.1"
|
||||||
|
lazy_static = "1.4"
|
||||||
lipsum = { version = "0.8", optional = true }
|
lipsum = { version = "0.8", optional = true }
|
||||||
log = { version = "0.4.16", features = ["kv_unstable_serde"] }
|
log = { version = "0.4.16", features = ["kv_unstable_serde"] }
|
||||||
parking_lot = "0.11.1"
|
parking_lot = "0.11.1"
|
||||||
@@ -33,9 +35,9 @@ scrypt = "0.7"
|
|||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
sha-1 = "0.9"
|
sha-1 = "0.9"
|
||||||
surf = "2.2.0"
|
tokio = { version = "1", features = ["full"] }
|
||||||
tide = "0.16.0"
|
tokio-tungstenite = "0.17"
|
||||||
tide-compress = "0.9.0"
|
tower = "0.4"
|
||||||
time = "0.2"
|
time = "0.2"
|
||||||
toml = "0.5.8"
|
toml = "0.5.8"
|
||||||
|
|
||||||
|
|||||||
+157
-152
@@ -1,179 +1,184 @@
|
|||||||
use crate::{auth, db::UserId, AppState, Request, RequestExt as _};
|
use crate::{
|
||||||
use async_trait::async_trait;
|
auth,
|
||||||
use serde::Deserialize;
|
db::{User, UserId},
|
||||||
use serde_json::json;
|
AppState, Error, Result,
|
||||||
|
};
|
||||||
|
use anyhow::anyhow;
|
||||||
|
use axum::{
|
||||||
|
body::Body,
|
||||||
|
extract::{Path, Query},
|
||||||
|
http::{self, Request, StatusCode},
|
||||||
|
middleware::{self, Next},
|
||||||
|
response::IntoResponse,
|
||||||
|
routing::{get, post, put},
|
||||||
|
Extension, Json, Router,
|
||||||
|
};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use surf::StatusCode;
|
use tower::ServiceBuilder;
|
||||||
|
|
||||||
pub fn add_routes(app: &mut tide::Server<Arc<AppState>>) {
|
pub fn routes(state: Arc<AppState>) -> Router<Body> {
|
||||||
app.at("/users").get(get_users);
|
Router::new()
|
||||||
app.at("/users").post(create_user);
|
.route("/users", get(get_users).post(create_user))
|
||||||
app.at("/users/:id").put(update_user);
|
.route(
|
||||||
app.at("/users/:id").delete(destroy_user);
|
"/users/:id",
|
||||||
app.at("/users/:github_login").get(get_user);
|
put(update_user).delete(destroy_user).get(get_user),
|
||||||
app.at("/users/:github_login/access_tokens")
|
)
|
||||||
.post(create_access_token);
|
.route("/users/:id/access_tokens", post(create_access_token))
|
||||||
|
.layer(
|
||||||
|
ServiceBuilder::new()
|
||||||
|
.layer(Extension(state))
|
||||||
|
.layer(middleware::from_fn(validate_api_token)),
|
||||||
|
)
|
||||||
|
// TODO: Compression on API routes?
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_user(request: Request) -> tide::Result {
|
pub async fn validate_api_token<B>(req: Request<B>, next: Next<B>) -> impl IntoResponse {
|
||||||
request.require_token().await?;
|
let token = req
|
||||||
|
.headers()
|
||||||
|
.get(http::header::AUTHORIZATION)
|
||||||
|
.and_then(|header| header.to_str().ok())
|
||||||
|
.ok_or_else(|| {
|
||||||
|
Error::Http(
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
"missing authorization header".to_string(),
|
||||||
|
)
|
||||||
|
})?
|
||||||
|
.strip_prefix("token ")
|
||||||
|
.ok_or_else(|| {
|
||||||
|
Error::Http(
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
"invalid authorization header".to_string(),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
let user = request
|
let state = req.extensions().get::<Arc<AppState>>().unwrap();
|
||||||
.db()
|
|
||||||
.get_user_by_github_login(request.param("github_login")?)
|
|
||||||
.await?
|
|
||||||
.ok_or_else(|| surf::Error::from_str(404, "user not found"))?;
|
|
||||||
|
|
||||||
Ok(tide::Response::builder(StatusCode::Ok)
|
if token != state.api_token {
|
||||||
.body(tide::Body::from_json(&user)?)
|
Err(Error::Http(
|
||||||
.build())
|
StatusCode::UNAUTHORIZED,
|
||||||
}
|
"invalid authorization token".to_string(),
|
||||||
|
))?
|
||||||
async fn get_users(request: Request) -> tide::Result {
|
|
||||||
request.require_token().await?;
|
|
||||||
|
|
||||||
let users = request.db().get_all_users().await?;
|
|
||||||
|
|
||||||
Ok(tide::Response::builder(StatusCode::Ok)
|
|
||||||
.body(tide::Body::from_json(&users)?)
|
|
||||||
.build())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn create_user(mut request: Request) -> tide::Result {
|
|
||||||
request.require_token().await?;
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
struct Params {
|
|
||||||
github_login: String,
|
|
||||||
admin: bool,
|
|
||||||
}
|
}
|
||||||
let params = request.body_json::<Params>().await?;
|
|
||||||
|
|
||||||
let user_id = request
|
Ok::<_, Error>(next.run(req).await)
|
||||||
.db()
|
}
|
||||||
|
|
||||||
|
async fn get_users(Extension(app): Extension<Arc<AppState>>) -> Result<Json<Vec<User>>> {
|
||||||
|
let users = app.db.get_all_users().await?;
|
||||||
|
Ok(Json(users))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct CreateUserParams {
|
||||||
|
github_login: String,
|
||||||
|
admin: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn create_user(
|
||||||
|
Json(params): Json<CreateUserParams>,
|
||||||
|
Extension(app): Extension<Arc<AppState>>,
|
||||||
|
) -> Result<Json<User>> {
|
||||||
|
let user_id = app
|
||||||
|
.db
|
||||||
.create_user(¶ms.github_login, params.admin)
|
.create_user(¶ms.github_login, params.admin)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let user = request.db().get_user_by_id(user_id).await?.ok_or_else(|| {
|
let user = app
|
||||||
surf::Error::from_str(
|
.db
|
||||||
StatusCode::InternalServerError,
|
.get_user_by_id(user_id)
|
||||||
"couldn't find the user we just created",
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
|
|
||||||
Ok(tide::Response::builder(StatusCode::Ok)
|
|
||||||
.body(tide::Body::from_json(&user)?)
|
|
||||||
.build())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn update_user(mut request: Request) -> tide::Result {
|
|
||||||
request.require_token().await?;
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
struct Params {
|
|
||||||
admin: bool,
|
|
||||||
}
|
|
||||||
let user_id = UserId(
|
|
||||||
request
|
|
||||||
.param("id")?
|
|
||||||
.parse::<i32>()
|
|
||||||
.map_err(|error| surf::Error::from_str(StatusCode::BadRequest, error.to_string()))?,
|
|
||||||
);
|
|
||||||
let params = request.body_json::<Params>().await?;
|
|
||||||
|
|
||||||
request
|
|
||||||
.db()
|
|
||||||
.set_user_is_admin(user_id, params.admin)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(tide::Response::builder(StatusCode::Ok).build())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn destroy_user(request: Request) -> tide::Result {
|
|
||||||
request.require_token().await?;
|
|
||||||
let user_id = UserId(
|
|
||||||
request
|
|
||||||
.param("id")?
|
|
||||||
.parse::<i32>()
|
|
||||||
.map_err(|error| surf::Error::from_str(StatusCode::BadRequest, error.to_string()))?,
|
|
||||||
);
|
|
||||||
|
|
||||||
request.db().destroy_user(user_id).await?;
|
|
||||||
|
|
||||||
Ok(tide::Response::builder(StatusCode::Ok).build())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn create_access_token(request: Request) -> tide::Result {
|
|
||||||
request.require_token().await?;
|
|
||||||
|
|
||||||
let user = request
|
|
||||||
.db()
|
|
||||||
.get_user_by_github_login(request.param("github_login")?)
|
|
||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| surf::Error::from_str(StatusCode::NotFound, "user not found"))?;
|
.ok_or_else(|| anyhow!("couldn't find the user we just created"))?;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
Ok(Json(user))
|
||||||
struct QueryParams {
|
}
|
||||||
public_key: String,
|
|
||||||
impersonate: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
let query_params: QueryParams = request.query().map_err(|_| {
|
#[derive(Deserialize)]
|
||||||
surf::Error::from_str(StatusCode::UnprocessableEntity, "invalid query params")
|
struct UpdateUserParams {
|
||||||
})?;
|
admin: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update_user(
|
||||||
|
Path(user_id): Path<i32>,
|
||||||
|
Json(params): Json<UpdateUserParams>,
|
||||||
|
Extension(app): Extension<Arc<AppState>>,
|
||||||
|
) -> Result<()> {
|
||||||
|
app.db
|
||||||
|
.set_user_is_admin(UserId(user_id), params.admin)
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn destroy_user(
|
||||||
|
Path(user_id): Path<i32>,
|
||||||
|
Extension(app): Extension<Arc<AppState>>,
|
||||||
|
) -> Result<()> {
|
||||||
|
app.db.destroy_user(UserId(user_id)).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_user(
|
||||||
|
Path(login): Path<String>,
|
||||||
|
Extension(app): Extension<Arc<AppState>>,
|
||||||
|
) -> Result<Json<User>> {
|
||||||
|
let user = app
|
||||||
|
.db
|
||||||
|
.get_user_by_github_login(&login)
|
||||||
|
.await?
|
||||||
|
.ok_or_else(|| anyhow!("user not found"))?;
|
||||||
|
Ok(Json(user))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct CreateAccessTokenQueryParams {
|
||||||
|
public_key: String,
|
||||||
|
impersonate: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct CreateAccessTokenResponse {
|
||||||
|
user_id: UserId,
|
||||||
|
encrypted_access_token: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn create_access_token(
|
||||||
|
Path(login): Path<String>,
|
||||||
|
Query(params): Query<CreateAccessTokenQueryParams>,
|
||||||
|
Extension(app): Extension<Arc<AppState>>,
|
||||||
|
) -> Result<Json<CreateAccessTokenResponse>> {
|
||||||
|
// request.require_token().await?;
|
||||||
|
|
||||||
|
let user = app
|
||||||
|
.db
|
||||||
|
.get_user_by_github_login(&login)
|
||||||
|
.await?
|
||||||
|
.ok_or_else(|| anyhow!("user not found"))?;
|
||||||
|
|
||||||
let mut user_id = user.id;
|
let mut user_id = user.id;
|
||||||
if let Some(impersonate) = query_params.impersonate {
|
if let Some(impersonate) = params.impersonate {
|
||||||
if user.admin {
|
if user.admin {
|
||||||
if let Some(impersonated_user) =
|
if let Some(impersonated_user) = app.db.get_user_by_github_login(&impersonate).await? {
|
||||||
request.db().get_user_by_github_login(&impersonate).await?
|
|
||||||
{
|
|
||||||
user_id = impersonated_user.id;
|
user_id = impersonated_user.id;
|
||||||
} else {
|
} else {
|
||||||
return Ok(tide::Response::builder(StatusCode::UnprocessableEntity)
|
return Err(Error::Http(
|
||||||
.body(format!(
|
StatusCode::UNPROCESSABLE_ENTITY,
|
||||||
"Can't impersonate non-existent user {}",
|
format!("user {impersonate} does not exist"),
|
||||||
impersonate
|
));
|
||||||
))
|
|
||||||
.build());
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return Ok(tide::Response::builder(StatusCode::Unauthorized)
|
return Err(Error::Http(
|
||||||
.body(format!(
|
StatusCode::UNAUTHORIZED,
|
||||||
"Can't impersonate user {} because the real user isn't an admin",
|
format!("you do not have permission to impersonate other users"),
|
||||||
impersonate
|
));
|
||||||
))
|
|
||||||
.build());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let access_token = auth::create_access_token(request.db().as_ref(), user_id).await?;
|
let access_token = auth::create_access_token(app.db.as_ref(), user_id).await?;
|
||||||
let encrypted_access_token =
|
let encrypted_access_token =
|
||||||
auth::encrypt_access_token(&access_token, query_params.public_key.clone())?;
|
auth::encrypt_access_token(&access_token, params.public_key.clone())?;
|
||||||
|
|
||||||
Ok(tide::Response::builder(StatusCode::Ok)
|
Ok(Json(CreateAccessTokenResponse {
|
||||||
.body(json!({"user_id": user_id, "encrypted_access_token": encrypted_access_token}))
|
user_id,
|
||||||
.build())
|
encrypted_access_token,
|
||||||
}
|
}))
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
pub trait RequestExt {
|
|
||||||
async fn require_token(&self) -> tide::Result<()>;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
impl RequestExt for Request {
|
|
||||||
async fn require_token(&self) -> tide::Result<()> {
|
|
||||||
let token = self
|
|
||||||
.header("Authorization")
|
|
||||||
.and_then(|header| header.get(0))
|
|
||||||
.and_then(|header| header.as_str().strip_prefix("token "))
|
|
||||||
.ok_or_else(|| surf::Error::from_str(403, "invalid authorization header"))?;
|
|
||||||
|
|
||||||
if token == self.state().config.api_token {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(tide::Error::from_str(403, "invalid authorization token"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+40
-37
@@ -1,45 +1,47 @@
|
|||||||
use super::{
|
use std::sync::Arc;
|
||||||
db::{self, UserId},
|
|
||||||
errors::TideResultExt,
|
use super::db::{self, UserId};
|
||||||
|
use crate::{AppState, Error};
|
||||||
|
use anyhow::{Context, Result};
|
||||||
|
use axum::{
|
||||||
|
http::{self, Request, StatusCode},
|
||||||
|
middleware::Next,
|
||||||
|
response::IntoResponse,
|
||||||
};
|
};
|
||||||
use crate::Request;
|
|
||||||
use anyhow::{anyhow, Context};
|
|
||||||
use rand::thread_rng;
|
use rand::thread_rng;
|
||||||
use rpc::auth as zed_auth;
|
|
||||||
use scrypt::{
|
use scrypt::{
|
||||||
password_hash::{PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
|
password_hash::{PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
|
||||||
Scrypt,
|
Scrypt,
|
||||||
};
|
};
|
||||||
use std::convert::TryFrom;
|
|
||||||
use surf::StatusCode;
|
|
||||||
use tide::Error;
|
|
||||||
|
|
||||||
pub async fn process_auth_header(request: &Request) -> tide::Result<UserId> {
|
pub async fn validate_header<B>(mut req: Request<B>, next: Next<B>) -> impl IntoResponse {
|
||||||
let mut auth_header = request
|
let mut auth_header = req
|
||||||
.header("Authorization")
|
.headers()
|
||||||
|
.get(http::header::AUTHORIZATION)
|
||||||
|
.and_then(|header| header.to_str().ok())
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
Error::new(
|
Error::Http(
|
||||||
StatusCode::BadRequest,
|
StatusCode::BAD_REQUEST,
|
||||||
anyhow!("missing authorization header"),
|
"missing authorization header".to_string(),
|
||||||
)
|
)
|
||||||
})?
|
})?
|
||||||
.last()
|
|
||||||
.as_str()
|
|
||||||
.split_whitespace();
|
.split_whitespace();
|
||||||
|
|
||||||
let user_id = UserId(auth_header.next().unwrap_or("").parse().map_err(|_| {
|
let user_id = UserId(auth_header.next().unwrap_or("").parse().map_err(|_| {
|
||||||
Error::new(
|
Error::Http(
|
||||||
StatusCode::BadRequest,
|
StatusCode::BAD_REQUEST,
|
||||||
anyhow!("missing user id in authorization header"),
|
"missing user id in authorization header".to_string(),
|
||||||
)
|
)
|
||||||
})?);
|
})?);
|
||||||
|
|
||||||
let access_token = auth_header.next().ok_or_else(|| {
|
let access_token = auth_header.next().ok_or_else(|| {
|
||||||
Error::new(
|
Error::Http(
|
||||||
StatusCode::BadRequest,
|
StatusCode::BAD_REQUEST,
|
||||||
anyhow!("missing access token in authorization header"),
|
"missing access token in authorization header".to_string(),
|
||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let state = request.state().clone();
|
let state = req.extensions().get::<Arc<AppState>>().unwrap();
|
||||||
let mut credentials_valid = false;
|
let mut credentials_valid = false;
|
||||||
for password_hash in state.db.get_access_token_hashes(user_id).await? {
|
for password_hash in state.db.get_access_token_hashes(user_id).await? {
|
||||||
if verify_access_token(&access_token, &password_hash)? {
|
if verify_access_token(&access_token, &password_hash)? {
|
||||||
@@ -48,20 +50,21 @@ pub async fn process_auth_header(request: &Request) -> tide::Result<UserId> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !credentials_valid {
|
if credentials_valid {
|
||||||
Err(Error::new(
|
req.extensions_mut().insert(user_id);
|
||||||
StatusCode::Unauthorized,
|
Ok::<_, Error>(next.run(req).await)
|
||||||
anyhow!("invalid credentials"),
|
} else {
|
||||||
))?;
|
Err(Error::Http(
|
||||||
|
StatusCode::UNAUTHORIZED,
|
||||||
|
"invalid credentials".to_string(),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(user_id)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const MAX_ACCESS_TOKENS_TO_STORE: usize = 8;
|
const MAX_ACCESS_TOKENS_TO_STORE: usize = 8;
|
||||||
|
|
||||||
pub async fn create_access_token(db: &dyn db::Db, user_id: UserId) -> tide::Result<String> {
|
pub async fn create_access_token(db: &dyn db::Db, user_id: UserId) -> Result<String> {
|
||||||
let access_token = zed_auth::random_token();
|
let access_token = rpc::auth::random_token();
|
||||||
let access_token_hash =
|
let access_token_hash =
|
||||||
hash_access_token(&access_token).context("failed to hash access token")?;
|
hash_access_token(&access_token).context("failed to hash access token")?;
|
||||||
db.create_access_token_hash(user_id, &access_token_hash, MAX_ACCESS_TOKENS_TO_STORE)
|
db.create_access_token_hash(user_id, &access_token_hash, MAX_ACCESS_TOKENS_TO_STORE)
|
||||||
@@ -69,7 +72,7 @@ pub async fn create_access_token(db: &dyn db::Db, user_id: UserId) -> tide::Resu
|
|||||||
Ok(access_token)
|
Ok(access_token)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hash_access_token(token: &str) -> tide::Result<String> {
|
fn hash_access_token(token: &str) -> Result<String> {
|
||||||
// Avoid slow hashing in debug mode.
|
// Avoid slow hashing in debug mode.
|
||||||
let params = if cfg!(debug_assertions) {
|
let params = if cfg!(debug_assertions) {
|
||||||
scrypt::Params::new(1, 1, 1).unwrap()
|
scrypt::Params::new(1, 1, 1).unwrap()
|
||||||
@@ -87,16 +90,16 @@ fn hash_access_token(token: &str) -> tide::Result<String> {
|
|||||||
.to_string())
|
.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn encrypt_access_token(access_token: &str, public_key: String) -> tide::Result<String> {
|
pub fn encrypt_access_token(access_token: &str, public_key: String) -> Result<String> {
|
||||||
let native_app_public_key =
|
let native_app_public_key =
|
||||||
zed_auth::PublicKey::try_from(public_key).context("failed to parse app public key")?;
|
rpc::auth::PublicKey::try_from(public_key).context("failed to parse app public key")?;
|
||||||
let encrypted_access_token = native_app_public_key
|
let encrypted_access_token = native_app_public_key
|
||||||
.encrypt_string(&access_token)
|
.encrypt_string(&access_token)
|
||||||
.context("failed to encrypt access token with public key")?;
|
.context("failed to encrypt access token with public key")?;
|
||||||
Ok(encrypted_access_token)
|
Ok(encrypted_access_token)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn verify_access_token(token: &str, hash: &str) -> tide::Result<bool> {
|
pub fn verify_access_token(token: &str, hash: &str) -> Result<bool> {
|
||||||
let hash = PasswordHash::new(hash)?;
|
let hash = PasswordHash::new(hash)?;
|
||||||
Ok(Scrypt.verify_password(token.as_bytes(), &hash).is_ok())
|
Ok(Scrypt.verify_password(token.as_bytes(), &hash).is_ok())
|
||||||
}
|
}
|
||||||
|
|||||||
+142
-200
@@ -1,26 +1,11 @@
|
|||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use async_std::task::{block_on, yield_now};
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
pub use sqlx::postgres::PgPoolOptions as DbOptions;
|
pub use sqlx::postgres::PgPoolOptions as DbOptions;
|
||||||
use sqlx::{types::Uuid, FromRow};
|
use sqlx::{types::Uuid, FromRow};
|
||||||
use time::OffsetDateTime;
|
use time::OffsetDateTime;
|
||||||
|
|
||||||
macro_rules! test_support {
|
|
||||||
($self:ident, { $($token:tt)* }) => {{
|
|
||||||
let body = async {
|
|
||||||
$($token)*
|
|
||||||
};
|
|
||||||
if $self.test_mode {
|
|
||||||
yield_now().await;
|
|
||||||
block_on(body)
|
|
||||||
} else {
|
|
||||||
body.await
|
|
||||||
}
|
|
||||||
}};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait Db: Send + Sync {
|
pub trait Db: Send + Sync {
|
||||||
async fn create_user(&self, github_login: &str, admin: bool) -> Result<UserId>;
|
async fn create_user(&self, github_login: &str, admin: bool) -> Result<UserId>;
|
||||||
@@ -77,20 +62,16 @@ pub trait Db: Send + Sync {
|
|||||||
|
|
||||||
pub struct PostgresDb {
|
pub struct PostgresDb {
|
||||||
pool: sqlx::PgPool,
|
pool: sqlx::PgPool,
|
||||||
test_mode: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PostgresDb {
|
impl PostgresDb {
|
||||||
pub async fn new(url: &str, max_connections: u32) -> tide::Result<Self> {
|
pub async fn new(url: &str, max_connections: u32) -> Result<Self> {
|
||||||
let pool = DbOptions::new()
|
let pool = DbOptions::new()
|
||||||
.max_connections(max_connections)
|
.max_connections(max_connections)
|
||||||
.connect(url)
|
.connect(url)
|
||||||
.await
|
.await
|
||||||
.context("failed to connect to postgres database")?;
|
.context("failed to connect to postgres database")?;
|
||||||
Ok(Self {
|
Ok(Self { pool })
|
||||||
pool,
|
|
||||||
test_mode: false,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,27 +80,23 @@ impl Db for PostgresDb {
|
|||||||
// users
|
// users
|
||||||
|
|
||||||
async fn create_user(&self, github_login: &str, admin: bool) -> Result<UserId> {
|
async fn create_user(&self, github_login: &str, admin: bool) -> Result<UserId> {
|
||||||
test_support!(self, {
|
let query = "
|
||||||
let query = "
|
|
||||||
INSERT INTO users (github_login, admin)
|
INSERT INTO users (github_login, admin)
|
||||||
VALUES ($1, $2)
|
VALUES ($1, $2)
|
||||||
ON CONFLICT (github_login) DO UPDATE SET github_login = excluded.github_login
|
ON CONFLICT (github_login) DO UPDATE SET github_login = excluded.github_login
|
||||||
RETURNING id
|
RETURNING id
|
||||||
";
|
";
|
||||||
Ok(sqlx::query_scalar(query)
|
Ok(sqlx::query_scalar(query)
|
||||||
.bind(github_login)
|
.bind(github_login)
|
||||||
.bind(admin)
|
.bind(admin)
|
||||||
.fetch_one(&self.pool)
|
.fetch_one(&self.pool)
|
||||||
.await
|
.await
|
||||||
.map(UserId)?)
|
.map(UserId)?)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_all_users(&self) -> Result<Vec<User>> {
|
async fn get_all_users(&self) -> Result<Vec<User>> {
|
||||||
test_support!(self, {
|
let query = "SELECT * FROM users ORDER BY github_login ASC";
|
||||||
let query = "SELECT * FROM users ORDER BY github_login ASC";
|
Ok(sqlx::query_as(query).fetch_all(&self.pool).await?)
|
||||||
Ok(sqlx::query_as(query).fetch_all(&self.pool).await?)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_user_by_id(&self, id: UserId) -> Result<Option<User>> {
|
async fn get_user_by_id(&self, id: UserId) -> Result<Option<User>> {
|
||||||
@@ -129,57 +106,49 @@ impl Db for PostgresDb {
|
|||||||
|
|
||||||
async fn get_users_by_ids(&self, ids: Vec<UserId>) -> Result<Vec<User>> {
|
async fn get_users_by_ids(&self, ids: Vec<UserId>) -> Result<Vec<User>> {
|
||||||
let ids = ids.into_iter().map(|id| id.0).collect::<Vec<_>>();
|
let ids = ids.into_iter().map(|id| id.0).collect::<Vec<_>>();
|
||||||
test_support!(self, {
|
let query = "
|
||||||
let query = "
|
|
||||||
SELECT users.*
|
SELECT users.*
|
||||||
FROM users
|
FROM users
|
||||||
WHERE users.id = ANY ($1)
|
WHERE users.id = ANY ($1)
|
||||||
";
|
";
|
||||||
|
|
||||||
Ok(sqlx::query_as(query)
|
Ok(sqlx::query_as(query)
|
||||||
.bind(&ids)
|
.bind(&ids)
|
||||||
.fetch_all(&self.pool)
|
.fetch_all(&self.pool)
|
||||||
.await?)
|
.await?)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_user_by_github_login(&self, github_login: &str) -> Result<Option<User>> {
|
async fn get_user_by_github_login(&self, github_login: &str) -> Result<Option<User>> {
|
||||||
test_support!(self, {
|
let query = "SELECT * FROM users WHERE github_login = $1 LIMIT 1";
|
||||||
let query = "SELECT * FROM users WHERE github_login = $1 LIMIT 1";
|
Ok(sqlx::query_as(query)
|
||||||
Ok(sqlx::query_as(query)
|
.bind(github_login)
|
||||||
.bind(github_login)
|
.fetch_optional(&self.pool)
|
||||||
.fetch_optional(&self.pool)
|
.await?)
|
||||||
.await?)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn set_user_is_admin(&self, id: UserId, is_admin: bool) -> Result<()> {
|
async fn set_user_is_admin(&self, id: UserId, is_admin: bool) -> Result<()> {
|
||||||
test_support!(self, {
|
let query = "UPDATE users SET admin = $1 WHERE id = $2";
|
||||||
let query = "UPDATE users SET admin = $1 WHERE id = $2";
|
Ok(sqlx::query(query)
|
||||||
Ok(sqlx::query(query)
|
.bind(is_admin)
|
||||||
.bind(is_admin)
|
.bind(id.0)
|
||||||
.bind(id.0)
|
.execute(&self.pool)
|
||||||
.execute(&self.pool)
|
.await
|
||||||
.await
|
.map(drop)?)
|
||||||
.map(drop)?)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn destroy_user(&self, id: UserId) -> Result<()> {
|
async fn destroy_user(&self, id: UserId) -> Result<()> {
|
||||||
test_support!(self, {
|
let query = "DELETE FROM access_tokens WHERE user_id = $1;";
|
||||||
let query = "DELETE FROM access_tokens WHERE user_id = $1;";
|
sqlx::query(query)
|
||||||
sqlx::query(query)
|
.bind(id.0)
|
||||||
.bind(id.0)
|
.execute(&self.pool)
|
||||||
.execute(&self.pool)
|
.await
|
||||||
.await
|
.map(drop)?;
|
||||||
.map(drop)?;
|
let query = "DELETE FROM users WHERE id = $1;";
|
||||||
let query = "DELETE FROM users WHERE id = $1;";
|
Ok(sqlx::query(query)
|
||||||
Ok(sqlx::query(query)
|
.bind(id.0)
|
||||||
.bind(id.0)
|
.execute(&self.pool)
|
||||||
.execute(&self.pool)
|
.await
|
||||||
.await
|
.map(drop)?)
|
||||||
.map(drop)?)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// access tokens
|
// access tokens
|
||||||
@@ -190,12 +159,11 @@ impl Db for PostgresDb {
|
|||||||
access_token_hash: &str,
|
access_token_hash: &str,
|
||||||
max_access_token_count: usize,
|
max_access_token_count: usize,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
test_support!(self, {
|
let insert_query = "
|
||||||
let insert_query = "
|
|
||||||
INSERT INTO access_tokens (user_id, hash)
|
INSERT INTO access_tokens (user_id, hash)
|
||||||
VALUES ($1, $2);
|
VALUES ($1, $2);
|
||||||
";
|
";
|
||||||
let cleanup_query = "
|
let cleanup_query = "
|
||||||
DELETE FROM access_tokens
|
DELETE FROM access_tokens
|
||||||
WHERE id IN (
|
WHERE id IN (
|
||||||
SELECT id from access_tokens
|
SELECT id from access_tokens
|
||||||
@@ -205,35 +173,32 @@ impl Db for PostgresDb {
|
|||||||
)
|
)
|
||||||
";
|
";
|
||||||
|
|
||||||
let mut tx = self.pool.begin().await?;
|
let mut tx = self.pool.begin().await?;
|
||||||
sqlx::query(insert_query)
|
sqlx::query(insert_query)
|
||||||
.bind(user_id.0)
|
.bind(user_id.0)
|
||||||
.bind(access_token_hash)
|
.bind(access_token_hash)
|
||||||
.execute(&mut tx)
|
.execute(&mut tx)
|
||||||
.await?;
|
.await?;
|
||||||
sqlx::query(cleanup_query)
|
sqlx::query(cleanup_query)
|
||||||
.bind(user_id.0)
|
.bind(user_id.0)
|
||||||
.bind(access_token_hash)
|
.bind(access_token_hash)
|
||||||
.bind(max_access_token_count as u32)
|
.bind(max_access_token_count as u32)
|
||||||
.execute(&mut tx)
|
.execute(&mut tx)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(tx.commit().await?)
|
Ok(tx.commit().await?)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_access_token_hashes(&self, user_id: UserId) -> Result<Vec<String>> {
|
async fn get_access_token_hashes(&self, user_id: UserId) -> Result<Vec<String>> {
|
||||||
test_support!(self, {
|
let query = "
|
||||||
let query = "
|
|
||||||
SELECT hash
|
SELECT hash
|
||||||
FROM access_tokens
|
FROM access_tokens
|
||||||
WHERE user_id = $1
|
WHERE user_id = $1
|
||||||
ORDER BY id DESC
|
ORDER BY id DESC
|
||||||
";
|
";
|
||||||
Ok(sqlx::query_scalar(query)
|
Ok(sqlx::query_scalar(query)
|
||||||
.bind(user_id.0)
|
.bind(user_id.0)
|
||||||
.fetch_all(&self.pool)
|
.fetch_all(&self.pool)
|
||||||
.await?)
|
.await?)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// orgs
|
// orgs
|
||||||
@@ -241,94 +206,83 @@ impl Db for PostgresDb {
|
|||||||
#[allow(unused)] // Help rust-analyzer
|
#[allow(unused)] // Help rust-analyzer
|
||||||
#[cfg(any(test, feature = "seed-support"))]
|
#[cfg(any(test, feature = "seed-support"))]
|
||||||
async fn find_org_by_slug(&self, slug: &str) -> Result<Option<Org>> {
|
async fn find_org_by_slug(&self, slug: &str) -> Result<Option<Org>> {
|
||||||
test_support!(self, {
|
let query = "
|
||||||
let query = "
|
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM orgs
|
FROM orgs
|
||||||
WHERE slug = $1
|
WHERE slug = $1
|
||||||
";
|
";
|
||||||
Ok(sqlx::query_as(query)
|
Ok(sqlx::query_as(query)
|
||||||
.bind(slug)
|
.bind(slug)
|
||||||
.fetch_optional(&self.pool)
|
.fetch_optional(&self.pool)
|
||||||
.await?)
|
.await?)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(test, feature = "seed-support"))]
|
#[cfg(any(test, feature = "seed-support"))]
|
||||||
async fn create_org(&self, name: &str, slug: &str) -> Result<OrgId> {
|
async fn create_org(&self, name: &str, slug: &str) -> Result<OrgId> {
|
||||||
test_support!(self, {
|
let query = "
|
||||||
let query = "
|
|
||||||
INSERT INTO orgs (name, slug)
|
INSERT INTO orgs (name, slug)
|
||||||
VALUES ($1, $2)
|
VALUES ($1, $2)
|
||||||
RETURNING id
|
RETURNING id
|
||||||
";
|
";
|
||||||
Ok(sqlx::query_scalar(query)
|
Ok(sqlx::query_scalar(query)
|
||||||
.bind(name)
|
.bind(name)
|
||||||
.bind(slug)
|
.bind(slug)
|
||||||
.fetch_one(&self.pool)
|
.fetch_one(&self.pool)
|
||||||
.await
|
.await
|
||||||
.map(OrgId)?)
|
.map(OrgId)?)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(test, feature = "seed-support"))]
|
#[cfg(any(test, feature = "seed-support"))]
|
||||||
async fn add_org_member(&self, org_id: OrgId, user_id: UserId, is_admin: bool) -> Result<()> {
|
async fn add_org_member(&self, org_id: OrgId, user_id: UserId, is_admin: bool) -> Result<()> {
|
||||||
test_support!(self, {
|
let query = "
|
||||||
let query = "
|
|
||||||
INSERT INTO org_memberships (org_id, user_id, admin)
|
INSERT INTO org_memberships (org_id, user_id, admin)
|
||||||
VALUES ($1, $2, $3)
|
VALUES ($1, $2, $3)
|
||||||
ON CONFLICT DO NOTHING
|
ON CONFLICT DO NOTHING
|
||||||
";
|
";
|
||||||
Ok(sqlx::query(query)
|
Ok(sqlx::query(query)
|
||||||
.bind(org_id.0)
|
.bind(org_id.0)
|
||||||
.bind(user_id.0)
|
.bind(user_id.0)
|
||||||
.bind(is_admin)
|
.bind(is_admin)
|
||||||
.execute(&self.pool)
|
.execute(&self.pool)
|
||||||
.await
|
.await
|
||||||
.map(drop)?)
|
.map(drop)?)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// channels
|
// channels
|
||||||
|
|
||||||
#[cfg(any(test, feature = "seed-support"))]
|
#[cfg(any(test, feature = "seed-support"))]
|
||||||
async fn create_org_channel(&self, org_id: OrgId, name: &str) -> Result<ChannelId> {
|
async fn create_org_channel(&self, org_id: OrgId, name: &str) -> Result<ChannelId> {
|
||||||
test_support!(self, {
|
let query = "
|
||||||
let query = "
|
|
||||||
INSERT INTO channels (owner_id, owner_is_user, name)
|
INSERT INTO channels (owner_id, owner_is_user, name)
|
||||||
VALUES ($1, false, $2)
|
VALUES ($1, false, $2)
|
||||||
RETURNING id
|
RETURNING id
|
||||||
";
|
";
|
||||||
Ok(sqlx::query_scalar(query)
|
Ok(sqlx::query_scalar(query)
|
||||||
.bind(org_id.0)
|
.bind(org_id.0)
|
||||||
.bind(name)
|
.bind(name)
|
||||||
.fetch_one(&self.pool)
|
.fetch_one(&self.pool)
|
||||||
.await
|
.await
|
||||||
.map(ChannelId)?)
|
.map(ChannelId)?)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unused)] // Help rust-analyzer
|
#[allow(unused)] // Help rust-analyzer
|
||||||
#[cfg(any(test, feature = "seed-support"))]
|
#[cfg(any(test, feature = "seed-support"))]
|
||||||
async fn get_org_channels(&self, org_id: OrgId) -> Result<Vec<Channel>> {
|
async fn get_org_channels(&self, org_id: OrgId) -> Result<Vec<Channel>> {
|
||||||
test_support!(self, {
|
let query = "
|
||||||
let query = "
|
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM channels
|
FROM channels
|
||||||
WHERE
|
WHERE
|
||||||
channels.owner_is_user = false AND
|
channels.owner_is_user = false AND
|
||||||
channels.owner_id = $1
|
channels.owner_id = $1
|
||||||
";
|
";
|
||||||
Ok(sqlx::query_as(query)
|
Ok(sqlx::query_as(query)
|
||||||
.bind(org_id.0)
|
.bind(org_id.0)
|
||||||
.fetch_all(&self.pool)
|
.fetch_all(&self.pool)
|
||||||
.await?)
|
.await?)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_accessible_channels(&self, user_id: UserId) -> Result<Vec<Channel>> {
|
async fn get_accessible_channels(&self, user_id: UserId) -> Result<Vec<Channel>> {
|
||||||
test_support!(self, {
|
let query = "
|
||||||
let query = "
|
|
||||||
SELECT
|
SELECT
|
||||||
channels.*
|
channels.*
|
||||||
FROM
|
FROM
|
||||||
@@ -337,11 +291,10 @@ impl Db for PostgresDb {
|
|||||||
channel_memberships.user_id = $1 AND
|
channel_memberships.user_id = $1 AND
|
||||||
channel_memberships.channel_id = channels.id
|
channel_memberships.channel_id = channels.id
|
||||||
";
|
";
|
||||||
Ok(sqlx::query_as(query)
|
Ok(sqlx::query_as(query)
|
||||||
.bind(user_id.0)
|
.bind(user_id.0)
|
||||||
.fetch_all(&self.pool)
|
.fetch_all(&self.pool)
|
||||||
.await?)
|
.await?)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn can_user_access_channel(
|
async fn can_user_access_channel(
|
||||||
@@ -349,20 +302,18 @@ impl Db for PostgresDb {
|
|||||||
user_id: UserId,
|
user_id: UserId,
|
||||||
channel_id: ChannelId,
|
channel_id: ChannelId,
|
||||||
) -> Result<bool> {
|
) -> Result<bool> {
|
||||||
test_support!(self, {
|
let query = "
|
||||||
let query = "
|
|
||||||
SELECT id
|
SELECT id
|
||||||
FROM channel_memberships
|
FROM channel_memberships
|
||||||
WHERE user_id = $1 AND channel_id = $2
|
WHERE user_id = $1 AND channel_id = $2
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
";
|
";
|
||||||
Ok(sqlx::query_scalar::<_, i32>(query)
|
Ok(sqlx::query_scalar::<_, i32>(query)
|
||||||
.bind(user_id.0)
|
.bind(user_id.0)
|
||||||
.bind(channel_id.0)
|
.bind(channel_id.0)
|
||||||
.fetch_optional(&self.pool)
|
.fetch_optional(&self.pool)
|
||||||
.await
|
.await
|
||||||
.map(|e| e.is_some())?)
|
.map(|e| e.is_some())?)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(test, feature = "seed-support"))]
|
#[cfg(any(test, feature = "seed-support"))]
|
||||||
@@ -372,20 +323,18 @@ impl Db for PostgresDb {
|
|||||||
user_id: UserId,
|
user_id: UserId,
|
||||||
is_admin: bool,
|
is_admin: bool,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
test_support!(self, {
|
let query = "
|
||||||
let query = "
|
|
||||||
INSERT INTO channel_memberships (channel_id, user_id, admin)
|
INSERT INTO channel_memberships (channel_id, user_id, admin)
|
||||||
VALUES ($1, $2, $3)
|
VALUES ($1, $2, $3)
|
||||||
ON CONFLICT DO NOTHING
|
ON CONFLICT DO NOTHING
|
||||||
";
|
";
|
||||||
Ok(sqlx::query(query)
|
Ok(sqlx::query(query)
|
||||||
.bind(channel_id.0)
|
.bind(channel_id.0)
|
||||||
.bind(user_id.0)
|
.bind(user_id.0)
|
||||||
.bind(is_admin)
|
.bind(is_admin)
|
||||||
.execute(&self.pool)
|
.execute(&self.pool)
|
||||||
.await
|
.await
|
||||||
.map(drop)?)
|
.map(drop)?)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// messages
|
// messages
|
||||||
@@ -398,23 +347,21 @@ impl Db for PostgresDb {
|
|||||||
timestamp: OffsetDateTime,
|
timestamp: OffsetDateTime,
|
||||||
nonce: u128,
|
nonce: u128,
|
||||||
) -> Result<MessageId> {
|
) -> Result<MessageId> {
|
||||||
test_support!(self, {
|
let query = "
|
||||||
let query = "
|
|
||||||
INSERT INTO channel_messages (channel_id, sender_id, body, sent_at, nonce)
|
INSERT INTO channel_messages (channel_id, sender_id, body, sent_at, nonce)
|
||||||
VALUES ($1, $2, $3, $4, $5)
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
ON CONFLICT (nonce) DO UPDATE SET nonce = excluded.nonce
|
ON CONFLICT (nonce) DO UPDATE SET nonce = excluded.nonce
|
||||||
RETURNING id
|
RETURNING id
|
||||||
";
|
";
|
||||||
Ok(sqlx::query_scalar(query)
|
Ok(sqlx::query_scalar(query)
|
||||||
.bind(channel_id.0)
|
.bind(channel_id.0)
|
||||||
.bind(sender_id.0)
|
.bind(sender_id.0)
|
||||||
.bind(body)
|
.bind(body)
|
||||||
.bind(timestamp)
|
.bind(timestamp)
|
||||||
.bind(Uuid::from_u128(nonce))
|
.bind(Uuid::from_u128(nonce))
|
||||||
.fetch_one(&self.pool)
|
.fetch_one(&self.pool)
|
||||||
.await
|
.await
|
||||||
.map(MessageId)?)
|
.map(MessageId)?)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_channel_messages(
|
async fn get_channel_messages(
|
||||||
@@ -423,8 +370,7 @@ impl Db for PostgresDb {
|
|||||||
count: usize,
|
count: usize,
|
||||||
before_id: Option<MessageId>,
|
before_id: Option<MessageId>,
|
||||||
) -> Result<Vec<ChannelMessage>> {
|
) -> Result<Vec<ChannelMessage>> {
|
||||||
test_support!(self, {
|
let query = r#"
|
||||||
let query = r#"
|
|
||||||
SELECT * FROM (
|
SELECT * FROM (
|
||||||
SELECT
|
SELECT
|
||||||
id, channel_id, sender_id, body, sent_at AT TIME ZONE 'UTC' as sent_at, nonce
|
id, channel_id, sender_id, body, sent_at AT TIME ZONE 'UTC' as sent_at, nonce
|
||||||
@@ -438,35 +384,32 @@ impl Db for PostgresDb {
|
|||||||
) as recent_messages
|
) as recent_messages
|
||||||
ORDER BY id ASC
|
ORDER BY id ASC
|
||||||
"#;
|
"#;
|
||||||
Ok(sqlx::query_as(query)
|
Ok(sqlx::query_as(query)
|
||||||
.bind(channel_id.0)
|
.bind(channel_id.0)
|
||||||
.bind(before_id.unwrap_or(MessageId::MAX))
|
.bind(before_id.unwrap_or(MessageId::MAX))
|
||||||
.bind(count as i64)
|
.bind(count as i64)
|
||||||
.fetch_all(&self.pool)
|
.fetch_all(&self.pool)
|
||||||
.await?)
|
.await?)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
async fn teardown(&self, name: &str, url: &str) {
|
async fn teardown(&self, name: &str, url: &str) {
|
||||||
use util::ResultExt;
|
use util::ResultExt;
|
||||||
|
|
||||||
test_support!(self, {
|
let query = "
|
||||||
let query = "
|
|
||||||
SELECT pg_terminate_backend(pg_stat_activity.pid)
|
SELECT pg_terminate_backend(pg_stat_activity.pid)
|
||||||
FROM pg_stat_activity
|
FROM pg_stat_activity
|
||||||
WHERE pg_stat_activity.datname = '{}' AND pid <> pg_backend_pid();
|
WHERE pg_stat_activity.datname = '{}' AND pid <> pg_backend_pid();
|
||||||
";
|
";
|
||||||
sqlx::query(query)
|
sqlx::query(query)
|
||||||
.bind(name)
|
.bind(name)
|
||||||
.execute(&self.pool)
|
.execute(&self.pool)
|
||||||
.await
|
.await
|
||||||
.log_err();
|
.log_err();
|
||||||
self.pool.close().await;
|
self.pool.close().await;
|
||||||
<sqlx::Postgres as sqlx::migrate::MigrateDatabase>::drop_database(url)
|
<sqlx::Postgres as sqlx::migrate::MigrateDatabase>::drop_database(url)
|
||||||
.await
|
.await
|
||||||
.log_err();
|
.log_err();
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -704,12 +647,11 @@ pub mod tests {
|
|||||||
let name = format!("zed-test-{}", rng.gen::<u128>());
|
let name = format!("zed-test-{}", rng.gen::<u128>());
|
||||||
let url = format!("postgres://postgres@localhost/{}", name);
|
let url = format!("postgres://postgres@localhost/{}", name);
|
||||||
let migrations_path = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/migrations"));
|
let migrations_path = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/migrations"));
|
||||||
let db = block_on(async {
|
let db = futures::executor::block_on(async {
|
||||||
Postgres::create_database(&url)
|
Postgres::create_database(&url)
|
||||||
.await
|
.await
|
||||||
.expect("failed to create test db");
|
.expect("failed to create test db");
|
||||||
let mut db = PostgresDb::new(&url, 5).await.unwrap();
|
let db = PostgresDb::new(&url, 5).await.unwrap();
|
||||||
db.test_mode = true;
|
|
||||||
let migrator = Migrator::new(migrations_path).await.unwrap();
|
let migrator = Migrator::new(migrations_path).await.unwrap();
|
||||||
migrator.run(&db.pool).await.unwrap();
|
migrator.run(&db.pool).await.unwrap();
|
||||||
db
|
db
|
||||||
@@ -737,7 +679,7 @@ pub mod tests {
|
|||||||
impl Drop for TestDb {
|
impl Drop for TestDb {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if let Some(db) = self.db.take() {
|
if let Some(db) = self.db.take() {
|
||||||
block_on(db.teardown(&self.name, &self.url));
|
futures::executor::block_on(db.teardown(&self.name, &self.url));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+66
-50
@@ -2,18 +2,16 @@ mod api;
|
|||||||
mod auth;
|
mod auth;
|
||||||
mod db;
|
mod db;
|
||||||
mod env;
|
mod env;
|
||||||
mod errors;
|
|
||||||
mod rpc;
|
mod rpc;
|
||||||
|
|
||||||
use ::rpc::Peer;
|
use axum::{body::Body, http::StatusCode, response::IntoResponse, Router};
|
||||||
use async_std::net::TcpListener;
|
|
||||||
use async_trait::async_trait;
|
|
||||||
use db::{Db, PostgresDb};
|
use db::{Db, PostgresDb};
|
||||||
use serde::Deserialize;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use tide_compress::CompressMiddleware;
|
|
||||||
|
|
||||||
type Request = tide::Request<Arc<AppState>>;
|
use serde::Deserialize;
|
||||||
|
use std::{
|
||||||
|
net::{SocketAddr, TcpListener},
|
||||||
|
sync::Arc,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Default, Deserialize)]
|
#[derive(Default, Deserialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
@@ -24,39 +22,26 @@ pub struct Config {
|
|||||||
|
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
db: Arc<dyn Db>,
|
db: Arc<dyn Db>,
|
||||||
config: Config,
|
api_token: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppState {
|
impl AppState {
|
||||||
async fn new(config: Config) -> tide::Result<Arc<Self>> {
|
async fn new(config: &Config) -> Result<Arc<Self>> {
|
||||||
let db = PostgresDb::new(&config.database_url, 5).await?;
|
let db = PostgresDb::new(&config.database_url, 5).await?;
|
||||||
|
|
||||||
let this = Self {
|
let this = Self {
|
||||||
db: Arc::new(db),
|
db: Arc::new(db),
|
||||||
config,
|
api_token: config.api_token.clone(),
|
||||||
};
|
};
|
||||||
Ok(Arc::new(this))
|
Ok(Arc::new(this))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[tokio::main]
|
||||||
trait RequestExt {
|
async fn main() -> Result<()> {
|
||||||
fn db(&self) -> &Arc<dyn Db>;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
impl RequestExt for Request {
|
|
||||||
fn db(&self) -> &Arc<dyn Db> {
|
|
||||||
&self.state().db
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_std::main]
|
|
||||||
async fn main() -> tide::Result<()> {
|
|
||||||
if std::env::var("LOG_JSON").is_ok() {
|
if std::env::var("LOG_JSON").is_ok() {
|
||||||
json_env_logger::init();
|
json_env_logger::init();
|
||||||
} else {
|
} else {
|
||||||
tide::log::start();
|
env_logger::init();
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Err(error) = env::load_dotenv() {
|
if let Err(error) = env::load_dotenv() {
|
||||||
@@ -67,32 +52,63 @@ async fn main() -> tide::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let config = envy::from_env::<Config>().expect("error loading config");
|
let config = envy::from_env::<Config>().expect("error loading config");
|
||||||
let state = AppState::new(config).await?;
|
let state = AppState::new(&config).await?;
|
||||||
let rpc = Peer::new();
|
|
||||||
run_server(
|
|
||||||
state.clone(),
|
|
||||||
rpc,
|
|
||||||
TcpListener::bind(&format!("0.0.0.0:{}", state.config.http_port)).await?,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn run_server(
|
let listener = TcpListener::bind(&format!("0.0.0.0:{}", config.http_port))
|
||||||
state: Arc<AppState>,
|
.expect("failed to bind TCP listener");
|
||||||
rpc: Arc<Peer>,
|
|
||||||
listener: TcpListener,
|
|
||||||
) -> tide::Result<()> {
|
|
||||||
let mut app = tide::with_state(state.clone());
|
|
||||||
rpc::add_routes(&mut app, &rpc);
|
|
||||||
|
|
||||||
let mut web = tide::with_state(state.clone());
|
let app = Router::<Body>::new()
|
||||||
web.with(CompressMiddleware::new());
|
.merge(api::routes(state.clone()))
|
||||||
api::add_routes(&mut web);
|
.merge(rpc::routes(state));
|
||||||
|
|
||||||
app.at("/").nest(web);
|
axum::Server::from_tcp(listener)?
|
||||||
|
.serve(app.into_make_service_with_connect_info::<SocketAddr>())
|
||||||
app.listen(listener).await?;
|
.await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub type Result<T, E = Error> = std::result::Result<T, E>;
|
||||||
|
|
||||||
|
pub enum Error {
|
||||||
|
Http(StatusCode, String),
|
||||||
|
Internal(anyhow::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<E> From<E> for Error
|
||||||
|
where
|
||||||
|
E: Into<anyhow::Error>,
|
||||||
|
{
|
||||||
|
fn from(error: E) -> Self {
|
||||||
|
Self::Internal(error.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IntoResponse for Error {
|
||||||
|
fn into_response(self) -> axum::response::Response {
|
||||||
|
match self {
|
||||||
|
Error::Http(code, message) => (code, message).into_response(),
|
||||||
|
Error::Internal(error) => {
|
||||||
|
(StatusCode::INTERNAL_SERVER_ERROR, format!("{}", &error)).into_response()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Debug for Error {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
Error::Http(code, message) => (code, message).fmt(f),
|
||||||
|
Error::Internal(error) => error.fmt(f),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for Error {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
Error::Http(code, message) => write!(f, "{code}: {message}"),
|
||||||
|
Error::Internal(error) => error.fmt(f),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+177
-149
@@ -1,47 +1,56 @@
|
|||||||
mod store;
|
mod store;
|
||||||
|
|
||||||
use super::{
|
use crate::{
|
||||||
auth::process_auth_header,
|
auth,
|
||||||
db::{ChannelId, MessageId, UserId},
|
db::{ChannelId, MessageId, UserId},
|
||||||
AppState,
|
AppState, Result,
|
||||||
};
|
};
|
||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
use async_io::Timer;
|
use async_tungstenite::tungstenite::{
|
||||||
use async_std::{
|
protocol::CloseFrame as TungsteniteCloseFrame, Message as TungsteniteMessage,
|
||||||
sync::{RwLock, RwLockReadGuard, RwLockWriteGuard},
|
};
|
||||||
task,
|
use axum::{
|
||||||
|
body::Body,
|
||||||
|
extract::{
|
||||||
|
ws::{CloseFrame as AxumCloseFrame, Message as AxumMessage},
|
||||||
|
ConnectInfo, WebSocketUpgrade,
|
||||||
|
},
|
||||||
|
headers::{Header, HeaderName},
|
||||||
|
http::StatusCode,
|
||||||
|
middleware,
|
||||||
|
response::{IntoResponse, Response},
|
||||||
|
routing::get,
|
||||||
|
Extension, Router, TypedHeader,
|
||||||
};
|
};
|
||||||
use async_tungstenite::{tungstenite::protocol::Role, WebSocketStream};
|
|
||||||
use collections::{HashMap, HashSet};
|
use collections::{HashMap, HashSet};
|
||||||
use futures::{channel::mpsc, future::BoxFuture, FutureExt, SinkExt, StreamExt};
|
use futures::{channel::mpsc, future::BoxFuture, FutureExt, SinkExt, StreamExt, TryStreamExt};
|
||||||
|
use lazy_static::lazy_static;
|
||||||
use log::{as_debug, as_display};
|
use log::{as_debug, as_display};
|
||||||
use rpc::{
|
use rpc::{
|
||||||
proto::{self, AnyTypedEnvelope, EntityMessage, EnvelopedMessage, RequestMessage},
|
proto::{self, AnyTypedEnvelope, EntityMessage, EnvelopedMessage, RequestMessage},
|
||||||
Connection, ConnectionId, Peer, TypedEnvelope,
|
Connection, ConnectionId, Peer, TypedEnvelope,
|
||||||
};
|
};
|
||||||
use sha1::{Digest as _, Sha1};
|
|
||||||
use std::{
|
use std::{
|
||||||
any::TypeId,
|
any::TypeId,
|
||||||
future::Future,
|
future::Future,
|
||||||
marker::PhantomData,
|
marker::PhantomData,
|
||||||
|
net::SocketAddr,
|
||||||
ops::{Deref, DerefMut},
|
ops::{Deref, DerefMut},
|
||||||
rc::Rc,
|
rc::Rc,
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
time::{Duration, Instant},
|
time::{Duration, Instant},
|
||||||
};
|
};
|
||||||
use store::{Store, Worktree};
|
use store::{Store, Worktree};
|
||||||
use surf::StatusCode;
|
|
||||||
use tide::{
|
|
||||||
http::headers::{HeaderName, CONNECTION, UPGRADE},
|
|
||||||
Request, Response,
|
|
||||||
};
|
|
||||||
use time::OffsetDateTime;
|
use time::OffsetDateTime;
|
||||||
|
use tokio::{
|
||||||
|
sync::{RwLock, RwLockReadGuard, RwLockWriteGuard},
|
||||||
|
time::Sleep,
|
||||||
|
};
|
||||||
|
use tower::ServiceBuilder;
|
||||||
use util::ResultExt;
|
use util::ResultExt;
|
||||||
|
|
||||||
type MessageHandler = Box<
|
type MessageHandler = Box<
|
||||||
dyn Send
|
dyn Send + Sync + Fn(Arc<Server>, Box<dyn AnyTypedEnvelope>) -> BoxFuture<'static, Result<()>>,
|
||||||
+ Sync
|
|
||||||
+ Fn(Arc<Server>, Box<dyn AnyTypedEnvelope>) -> BoxFuture<'static, tide::Result<()>>,
|
|
||||||
>;
|
>;
|
||||||
|
|
||||||
pub struct Server {
|
pub struct Server {
|
||||||
@@ -53,9 +62,9 @@ pub struct Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait Executor: Send + Clone {
|
pub trait Executor: Send + Clone {
|
||||||
type Timer: Send + Future;
|
type Sleep: Send + Future;
|
||||||
fn spawn_detached<F: 'static + Send + Future<Output = ()>>(&self, future: F);
|
fn spawn_detached<F: 'static + Send + Future<Output = ()>>(&self, future: F);
|
||||||
fn timer(&self, duration: Duration) -> Self::Timer;
|
fn sleep(&self, duration: Duration) -> Self::Sleep;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@@ -77,11 +86,10 @@ struct StoreWriteGuard<'a> {
|
|||||||
impl Server {
|
impl Server {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
app_state: Arc<AppState>,
|
app_state: Arc<AppState>,
|
||||||
peer: Arc<Peer>,
|
|
||||||
notifications: Option<mpsc::UnboundedSender<()>>,
|
notifications: Option<mpsc::UnboundedSender<()>>,
|
||||||
) -> Arc<Self> {
|
) -> Arc<Self> {
|
||||||
let mut server = Self {
|
let mut server = Self {
|
||||||
peer,
|
peer: Peer::new(),
|
||||||
app_state,
|
app_state,
|
||||||
store: Default::default(),
|
store: Default::default(),
|
||||||
handlers: Default::default(),
|
handlers: Default::default(),
|
||||||
@@ -141,7 +149,7 @@ impl Server {
|
|||||||
fn add_message_handler<F, Fut, M>(&mut self, handler: F) -> &mut Self
|
fn add_message_handler<F, Fut, M>(&mut self, handler: F) -> &mut Self
|
||||||
where
|
where
|
||||||
F: 'static + Send + Sync + Fn(Arc<Self>, TypedEnvelope<M>) -> Fut,
|
F: 'static + Send + Sync + Fn(Arc<Self>, TypedEnvelope<M>) -> Fut,
|
||||||
Fut: 'static + Send + Future<Output = tide::Result<()>>,
|
Fut: 'static + Send + Future<Output = Result<()>>,
|
||||||
M: EnvelopedMessage,
|
M: EnvelopedMessage,
|
||||||
{
|
{
|
||||||
let prev_handler = self.handlers.insert(
|
let prev_handler = self.handlers.insert(
|
||||||
@@ -160,7 +168,7 @@ impl Server {
|
|||||||
fn add_request_handler<F, Fut, M>(&mut self, handler: F) -> &mut Self
|
fn add_request_handler<F, Fut, M>(&mut self, handler: F) -> &mut Self
|
||||||
where
|
where
|
||||||
F: 'static + Send + Sync + Fn(Arc<Self>, TypedEnvelope<M>) -> Fut,
|
F: 'static + Send + Sync + Fn(Arc<Self>, TypedEnvelope<M>) -> Fut,
|
||||||
Fut: 'static + Send + Future<Output = tide::Result<M::Response>>,
|
Fut: 'static + Send + Future<Output = Result<M::Response>>,
|
||||||
M: RequestMessage,
|
M: RequestMessage,
|
||||||
{
|
{
|
||||||
self.add_message_handler(move |server, envelope| {
|
self.add_message_handler(move |server, envelope| {
|
||||||
@@ -193,7 +201,7 @@ impl Server {
|
|||||||
F: 'static
|
F: 'static
|
||||||
+ Send
|
+ Send
|
||||||
+ Sync
|
+ Sync
|
||||||
+ Fn(Arc<Self>, &mut Store, TypedEnvelope<M>) -> tide::Result<M::Response>,
|
+ Fn(Arc<Self>, &mut Store, TypedEnvelope<M>) -> Result<M::Response>,
|
||||||
M: RequestMessage,
|
M: RequestMessage,
|
||||||
{
|
{
|
||||||
let handler = Arc::new(handler);
|
let handler = Arc::new(handler);
|
||||||
@@ -237,7 +245,7 @@ impl Server {
|
|||||||
.add_connection(connection, {
|
.add_connection(connection, {
|
||||||
let executor = executor.clone();
|
let executor = executor.clone();
|
||||||
move |duration| {
|
move |duration| {
|
||||||
let timer = executor.timer(duration);
|
let timer = executor.sleep(duration);
|
||||||
async move {
|
async move {
|
||||||
timer.await;
|
timer.await;
|
||||||
}
|
}
|
||||||
@@ -308,7 +316,7 @@ impl Server {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn sign_out(self: &mut Arc<Self>, connection_id: ConnectionId) -> tide::Result<()> {
|
async fn sign_out(self: &mut Arc<Self>, connection_id: ConnectionId) -> Result<()> {
|
||||||
self.peer.disconnect(connection_id);
|
self.peer.disconnect(connection_id);
|
||||||
let mut state = self.state_mut().await;
|
let mut state = self.state_mut().await;
|
||||||
let removed_connection = state.remove_connection(connection_id)?;
|
let removed_connection = state.remove_connection(connection_id)?;
|
||||||
@@ -342,14 +350,14 @@ impl Server {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn ping(self: Arc<Server>, _: TypedEnvelope<proto::Ping>) -> tide::Result<proto::Ack> {
|
async fn ping(self: Arc<Server>, _: TypedEnvelope<proto::Ping>) -> Result<proto::Ack> {
|
||||||
Ok(proto::Ack {})
|
Ok(proto::Ack {})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn register_project(
|
async fn register_project(
|
||||||
self: Arc<Server>,
|
self: Arc<Server>,
|
||||||
request: TypedEnvelope<proto::RegisterProject>,
|
request: TypedEnvelope<proto::RegisterProject>,
|
||||||
) -> tide::Result<proto::RegisterProjectResponse> {
|
) -> Result<proto::RegisterProjectResponse> {
|
||||||
let project_id = {
|
let project_id = {
|
||||||
let mut state = self.state_mut().await;
|
let mut state = self.state_mut().await;
|
||||||
let user_id = state.user_id_for_connection(request.sender_id)?;
|
let user_id = state.user_id_for_connection(request.sender_id)?;
|
||||||
@@ -361,7 +369,7 @@ impl Server {
|
|||||||
async fn unregister_project(
|
async fn unregister_project(
|
||||||
self: Arc<Server>,
|
self: Arc<Server>,
|
||||||
request: TypedEnvelope<proto::UnregisterProject>,
|
request: TypedEnvelope<proto::UnregisterProject>,
|
||||||
) -> tide::Result<()> {
|
) -> Result<()> {
|
||||||
let mut state = self.state_mut().await;
|
let mut state = self.state_mut().await;
|
||||||
let project = state.unregister_project(request.payload.project_id, request.sender_id)?;
|
let project = state.unregister_project(request.payload.project_id, request.sender_id)?;
|
||||||
self.update_contacts_for_users(&*state, &project.authorized_user_ids());
|
self.update_contacts_for_users(&*state, &project.authorized_user_ids());
|
||||||
@@ -371,7 +379,7 @@ impl Server {
|
|||||||
async fn share_project(
|
async fn share_project(
|
||||||
self: Arc<Server>,
|
self: Arc<Server>,
|
||||||
request: TypedEnvelope<proto::ShareProject>,
|
request: TypedEnvelope<proto::ShareProject>,
|
||||||
) -> tide::Result<proto::Ack> {
|
) -> Result<proto::Ack> {
|
||||||
let mut state = self.state_mut().await;
|
let mut state = self.state_mut().await;
|
||||||
let project = state.share_project(request.payload.project_id, request.sender_id)?;
|
let project = state.share_project(request.payload.project_id, request.sender_id)?;
|
||||||
self.update_contacts_for_users(&mut *state, &project.authorized_user_ids);
|
self.update_contacts_for_users(&mut *state, &project.authorized_user_ids);
|
||||||
@@ -381,7 +389,7 @@ impl Server {
|
|||||||
async fn unshare_project(
|
async fn unshare_project(
|
||||||
self: Arc<Server>,
|
self: Arc<Server>,
|
||||||
request: TypedEnvelope<proto::UnshareProject>,
|
request: TypedEnvelope<proto::UnshareProject>,
|
||||||
) -> tide::Result<()> {
|
) -> Result<()> {
|
||||||
let project_id = request.payload.project_id;
|
let project_id = request.payload.project_id;
|
||||||
let mut state = self.state_mut().await;
|
let mut state = self.state_mut().await;
|
||||||
let project = state.unshare_project(project_id, request.sender_id)?;
|
let project = state.unshare_project(project_id, request.sender_id)?;
|
||||||
@@ -397,7 +405,7 @@ impl Server {
|
|||||||
self: Arc<Server>,
|
self: Arc<Server>,
|
||||||
state: &mut Store,
|
state: &mut Store,
|
||||||
request: TypedEnvelope<proto::JoinProject>,
|
request: TypedEnvelope<proto::JoinProject>,
|
||||||
) -> tide::Result<proto::JoinProjectResponse> {
|
) -> Result<proto::JoinProjectResponse> {
|
||||||
let project_id = request.payload.project_id;
|
let project_id = request.payload.project_id;
|
||||||
|
|
||||||
let user_id = state.user_id_for_connection(request.sender_id)?;
|
let user_id = state.user_id_for_connection(request.sender_id)?;
|
||||||
@@ -470,7 +478,7 @@ impl Server {
|
|||||||
async fn leave_project(
|
async fn leave_project(
|
||||||
self: Arc<Server>,
|
self: Arc<Server>,
|
||||||
request: TypedEnvelope<proto::LeaveProject>,
|
request: TypedEnvelope<proto::LeaveProject>,
|
||||||
) -> tide::Result<()> {
|
) -> Result<()> {
|
||||||
let sender_id = request.sender_id;
|
let sender_id = request.sender_id;
|
||||||
let project_id = request.payload.project_id;
|
let project_id = request.payload.project_id;
|
||||||
let mut state = self.state_mut().await;
|
let mut state = self.state_mut().await;
|
||||||
@@ -491,7 +499,7 @@ impl Server {
|
|||||||
async fn register_worktree(
|
async fn register_worktree(
|
||||||
self: Arc<Server>,
|
self: Arc<Server>,
|
||||||
request: TypedEnvelope<proto::RegisterWorktree>,
|
request: TypedEnvelope<proto::RegisterWorktree>,
|
||||||
) -> tide::Result<proto::Ack> {
|
) -> Result<proto::Ack> {
|
||||||
let mut contact_user_ids = HashSet::default();
|
let mut contact_user_ids = HashSet::default();
|
||||||
for github_login in &request.payload.authorized_logins {
|
for github_login in &request.payload.authorized_logins {
|
||||||
let contact_user_id = self.app_state.db.create_user(github_login, false).await?;
|
let contact_user_id = self.app_state.db.create_user(github_login, false).await?;
|
||||||
@@ -528,7 +536,7 @@ impl Server {
|
|||||||
async fn unregister_worktree(
|
async fn unregister_worktree(
|
||||||
self: Arc<Server>,
|
self: Arc<Server>,
|
||||||
request: TypedEnvelope<proto::UnregisterWorktree>,
|
request: TypedEnvelope<proto::UnregisterWorktree>,
|
||||||
) -> tide::Result<()> {
|
) -> Result<()> {
|
||||||
let project_id = request.payload.project_id;
|
let project_id = request.payload.project_id;
|
||||||
let worktree_id = request.payload.worktree_id;
|
let worktree_id = request.payload.worktree_id;
|
||||||
let mut state = self.state_mut().await;
|
let mut state = self.state_mut().await;
|
||||||
@@ -550,7 +558,7 @@ impl Server {
|
|||||||
async fn update_worktree(
|
async fn update_worktree(
|
||||||
self: Arc<Server>,
|
self: Arc<Server>,
|
||||||
request: TypedEnvelope<proto::UpdateWorktree>,
|
request: TypedEnvelope<proto::UpdateWorktree>,
|
||||||
) -> tide::Result<proto::Ack> {
|
) -> Result<proto::Ack> {
|
||||||
let connection_ids = self.state_mut().await.update_worktree(
|
let connection_ids = self.state_mut().await.update_worktree(
|
||||||
request.sender_id,
|
request.sender_id,
|
||||||
request.payload.project_id,
|
request.payload.project_id,
|
||||||
@@ -570,7 +578,7 @@ impl Server {
|
|||||||
async fn update_diagnostic_summary(
|
async fn update_diagnostic_summary(
|
||||||
self: Arc<Server>,
|
self: Arc<Server>,
|
||||||
request: TypedEnvelope<proto::UpdateDiagnosticSummary>,
|
request: TypedEnvelope<proto::UpdateDiagnosticSummary>,
|
||||||
) -> tide::Result<()> {
|
) -> Result<()> {
|
||||||
let summary = request
|
let summary = request
|
||||||
.payload
|
.payload
|
||||||
.summary
|
.summary
|
||||||
@@ -593,7 +601,7 @@ impl Server {
|
|||||||
async fn start_language_server(
|
async fn start_language_server(
|
||||||
self: Arc<Server>,
|
self: Arc<Server>,
|
||||||
request: TypedEnvelope<proto::StartLanguageServer>,
|
request: TypedEnvelope<proto::StartLanguageServer>,
|
||||||
) -> tide::Result<()> {
|
) -> Result<()> {
|
||||||
let receiver_ids = self.state_mut().await.start_language_server(
|
let receiver_ids = self.state_mut().await.start_language_server(
|
||||||
request.payload.project_id,
|
request.payload.project_id,
|
||||||
request.sender_id,
|
request.sender_id,
|
||||||
@@ -613,7 +621,7 @@ impl Server {
|
|||||||
async fn update_language_server(
|
async fn update_language_server(
|
||||||
self: Arc<Server>,
|
self: Arc<Server>,
|
||||||
request: TypedEnvelope<proto::UpdateLanguageServer>,
|
request: TypedEnvelope<proto::UpdateLanguageServer>,
|
||||||
) -> tide::Result<()> {
|
) -> Result<()> {
|
||||||
let receiver_ids = self
|
let receiver_ids = self
|
||||||
.state()
|
.state()
|
||||||
.await
|
.await
|
||||||
@@ -628,7 +636,7 @@ impl Server {
|
|||||||
async fn forward_project_request<T>(
|
async fn forward_project_request<T>(
|
||||||
self: Arc<Server>,
|
self: Arc<Server>,
|
||||||
request: TypedEnvelope<T>,
|
request: TypedEnvelope<T>,
|
||||||
) -> tide::Result<T::Response>
|
) -> Result<T::Response>
|
||||||
where
|
where
|
||||||
T: EntityMessage + RequestMessage,
|
T: EntityMessage + RequestMessage,
|
||||||
{
|
{
|
||||||
@@ -646,7 +654,7 @@ impl Server {
|
|||||||
async fn save_buffer(
|
async fn save_buffer(
|
||||||
self: Arc<Server>,
|
self: Arc<Server>,
|
||||||
request: TypedEnvelope<proto::SaveBuffer>,
|
request: TypedEnvelope<proto::SaveBuffer>,
|
||||||
) -> tide::Result<proto::BufferSaved> {
|
) -> Result<proto::BufferSaved> {
|
||||||
let host = self
|
let host = self
|
||||||
.state()
|
.state()
|
||||||
.await
|
.await
|
||||||
@@ -673,7 +681,7 @@ impl Server {
|
|||||||
async fn update_buffer(
|
async fn update_buffer(
|
||||||
self: Arc<Server>,
|
self: Arc<Server>,
|
||||||
request: TypedEnvelope<proto::UpdateBuffer>,
|
request: TypedEnvelope<proto::UpdateBuffer>,
|
||||||
) -> tide::Result<proto::Ack> {
|
) -> Result<proto::Ack> {
|
||||||
let receiver_ids = self
|
let receiver_ids = self
|
||||||
.state()
|
.state()
|
||||||
.await
|
.await
|
||||||
@@ -688,7 +696,7 @@ impl Server {
|
|||||||
async fn update_buffer_file(
|
async fn update_buffer_file(
|
||||||
self: Arc<Server>,
|
self: Arc<Server>,
|
||||||
request: TypedEnvelope<proto::UpdateBufferFile>,
|
request: TypedEnvelope<proto::UpdateBufferFile>,
|
||||||
) -> tide::Result<()> {
|
) -> Result<()> {
|
||||||
let receiver_ids = self
|
let receiver_ids = self
|
||||||
.state()
|
.state()
|
||||||
.await
|
.await
|
||||||
@@ -703,7 +711,7 @@ impl Server {
|
|||||||
async fn buffer_reloaded(
|
async fn buffer_reloaded(
|
||||||
self: Arc<Server>,
|
self: Arc<Server>,
|
||||||
request: TypedEnvelope<proto::BufferReloaded>,
|
request: TypedEnvelope<proto::BufferReloaded>,
|
||||||
) -> tide::Result<()> {
|
) -> Result<()> {
|
||||||
let receiver_ids = self
|
let receiver_ids = self
|
||||||
.state()
|
.state()
|
||||||
.await
|
.await
|
||||||
@@ -718,7 +726,7 @@ impl Server {
|
|||||||
async fn buffer_saved(
|
async fn buffer_saved(
|
||||||
self: Arc<Server>,
|
self: Arc<Server>,
|
||||||
request: TypedEnvelope<proto::BufferSaved>,
|
request: TypedEnvelope<proto::BufferSaved>,
|
||||||
) -> tide::Result<()> {
|
) -> Result<()> {
|
||||||
let receiver_ids = self
|
let receiver_ids = self
|
||||||
.state()
|
.state()
|
||||||
.await
|
.await
|
||||||
@@ -733,7 +741,7 @@ impl Server {
|
|||||||
async fn follow(
|
async fn follow(
|
||||||
self: Arc<Self>,
|
self: Arc<Self>,
|
||||||
request: TypedEnvelope<proto::Follow>,
|
request: TypedEnvelope<proto::Follow>,
|
||||||
) -> tide::Result<proto::FollowResponse> {
|
) -> Result<proto::FollowResponse> {
|
||||||
let leader_id = ConnectionId(request.payload.leader_id);
|
let leader_id = ConnectionId(request.payload.leader_id);
|
||||||
let follower_id = request.sender_id;
|
let follower_id = request.sender_id;
|
||||||
if !self
|
if !self
|
||||||
@@ -754,10 +762,7 @@ impl Server {
|
|||||||
Ok(response)
|
Ok(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn unfollow(
|
async fn unfollow(self: Arc<Self>, request: TypedEnvelope<proto::Unfollow>) -> Result<()> {
|
||||||
self: Arc<Self>,
|
|
||||||
request: TypedEnvelope<proto::Unfollow>,
|
|
||||||
) -> tide::Result<()> {
|
|
||||||
let leader_id = ConnectionId(request.payload.leader_id);
|
let leader_id = ConnectionId(request.payload.leader_id);
|
||||||
if !self
|
if !self
|
||||||
.state()
|
.state()
|
||||||
@@ -775,7 +780,7 @@ impl Server {
|
|||||||
async fn update_followers(
|
async fn update_followers(
|
||||||
self: Arc<Self>,
|
self: Arc<Self>,
|
||||||
request: TypedEnvelope<proto::UpdateFollowers>,
|
request: TypedEnvelope<proto::UpdateFollowers>,
|
||||||
) -> tide::Result<()> {
|
) -> Result<()> {
|
||||||
let connection_ids = self
|
let connection_ids = self
|
||||||
.state()
|
.state()
|
||||||
.await
|
.await
|
||||||
@@ -802,7 +807,7 @@ impl Server {
|
|||||||
async fn get_channels(
|
async fn get_channels(
|
||||||
self: Arc<Server>,
|
self: Arc<Server>,
|
||||||
request: TypedEnvelope<proto::GetChannels>,
|
request: TypedEnvelope<proto::GetChannels>,
|
||||||
) -> tide::Result<proto::GetChannelsResponse> {
|
) -> Result<proto::GetChannelsResponse> {
|
||||||
let user_id = self
|
let user_id = self
|
||||||
.state()
|
.state()
|
||||||
.await
|
.await
|
||||||
@@ -822,7 +827,7 @@ impl Server {
|
|||||||
async fn get_users(
|
async fn get_users(
|
||||||
self: Arc<Server>,
|
self: Arc<Server>,
|
||||||
request: TypedEnvelope<proto::GetUsers>,
|
request: TypedEnvelope<proto::GetUsers>,
|
||||||
) -> tide::Result<proto::GetUsersResponse> {
|
) -> Result<proto::GetUsersResponse> {
|
||||||
let user_ids = request
|
let user_ids = request
|
||||||
.payload
|
.payload
|
||||||
.user_ids
|
.user_ids
|
||||||
@@ -867,7 +872,7 @@ impl Server {
|
|||||||
async fn join_channel(
|
async fn join_channel(
|
||||||
self: Arc<Self>,
|
self: Arc<Self>,
|
||||||
request: TypedEnvelope<proto::JoinChannel>,
|
request: TypedEnvelope<proto::JoinChannel>,
|
||||||
) -> tide::Result<proto::JoinChannelResponse> {
|
) -> Result<proto::JoinChannelResponse> {
|
||||||
let user_id = self
|
let user_id = self
|
||||||
.state()
|
.state()
|
||||||
.await
|
.await
|
||||||
@@ -908,7 +913,7 @@ impl Server {
|
|||||||
async fn leave_channel(
|
async fn leave_channel(
|
||||||
self: Arc<Self>,
|
self: Arc<Self>,
|
||||||
request: TypedEnvelope<proto::LeaveChannel>,
|
request: TypedEnvelope<proto::LeaveChannel>,
|
||||||
) -> tide::Result<()> {
|
) -> Result<()> {
|
||||||
let user_id = self
|
let user_id = self
|
||||||
.state()
|
.state()
|
||||||
.await
|
.await
|
||||||
@@ -933,7 +938,7 @@ impl Server {
|
|||||||
async fn send_channel_message(
|
async fn send_channel_message(
|
||||||
self: Arc<Self>,
|
self: Arc<Self>,
|
||||||
request: TypedEnvelope<proto::SendChannelMessage>,
|
request: TypedEnvelope<proto::SendChannelMessage>,
|
||||||
) -> tide::Result<proto::SendChannelMessageResponse> {
|
) -> Result<proto::SendChannelMessageResponse> {
|
||||||
let channel_id = ChannelId::from_proto(request.payload.channel_id);
|
let channel_id = ChannelId::from_proto(request.payload.channel_id);
|
||||||
let user_id;
|
let user_id;
|
||||||
let connection_ids;
|
let connection_ids;
|
||||||
@@ -988,7 +993,7 @@ impl Server {
|
|||||||
async fn get_channel_messages(
|
async fn get_channel_messages(
|
||||||
self: Arc<Self>,
|
self: Arc<Self>,
|
||||||
request: TypedEnvelope<proto::GetChannelMessages>,
|
request: TypedEnvelope<proto::GetChannelMessages>,
|
||||||
) -> tide::Result<proto::GetChannelMessagesResponse> {
|
) -> Result<proto::GetChannelMessagesResponse> {
|
||||||
let user_id = self
|
let user_id = self
|
||||||
.state()
|
.state()
|
||||||
.await
|
.await
|
||||||
@@ -1030,10 +1035,10 @@ impl Server {
|
|||||||
|
|
||||||
async fn state<'a>(self: &'a Arc<Self>) -> StoreReadGuard<'a> {
|
async fn state<'a>(self: &'a Arc<Self>) -> StoreReadGuard<'a> {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
async_std::task::yield_now().await;
|
tokio::task::yield_now().await;
|
||||||
let guard = self.store.read().await;
|
let guard = self.store.read().await;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
async_std::task::yield_now().await;
|
tokio::task::yield_now().await;
|
||||||
StoreReadGuard {
|
StoreReadGuard {
|
||||||
guard,
|
guard,
|
||||||
_not_send: PhantomData,
|
_not_send: PhantomData,
|
||||||
@@ -1042,10 +1047,10 @@ impl Server {
|
|||||||
|
|
||||||
async fn state_mut<'a>(self: &'a Arc<Self>) -> StoreWriteGuard<'a> {
|
async fn state_mut<'a>(self: &'a Arc<Self>) -> StoreWriteGuard<'a> {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
async_std::task::yield_now().await;
|
tokio::task::yield_now().await;
|
||||||
let guard = self.store.write().await;
|
let guard = self.store.write().await;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
async_std::task::yield_now().await;
|
tokio::task::yield_now().await;
|
||||||
StoreWriteGuard {
|
StoreWriteGuard {
|
||||||
guard,
|
guard,
|
||||||
_not_send: PhantomData,
|
_not_send: PhantomData,
|
||||||
@@ -1083,14 +1088,14 @@ impl<'a> Drop for StoreWriteGuard<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Executor for RealExecutor {
|
impl Executor for RealExecutor {
|
||||||
type Timer = Timer;
|
type Sleep = Sleep;
|
||||||
|
|
||||||
fn spawn_detached<F: 'static + Send + Future<Output = ()>>(&self, future: F) {
|
fn spawn_detached<F: 'static + Send + Future<Output = ()>>(&self, future: F) {
|
||||||
task::spawn(future);
|
tokio::task::spawn(future);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn timer(&self, duration: Duration) -> Self::Timer {
|
fn sleep(&self, duration: Duration) -> Self::Sleep {
|
||||||
Timer::after(duration)
|
tokio::time::sleep(duration)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1105,75 +1110,100 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_routes(app: &mut tide::Server<Arc<AppState>>, rpc: &Arc<Peer>) {
|
lazy_static! {
|
||||||
let server = Server::new(app.state().clone(), rpc.clone(), None);
|
static ref ZED_PROTOCOL_VERSION: HeaderName = HeaderName::from_static("x-zed-protocol-version");
|
||||||
app.at("/rpc").get(move |request: Request<Arc<AppState>>| {
|
|
||||||
let server = server.clone();
|
|
||||||
async move {
|
|
||||||
const WEBSOCKET_GUID: &str = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
|
||||||
|
|
||||||
let connection_upgrade = header_contains_ignore_case(&request, CONNECTION, "upgrade");
|
|
||||||
let upgrade_to_websocket = header_contains_ignore_case(&request, UPGRADE, "websocket");
|
|
||||||
let upgrade_requested = connection_upgrade && upgrade_to_websocket;
|
|
||||||
let client_protocol_version: Option<u32> = request
|
|
||||||
.header("X-Zed-Protocol-Version")
|
|
||||||
.and_then(|v| v.as_str().parse().ok());
|
|
||||||
|
|
||||||
if !upgrade_requested || client_protocol_version != Some(rpc::PROTOCOL_VERSION) {
|
|
||||||
return Ok(Response::new(StatusCode::UpgradeRequired));
|
|
||||||
}
|
|
||||||
|
|
||||||
let header = match request.header("Sec-Websocket-Key") {
|
|
||||||
Some(h) => h.as_str(),
|
|
||||||
None => return Err(anyhow!("expected sec-websocket-key"))?,
|
|
||||||
};
|
|
||||||
|
|
||||||
let user_id = process_auth_header(&request).await?;
|
|
||||||
|
|
||||||
let mut response = Response::new(StatusCode::SwitchingProtocols);
|
|
||||||
response.insert_header(UPGRADE, "websocket");
|
|
||||||
response.insert_header(CONNECTION, "Upgrade");
|
|
||||||
let hash = Sha1::new().chain(header).chain(WEBSOCKET_GUID).finalize();
|
|
||||||
response.insert_header("Sec-Websocket-Accept", base64::encode(&hash[..]));
|
|
||||||
response.insert_header("Sec-Websocket-Version", "13");
|
|
||||||
|
|
||||||
let http_res: &mut tide::http::Response = response.as_mut();
|
|
||||||
let upgrade_receiver = http_res.recv_upgrade().await;
|
|
||||||
let addr = request.remote().unwrap_or("unknown").to_string();
|
|
||||||
task::spawn(async move {
|
|
||||||
if let Some(stream) = upgrade_receiver.await {
|
|
||||||
server
|
|
||||||
.handle_connection(
|
|
||||||
Connection::new(
|
|
||||||
WebSocketStream::from_raw_socket(stream, Role::Server, None).await,
|
|
||||||
),
|
|
||||||
addr,
|
|
||||||
user_id,
|
|
||||||
None,
|
|
||||||
RealExecutor,
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Ok(response)
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn header_contains_ignore_case<T>(
|
pub struct ProtocolVersion(u32);
|
||||||
request: &tide::Request<T>,
|
|
||||||
header_name: HeaderName,
|
impl Header for ProtocolVersion {
|
||||||
value: &str,
|
fn name() -> &'static HeaderName {
|
||||||
) -> bool {
|
&ZED_PROTOCOL_VERSION
|
||||||
request
|
}
|
||||||
.header(header_name)
|
|
||||||
.map(|h| {
|
fn decode<'i, I>(values: &mut I) -> Result<Self, axum::headers::Error>
|
||||||
h.as_str()
|
where
|
||||||
.split(',')
|
Self: Sized,
|
||||||
.any(|s| s.trim().eq_ignore_ascii_case(value.trim()))
|
I: Iterator<Item = &'i axum::http::HeaderValue>,
|
||||||
})
|
{
|
||||||
.unwrap_or(false)
|
let version = values
|
||||||
|
.next()
|
||||||
|
.ok_or_else(|| axum::headers::Error::invalid())?
|
||||||
|
.to_str()
|
||||||
|
.map_err(|_| axum::headers::Error::invalid())?
|
||||||
|
.parse()
|
||||||
|
.map_err(|_| axum::headers::Error::invalid())?;
|
||||||
|
Ok(Self(version))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn encode<E: Extend<axum::http::HeaderValue>>(&self, values: &mut E) {
|
||||||
|
values.extend([self.0.to_string().parse().unwrap()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn routes(app_state: Arc<AppState>) -> Router<Body> {
|
||||||
|
let server = Server::new(app_state.clone(), None);
|
||||||
|
Router::new()
|
||||||
|
.route("/rpc", get(handle_websocket_request))
|
||||||
|
.layer(
|
||||||
|
ServiceBuilder::new()
|
||||||
|
.layer(Extension(app_state))
|
||||||
|
.layer(middleware::from_fn(auth::validate_header))
|
||||||
|
.layer(Extension(server)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn handle_websocket_request(
|
||||||
|
TypedHeader(ProtocolVersion(protocol_version)): TypedHeader<ProtocolVersion>,
|
||||||
|
ConnectInfo(socket_address): ConnectInfo<SocketAddr>,
|
||||||
|
Extension(server): Extension<Arc<Server>>,
|
||||||
|
Extension(user_id): Extension<UserId>,
|
||||||
|
ws: WebSocketUpgrade,
|
||||||
|
) -> Response {
|
||||||
|
if protocol_version != rpc::PROTOCOL_VERSION {
|
||||||
|
return (
|
||||||
|
StatusCode::UPGRADE_REQUIRED,
|
||||||
|
"client must be upgraded".to_string(),
|
||||||
|
)
|
||||||
|
.into_response();
|
||||||
|
}
|
||||||
|
let socket_address = socket_address.to_string();
|
||||||
|
ws.on_upgrade(move |socket| {
|
||||||
|
let socket = socket
|
||||||
|
.map_ok(to_tungstenite_message)
|
||||||
|
.err_into()
|
||||||
|
.with(|message| async move { Ok(to_axum_message(message)) });
|
||||||
|
let connection = Connection::new(Box::pin(socket));
|
||||||
|
server.handle_connection(connection, socket_address, user_id, None, RealExecutor)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_axum_message(message: TungsteniteMessage) -> AxumMessage {
|
||||||
|
match message {
|
||||||
|
TungsteniteMessage::Text(payload) => AxumMessage::Text(payload),
|
||||||
|
TungsteniteMessage::Binary(payload) => AxumMessage::Binary(payload),
|
||||||
|
TungsteniteMessage::Ping(payload) => AxumMessage::Ping(payload),
|
||||||
|
TungsteniteMessage::Pong(payload) => AxumMessage::Pong(payload),
|
||||||
|
TungsteniteMessage::Close(frame) => AxumMessage::Close(frame.map(|frame| AxumCloseFrame {
|
||||||
|
code: frame.code.into(),
|
||||||
|
reason: frame.reason,
|
||||||
|
})),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_tungstenite_message(message: AxumMessage) -> TungsteniteMessage {
|
||||||
|
match message {
|
||||||
|
AxumMessage::Text(payload) => TungsteniteMessage::Text(payload),
|
||||||
|
AxumMessage::Binary(payload) => TungsteniteMessage::Binary(payload),
|
||||||
|
AxumMessage::Ping(payload) => TungsteniteMessage::Ping(payload),
|
||||||
|
AxumMessage::Pong(payload) => TungsteniteMessage::Pong(payload),
|
||||||
|
AxumMessage::Close(frame) => {
|
||||||
|
TungsteniteMessage::Close(frame.map(|frame| TungsteniteCloseFrame {
|
||||||
|
code: frame.code.into(),
|
||||||
|
reason: frame.reason,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -1181,7 +1211,7 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
use crate::{
|
use crate::{
|
||||||
db::{tests::TestDb, UserId},
|
db::{tests::TestDb, UserId},
|
||||||
AppState, Config,
|
AppState,
|
||||||
};
|
};
|
||||||
use ::rpc::Peer;
|
use ::rpc::Peer;
|
||||||
use client::{
|
use client::{
|
||||||
@@ -5621,7 +5651,7 @@ mod tests {
|
|||||||
let app_state = Self::build_app_state(&test_db).await;
|
let app_state = Self::build_app_state(&test_db).await;
|
||||||
let peer = Peer::new();
|
let peer = Peer::new();
|
||||||
let notifications = mpsc::unbounded();
|
let notifications = mpsc::unbounded();
|
||||||
let server = Server::new(app_state.clone(), peer.clone(), Some(notifications.0));
|
let server = Server::new(app_state.clone(), Some(notifications.0));
|
||||||
Self {
|
Self {
|
||||||
peer,
|
peer,
|
||||||
app_state,
|
app_state,
|
||||||
@@ -5736,11 +5766,9 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn build_app_state(test_db: &TestDb) -> Arc<AppState> {
|
async fn build_app_state(test_db: &TestDb) -> Arc<AppState> {
|
||||||
let mut config = Config::default();
|
|
||||||
config.database_url = test_db.url.clone();
|
|
||||||
Arc::new(AppState {
|
Arc::new(AppState {
|
||||||
db: test_db.db().clone(),
|
db: test_db.db().clone(),
|
||||||
config,
|
api_token: Default::default(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -5752,15 +5780,15 @@ mod tests {
|
|||||||
where
|
where
|
||||||
F: FnMut(&Store) -> bool,
|
F: FnMut(&Store) -> bool,
|
||||||
{
|
{
|
||||||
async_std::future::timeout(Duration::from_millis(500), async {
|
assert!(
|
||||||
while !(predicate)(&*self.server.store.read().await) {
|
self.foreground.parking_forbidden(),
|
||||||
self.foreground.start_waiting();
|
"you must call forbid_parking to use server conditions so we don't block indefinitely"
|
||||||
self.notifications.next().await;
|
);
|
||||||
self.foreground.finish_waiting();
|
while !(predicate)(&*self.server.store.read().await) {
|
||||||
}
|
self.foreground.start_waiting();
|
||||||
})
|
self.notifications.next().await;
|
||||||
.await
|
self.foreground.finish_waiting();
|
||||||
.expect("condition timed out");
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -6325,13 +6353,13 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Executor for Arc<gpui::executor::Background> {
|
impl Executor for Arc<gpui::executor::Background> {
|
||||||
type Timer = gpui::executor::Timer;
|
type Sleep = gpui::executor::Timer;
|
||||||
|
|
||||||
fn spawn_detached<F: 'static + Send + Future<Output = ()>>(&self, future: F) {
|
fn spawn_detached<F: 'static + Send + Future<Output = ()>>(&self, future: F) {
|
||||||
self.spawn(future).detach();
|
self.spawn(future).detach();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn timer(&self, duration: Duration) -> Self::Timer {
|
fn sleep(&self, duration: Duration) -> Self::Sleep {
|
||||||
self.as_ref().timer(duration)
|
self.as_ref().timer(duration)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use crate::db::{ChannelId, UserId};
|
use crate::db::{ChannelId, UserId};
|
||||||
use anyhow::anyhow;
|
use anyhow::{anyhow, Result};
|
||||||
use collections::{BTreeMap, HashMap, HashSet};
|
use collections::{BTreeMap, HashMap, HashSet};
|
||||||
use rpc::{proto, ConnectionId};
|
use rpc::{proto, ConnectionId};
|
||||||
use std::{collections::hash_map, path::PathBuf};
|
use std::{collections::hash_map, path::PathBuf};
|
||||||
@@ -99,7 +99,7 @@ impl Store {
|
|||||||
pub fn remove_connection(
|
pub fn remove_connection(
|
||||||
&mut self,
|
&mut self,
|
||||||
connection_id: ConnectionId,
|
connection_id: ConnectionId,
|
||||||
) -> tide::Result<RemovedConnectionState> {
|
) -> Result<RemovedConnectionState> {
|
||||||
let connection = if let Some(connection) = self.connections.remove(&connection_id) {
|
let connection = if let Some(connection) = self.connections.remove(&connection_id) {
|
||||||
connection
|
connection
|
||||||
} else {
|
} else {
|
||||||
@@ -165,7 +165,7 @@ impl Store {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn user_id_for_connection(&self, connection_id: ConnectionId) -> tide::Result<UserId> {
|
pub fn user_id_for_connection(&self, connection_id: ConnectionId) -> Result<UserId> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.connections
|
.connections
|
||||||
.get(&connection_id)
|
.get(&connection_id)
|
||||||
@@ -258,7 +258,7 @@ impl Store {
|
|||||||
worktree_id: u64,
|
worktree_id: u64,
|
||||||
connection_id: ConnectionId,
|
connection_id: ConnectionId,
|
||||||
worktree: Worktree,
|
worktree: Worktree,
|
||||||
) -> tide::Result<()> {
|
) -> Result<()> {
|
||||||
let project = self
|
let project = self
|
||||||
.projects
|
.projects
|
||||||
.get_mut(&project_id)
|
.get_mut(&project_id)
|
||||||
@@ -286,7 +286,7 @@ impl Store {
|
|||||||
&mut self,
|
&mut self,
|
||||||
project_id: u64,
|
project_id: u64,
|
||||||
connection_id: ConnectionId,
|
connection_id: ConnectionId,
|
||||||
) -> tide::Result<Project> {
|
) -> Result<Project> {
|
||||||
match self.projects.entry(project_id) {
|
match self.projects.entry(project_id) {
|
||||||
hash_map::Entry::Occupied(e) => {
|
hash_map::Entry::Occupied(e) => {
|
||||||
if e.get().host_connection_id == connection_id {
|
if e.get().host_connection_id == connection_id {
|
||||||
@@ -326,7 +326,7 @@ impl Store {
|
|||||||
project_id: u64,
|
project_id: u64,
|
||||||
worktree_id: u64,
|
worktree_id: u64,
|
||||||
acting_connection_id: ConnectionId,
|
acting_connection_id: ConnectionId,
|
||||||
) -> tide::Result<(Worktree, Vec<ConnectionId>)> {
|
) -> Result<(Worktree, Vec<ConnectionId>)> {
|
||||||
let project = self
|
let project = self
|
||||||
.projects
|
.projects
|
||||||
.get_mut(&project_id)
|
.get_mut(&project_id)
|
||||||
@@ -363,7 +363,7 @@ impl Store {
|
|||||||
&mut self,
|
&mut self,
|
||||||
project_id: u64,
|
project_id: u64,
|
||||||
connection_id: ConnectionId,
|
connection_id: ConnectionId,
|
||||||
) -> tide::Result<SharedProject> {
|
) -> Result<SharedProject> {
|
||||||
if let Some(project) = self.projects.get_mut(&project_id) {
|
if let Some(project) = self.projects.get_mut(&project_id) {
|
||||||
if project.host_connection_id == connection_id {
|
if project.host_connection_id == connection_id {
|
||||||
let mut share = ProjectShare::default();
|
let mut share = ProjectShare::default();
|
||||||
@@ -383,7 +383,7 @@ impl Store {
|
|||||||
&mut self,
|
&mut self,
|
||||||
project_id: u64,
|
project_id: u64,
|
||||||
acting_connection_id: ConnectionId,
|
acting_connection_id: ConnectionId,
|
||||||
) -> tide::Result<UnsharedProject> {
|
) -> Result<UnsharedProject> {
|
||||||
let project = if let Some(project) = self.projects.get_mut(&project_id) {
|
let project = if let Some(project) = self.projects.get_mut(&project_id) {
|
||||||
project
|
project
|
||||||
} else {
|
} else {
|
||||||
@@ -418,7 +418,7 @@ impl Store {
|
|||||||
worktree_id: u64,
|
worktree_id: u64,
|
||||||
connection_id: ConnectionId,
|
connection_id: ConnectionId,
|
||||||
summary: proto::DiagnosticSummary,
|
summary: proto::DiagnosticSummary,
|
||||||
) -> tide::Result<Vec<ConnectionId>> {
|
) -> Result<Vec<ConnectionId>> {
|
||||||
let project = self
|
let project = self
|
||||||
.projects
|
.projects
|
||||||
.get_mut(&project_id)
|
.get_mut(&project_id)
|
||||||
@@ -443,7 +443,7 @@ impl Store {
|
|||||||
project_id: u64,
|
project_id: u64,
|
||||||
connection_id: ConnectionId,
|
connection_id: ConnectionId,
|
||||||
language_server: proto::LanguageServer,
|
language_server: proto::LanguageServer,
|
||||||
) -> tide::Result<Vec<ConnectionId>> {
|
) -> Result<Vec<ConnectionId>> {
|
||||||
let project = self
|
let project = self
|
||||||
.projects
|
.projects
|
||||||
.get_mut(&project_id)
|
.get_mut(&project_id)
|
||||||
@@ -461,7 +461,7 @@ impl Store {
|
|||||||
connection_id: ConnectionId,
|
connection_id: ConnectionId,
|
||||||
user_id: UserId,
|
user_id: UserId,
|
||||||
project_id: u64,
|
project_id: u64,
|
||||||
) -> tide::Result<JoinedProject> {
|
) -> Result<JoinedProject> {
|
||||||
let connection = self
|
let connection = self
|
||||||
.connections
|
.connections
|
||||||
.get_mut(&connection_id)
|
.get_mut(&connection_id)
|
||||||
@@ -498,7 +498,7 @@ impl Store {
|
|||||||
&mut self,
|
&mut self,
|
||||||
connection_id: ConnectionId,
|
connection_id: ConnectionId,
|
||||||
project_id: u64,
|
project_id: u64,
|
||||||
) -> tide::Result<LeftProject> {
|
) -> Result<LeftProject> {
|
||||||
let project = self
|
let project = self
|
||||||
.projects
|
.projects
|
||||||
.get_mut(&project_id)
|
.get_mut(&project_id)
|
||||||
@@ -533,7 +533,7 @@ impl Store {
|
|||||||
worktree_id: u64,
|
worktree_id: u64,
|
||||||
removed_entries: &[u64],
|
removed_entries: &[u64],
|
||||||
updated_entries: &[proto::Entry],
|
updated_entries: &[proto::Entry],
|
||||||
) -> tide::Result<Vec<ConnectionId>> {
|
) -> Result<Vec<ConnectionId>> {
|
||||||
let project = self.write_project(project_id, connection_id)?;
|
let project = self.write_project(project_id, connection_id)?;
|
||||||
let worktree = project
|
let worktree = project
|
||||||
.share_mut()?
|
.share_mut()?
|
||||||
@@ -554,13 +554,13 @@ impl Store {
|
|||||||
&self,
|
&self,
|
||||||
project_id: u64,
|
project_id: u64,
|
||||||
acting_connection_id: ConnectionId,
|
acting_connection_id: ConnectionId,
|
||||||
) -> tide::Result<Vec<ConnectionId>> {
|
) -> Result<Vec<ConnectionId>> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.read_project(project_id, acting_connection_id)?
|
.read_project(project_id, acting_connection_id)?
|
||||||
.connection_ids())
|
.connection_ids())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn channel_connection_ids(&self, channel_id: ChannelId) -> tide::Result<Vec<ConnectionId>> {
|
pub fn channel_connection_ids(&self, channel_id: ChannelId) -> Result<Vec<ConnectionId>> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.channels
|
.channels
|
||||||
.get(&channel_id)
|
.get(&channel_id)
|
||||||
@@ -573,11 +573,7 @@ impl Store {
|
|||||||
self.projects.get(&project_id)
|
self.projects.get(&project_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_project(
|
pub fn read_project(&self, project_id: u64, connection_id: ConnectionId) -> Result<&Project> {
|
||||||
&self,
|
|
||||||
project_id: u64,
|
|
||||||
connection_id: ConnectionId,
|
|
||||||
) -> tide::Result<&Project> {
|
|
||||||
let project = self
|
let project = self
|
||||||
.projects
|
.projects
|
||||||
.get(&project_id)
|
.get(&project_id)
|
||||||
@@ -600,7 +596,7 @@ impl Store {
|
|||||||
&mut self,
|
&mut self,
|
||||||
project_id: u64,
|
project_id: u64,
|
||||||
connection_id: ConnectionId,
|
connection_id: ConnectionId,
|
||||||
) -> tide::Result<&mut Project> {
|
) -> Result<&mut Project> {
|
||||||
let project = self
|
let project = self
|
||||||
.projects
|
.projects
|
||||||
.get_mut(&project_id)
|
.get_mut(&project_id)
|
||||||
@@ -755,14 +751,14 @@ impl Project {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn share(&self) -> tide::Result<&ProjectShare> {
|
pub fn share(&self) -> Result<&ProjectShare> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.share
|
.share
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.ok_or_else(|| anyhow!("worktree is not shared"))?)
|
.ok_or_else(|| anyhow!("worktree is not shared"))?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn share_mut(&mut self) -> tide::Result<&mut ProjectShare> {
|
fn share_mut(&mut self) -> Result<&mut ProjectShare> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.share
|
.share
|
||||||
.as_mut()
|
.as_mut()
|
||||||
|
|||||||
+28
-31
@@ -1,14 +1,14 @@
|
|||||||
use async_tungstenite::tungstenite::{Error as WebSocketError, Message as WebSocketMessage};
|
use async_tungstenite::tungstenite::Message as WebSocketMessage;
|
||||||
use futures::{SinkExt as _, StreamExt as _};
|
use futures::{SinkExt as _, StreamExt as _};
|
||||||
|
|
||||||
pub struct Connection {
|
pub struct Connection {
|
||||||
pub(crate) tx:
|
pub(crate) tx:
|
||||||
Box<dyn 'static + Send + Unpin + futures::Sink<WebSocketMessage, Error = WebSocketError>>,
|
Box<dyn 'static + Send + Unpin + futures::Sink<WebSocketMessage, Error = anyhow::Error>>,
|
||||||
pub(crate) rx: Box<
|
pub(crate) rx: Box<
|
||||||
dyn 'static
|
dyn 'static
|
||||||
+ Send
|
+ Send
|
||||||
+ Unpin
|
+ Unpin
|
||||||
+ futures::Stream<Item = Result<WebSocketMessage, WebSocketError>>,
|
+ futures::Stream<Item = Result<WebSocketMessage, anyhow::Error>>,
|
||||||
>,
|
>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -18,8 +18,8 @@ impl Connection {
|
|||||||
S: 'static
|
S: 'static
|
||||||
+ Send
|
+ Send
|
||||||
+ Unpin
|
+ Unpin
|
||||||
+ futures::Sink<WebSocketMessage, Error = WebSocketError>
|
+ futures::Sink<WebSocketMessage, Error = anyhow::Error>
|
||||||
+ futures::Stream<Item = Result<WebSocketMessage, WebSocketError>>,
|
+ futures::Stream<Item = Result<WebSocketMessage, anyhow::Error>>,
|
||||||
{
|
{
|
||||||
let (tx, rx) = stream.split();
|
let (tx, rx) = stream.split();
|
||||||
Self {
|
Self {
|
||||||
@@ -28,7 +28,7 @@ impl Connection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send(&mut self, message: WebSocketMessage) -> Result<(), WebSocketError> {
|
pub async fn send(&mut self, message: WebSocketMessage) -> Result<(), anyhow::Error> {
|
||||||
self.tx.send(message).await
|
self.tx.send(message).await
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,40 +54,37 @@ impl Connection {
|
|||||||
killed: Arc<AtomicBool>,
|
killed: Arc<AtomicBool>,
|
||||||
executor: Arc<gpui::executor::Background>,
|
executor: Arc<gpui::executor::Background>,
|
||||||
) -> (
|
) -> (
|
||||||
Box<dyn Send + Unpin + futures::Sink<WebSocketMessage, Error = WebSocketError>>,
|
Box<dyn Send + Unpin + futures::Sink<WebSocketMessage, Error = anyhow::Error>>,
|
||||||
Box<
|
Box<dyn Send + Unpin + futures::Stream<Item = Result<WebSocketMessage, anyhow::Error>>>,
|
||||||
dyn Send + Unpin + futures::Stream<Item = Result<WebSocketMessage, WebSocketError>>,
|
|
||||||
>,
|
|
||||||
) {
|
) {
|
||||||
|
use anyhow::anyhow;
|
||||||
use futures::channel::mpsc;
|
use futures::channel::mpsc;
|
||||||
use std::io::{Error, ErrorKind};
|
use std::io::{Error, ErrorKind};
|
||||||
|
|
||||||
let (tx, rx) = mpsc::unbounded::<WebSocketMessage>();
|
let (tx, rx) = mpsc::unbounded::<WebSocketMessage>();
|
||||||
|
|
||||||
let tx = tx
|
let tx = tx.sink_map_err(|error| anyhow!(error)).with({
|
||||||
.sink_map_err(|e| WebSocketError::from(Error::new(ErrorKind::Other, e)))
|
let killed = killed.clone();
|
||||||
.with({
|
let executor = Arc::downgrade(&executor);
|
||||||
|
move |msg| {
|
||||||
let killed = killed.clone();
|
let killed = killed.clone();
|
||||||
let executor = Arc::downgrade(&executor);
|
let executor = executor.clone();
|
||||||
move |msg| {
|
Box::pin(async move {
|
||||||
let killed = killed.clone();
|
if let Some(executor) = executor.upgrade() {
|
||||||
let executor = executor.clone();
|
executor.simulate_random_delay().await;
|
||||||
Box::pin(async move {
|
}
|
||||||
if let Some(executor) = executor.upgrade() {
|
|
||||||
executor.simulate_random_delay().await;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Writes to a half-open TCP connection will error.
|
// Writes to a half-open TCP connection will error.
|
||||||
if killed.load(SeqCst) {
|
if killed.load(SeqCst) {
|
||||||
std::io::Result::Err(
|
std::io::Result::Err(
|
||||||
Error::new(ErrorKind::Other, "connection lost").into(),
|
Error::new(ErrorKind::Other, "connection lost").into(),
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(msg)
|
Ok(msg)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let rx = rx.then({
|
let rx = rx.then({
|
||||||
let killed = killed.clone();
|
let killed = killed.clone();
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use super::{ConnectionId, PeerId, TypedEnvelope};
|
use super::{ConnectionId, PeerId, TypedEnvelope};
|
||||||
use anyhow::Result;
|
use anyhow::{anyhow, Result};
|
||||||
use async_tungstenite::tungstenite::{Error as WebSocketError, Message as WebSocketMessage};
|
use async_tungstenite::tungstenite::Message as WebSocketMessage;
|
||||||
use futures::{SinkExt as _, StreamExt as _};
|
use futures::{SinkExt as _, StreamExt as _};
|
||||||
use prost::Message as _;
|
use prost::Message as _;
|
||||||
use std::any::{Any, TypeId};
|
use std::any::{Any, TypeId};
|
||||||
@@ -318,9 +318,9 @@ impl<S> MessageStream<S> {
|
|||||||
|
|
||||||
impl<S> MessageStream<S>
|
impl<S> MessageStream<S>
|
||||||
where
|
where
|
||||||
S: futures::Sink<WebSocketMessage, Error = WebSocketError> + Unpin,
|
S: futures::Sink<WebSocketMessage, Error = anyhow::Error> + Unpin,
|
||||||
{
|
{
|
||||||
pub async fn write(&mut self, message: Message) -> Result<(), WebSocketError> {
|
pub async fn write(&mut self, message: Message) -> Result<(), anyhow::Error> {
|
||||||
#[cfg(any(test, feature = "test-support"))]
|
#[cfg(any(test, feature = "test-support"))]
|
||||||
const COMPRESSION_LEVEL: i32 = -7;
|
const COMPRESSION_LEVEL: i32 = -7;
|
||||||
|
|
||||||
@@ -357,9 +357,9 @@ where
|
|||||||
|
|
||||||
impl<S> MessageStream<S>
|
impl<S> MessageStream<S>
|
||||||
where
|
where
|
||||||
S: futures::Stream<Item = Result<WebSocketMessage, WebSocketError>> + Unpin,
|
S: futures::Stream<Item = Result<WebSocketMessage, anyhow::Error>> + Unpin,
|
||||||
{
|
{
|
||||||
pub async fn read(&mut self) -> Result<Message, WebSocketError> {
|
pub async fn read(&mut self) -> Result<Message, anyhow::Error> {
|
||||||
while let Some(bytes) = self.stream.next().await {
|
while let Some(bytes) = self.stream.next().await {
|
||||||
match bytes? {
|
match bytes? {
|
||||||
WebSocketMessage::Binary(bytes) => {
|
WebSocketMessage::Binary(bytes) => {
|
||||||
@@ -375,7 +375,7 @@ where
|
|||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(WebSocketError::ConnectionClosed)
|
Err(anyhow!("connection closed"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user