Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgb committed Apr 16, 2024
1 parent 84b058c commit 0a9932d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
14 changes: 12 additions & 2 deletions crates/y-sweet-worker/src/durable_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ pub struct YServe {

impl YServe {
/// We need to lazily create the doc because the constructor is non-async.
pub async fn get_doc(&mut self, req: &Request, doc_id: &str, create: bool) -> Result<&mut DocWithSyncKv> {
pub async fn get_doc(
&mut self,
req: &Request,
doc_id: &str,
create: bool,
) -> Result<&mut DocWithSyncKv> {
if self.lazy_doc.is_none() {
let mut context = ServerContext::from_request(req, &self.env).unwrap();
#[allow(clippy::arc_with_non_send_sync)] // Arc required for compatibility with core.
Expand Down Expand Up @@ -109,7 +114,12 @@ async fn websocket_connect(req: Request, ctx: RouteContext<&mut YServe>) -> Resu
server.accept()?;

let doc_id = ctx.param("doc_id").unwrap().to_owned();
let awareness = ctx.data.get_doc(&req, &doc_id, false).await.unwrap().awareness();
let awareness = ctx
.data
.get_doc(&req, &doc_id, false)
.await
.unwrap()
.awareness();

let connection = {
let server = server.clone();
Expand Down
23 changes: 16 additions & 7 deletions crates/y-sweet-worker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ use error::{Error, IntoResponse};
use serde_json::{json, Value};
use server_context::ServerContext;
use std::collections::HashMap;
use worker::{
console_log, Date, Method, Request, Response, ResponseBody, Result, RouteContext, Router, Url,
};
#[cfg(feature = "fetch-event")]
use worker::{event, Env};
use worker::{console_log, Date, Method, Request, Response, ResponseBody, Result, RouteContext, Router, Url};
use y_sweet_core::{
api_types::{validate_doc_name, ClientToken, DocCreationRequest, NewDocResponse},
auth::Authenticator,
Expand All @@ -31,7 +33,6 @@ fn get_time_millis_since_epoch() -> u64 {
pub fn router(
context: ServerContext,
) -> std::result::Result<Router<'static, ServerContext>, Error> {

Ok(Router::with_data(context)
.get("/", |_, _| Response::ok("Y-Sweet!"))
.get_async("/check_store", check_store_handler)
Expand Down Expand Up @@ -110,9 +111,15 @@ async fn new_doc(
};

console_log!("here0x");
let req = Request::new(&format!("http://ignored/doc/{}{}", doc_id, auth), Method::Post).map_err(|_| Error::CouldNotConstructRequest)?;
let req = Request::new(
&format!("http://ignored/doc/{}{}", doc_id, auth),
Method::Post,
)
.map_err(|_| Error::CouldNotConstructRequest)?;
console_log!("here1x");
let result = forward_to_durable_object_with_doc_id(req, ctx, &doc_id).await.map_err(Error::CouldNotForwardRequest)?;
let result = forward_to_durable_object_with_doc_id(req, ctx, &doc_id)
.await
.map_err(Error::CouldNotForwardRequest)?;

if result.status_code() != 200 {
let body = match result.body() {
Expand All @@ -125,7 +132,6 @@ async fn new_doc(

console_log!("here2x");


let response = NewDocResponse { doc_id };

Ok(response)
Expand Down Expand Up @@ -227,7 +233,10 @@ async fn auth_doc(
})
}

async fn forward_to_durable_object(req: Request, mut ctx: RouteContext<ServerContext>) -> Result<Response> {
async fn forward_to_durable_object(
req: Request,
mut ctx: RouteContext<ServerContext>,
) -> Result<Response> {
let doc_id = ctx.param("doc_id").unwrap().to_string();

if let Some(auth) = ctx.data.auth().unwrap() {
Expand All @@ -250,7 +259,7 @@ async fn forward_to_durable_object(req: Request, mut ctx: RouteContext<ServerCon
return e.into();
}
}

forward_to_durable_object_with_doc_id(req, ctx, &doc_id).await
}

Expand Down

0 comments on commit 0a9932d

Please # to comment.