Skip to content

Commit

Permalink
Merge pull request #146 from olegchir/fix-actix-example-blocking
Browse files Browse the repository at this point in the history
Fix 'Cannot drop a runtime' error
  • Loading branch information
HeroicKatora authored Jul 9, 2022
2 parents 5e3ce6f + a2f212f commit 624ac86
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
1 change: 1 addition & 0 deletions Contributors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ ParisLiakos (Paris Liakos, gotham frontend)
asonix <asonix@asonix.dog> (actix 1.0 update)
Geobert Quach (on behalf of Isode Ltd.)
robjtede <robjtede@icloud.com> (actix-web 3.0 update)
Oleg Chirukhin <oleg@jetstorm.ru>
1 change: 1 addition & 0 deletions oxide-auth-actix/examples/actix-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ serde = "1.0"
serde_json = "1.0"
url = "2"
serde_urlencoded = "0.7"
tokio = "1.16.1"
30 changes: 25 additions & 5 deletions oxide-auth-actix/examples/actix-example/src/support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,41 @@ async fn endpoint_impl(
Some(code) => code.clone(),
};

match state.authorize(&code) {
Ok(()) => HttpResponse::Found().append_header(("Location", "/")).finish(),
let auth_handle = tokio::task::spawn_blocking(move || {
let res = state.authorize(&code);
res
});
let auth_result = auth_handle.await.unwrap();

match auth_result {
Ok(()) => {HttpResponse::Found().append_header(("Location", "/")).finish()},
Err(err) => HttpResponse::InternalServerError().body(format!("{}", err)),
}
}

async fn refresh(state: web::Data<Client>) -> impl Responder {
match state.refresh() {
let refresh_handle = tokio::task::spawn_blocking(move || {
let res = state.refresh();
res
});
let refresh_result = refresh_handle.await.unwrap();

match refresh_result {
Ok(()) => HttpResponse::Found().append_header(("Location", "/")).finish(),
Err(err) => HttpResponse::InternalServerError().body(format!("{}", err)),
}
}

async fn get_with_token(state: web::Data<Client>) -> impl Responder {
let protected_page = match state.retrieve_protected_page() {
let html = state.as_html();

let protected_page_handle = tokio::task::spawn_blocking(move || {
let res = state.retrieve_protected_page();
res
});
let protected_page_result = protected_page_handle.await.unwrap();

let protected_page = match protected_page_result {
Ok(page) => page,
Err(err) => return HttpResponse::InternalServerError().body(format!("{}", err)),
};
Expand All @@ -81,7 +101,7 @@ async fn get_with_token(state: web::Data<Client>) -> impl Responder {
Its contents are:
<article>{}</article>
<form action=\"refresh\" method=\"post\"><button>Refresh token</button></form>
</main></html>", state.as_html(), protected_page);
</main></html>", html, protected_page);

HttpResponse::Ok().content_type("text/html").body(display_page)
}

0 comments on commit 624ac86

Please sign in to comment.