From 02bcbc42d6a2f218cbc67a5d3d5a0ed26c065afc Mon Sep 17 00:00:00 2001 From: Felix Date: Sat, 11 Jan 2020 13:30:45 +0100 Subject: [PATCH] Make various functions async --- server/Cargo.lock | 1 + server/Cargo.toml | 1 + server/src/apub/community.rs | 4 ++-- server/src/apub/user.rs | 2 +- server/src/main.rs | 19 +++++++++++-------- server/src/routes/feeds.rs | 7 +++++-- server/src/routes/nodeinfo.rs | 4 ++-- server/src/routes/webfinger.rs | 2 +- 8 files changed, 24 insertions(+), 16 deletions(-) diff --git a/server/Cargo.lock b/server/Cargo.lock index ea5d172a4..16a51fab8 100644 --- a/server/Cargo.lock +++ b/server/Cargo.lock @@ -1298,6 +1298,7 @@ dependencies = [ "activitypub 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "actix 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "actix-files 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-rt 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "actix-web 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "actix-web-actors 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "bcrypt 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/server/Cargo.toml b/server/Cargo.toml index a4d068ef1..5d00d6ee5 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -18,6 +18,7 @@ actix = "0.9.0" actix-web = "2.0.0" actix-files = "0.2.1" actix-web-actors = "2.0.0" +actix-rt = "1.0.0" env_logger = "0.7.1" rand = "0.7.0" strum = "0.17.1" diff --git a/server/src/apub/community.rs b/server/src/apub/community.rs index 504e17907..fac6088e4 100644 --- a/server/src/apub/community.rs +++ b/server/src/apub/community.rs @@ -84,7 +84,7 @@ pub struct CommunityQuery { community_name: String, } -pub fn get_apub_community(info: Path) -> HttpResponse { +pub async fn get_apub_community(info: Path) -> HttpResponse { let connection = establish_connection(); if let Ok(community) = Community::read_from_name(&connection, info.community_name.to_owned()) { @@ -96,7 +96,7 @@ pub fn get_apub_community(info: Path) -> HttpResponse { } } -pub fn get_apub_community_followers(info: Path) -> HttpResponse { +pub async fn get_apub_community_followers(info: Path) -> HttpResponse { let connection = establish_connection(); if let Ok(community) = Community::read_from_name(&connection, info.community_name.to_owned()) { diff --git a/server/src/apub/user.rs b/server/src/apub/user.rs index 9de2c36c3..cf9a9797c 100644 --- a/server/src/apub/user.rs +++ b/server/src/apub/user.rs @@ -61,7 +61,7 @@ pub struct UserQuery { user_name: String, } -pub fn get_apub_user(info: Path) -> HttpResponse { +pub async fn get_apub_user(info: Path) -> HttpResponse { let connection = establish_connection(); if let Ok(user) = User_::find_by_email_or_username(&connection, &info.user_name) { diff --git a/server/src/main.rs b/server/src/main.rs index 8ee9f4ef8..763f540fb 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -6,12 +6,13 @@ use actix_web::*; use lemmy_server::db::establish_connection; use lemmy_server::routes::{federation, feeds, index, nodeinfo, webfinger, websocket}; use lemmy_server::settings::Settings; +use std::io; embed_migrations!(); -fn main() { +#[actix_rt::main] +async fn main() -> io::Result<()> { env_logger::init(); - let sys = actix::System::new("lemmy"); // Run the migrations from code let conn = establish_connection(); @@ -19,6 +20,11 @@ fn main() { let settings = Settings::get(); + println!( + "Starting http server at {}:{}", + settings.bind, settings.port + ); + // Create Http server with websocket support HttpServer::new(move || { App::new() @@ -37,10 +43,7 @@ fn main() { settings.front_end_dir.to_owned() + "/documentation", )) }) - .bind((settings.bind, settings.port)) - .unwrap() - .run(); - - println!("Started http server at {}:{}", settings.bind, settings.port); - let _ = sys.run(); + .bind((settings.bind, settings.port))? + .run() + .await } diff --git a/server/src/routes/feeds.rs b/server/src/routes/feeds.rs index 0b2ccac19..ae1631e2d 100644 --- a/server/src/routes/feeds.rs +++ b/server/src/routes/feeds.rs @@ -37,7 +37,7 @@ pub fn config(cfg: &mut web::ServiceConfig) { .route("/feeds/all.xml", web::get().to(feeds::get_all_feed)); } -fn get_all_feed(info: web::Query) -> HttpResponse { +async fn get_all_feed(info: web::Query) -> HttpResponse { let sort_type = match get_sort_type(info) { Ok(sort_type) => sort_type, Err(_) => return HttpResponse::BadRequest().finish(), @@ -53,7 +53,10 @@ fn get_all_feed(info: web::Query) -> HttpResponse { } } -fn get_feed(path: web::Path<(String, String)>, info: web::Query) -> HttpResponse { +async fn get_feed( + path: web::Path<(String, String)>, + info: web::Query, +) -> HttpResponse { let sort_type = match get_sort_type(info) { Ok(sort_type) => sort_type, Err(_) => return HttpResponse::BadRequest().finish(), diff --git a/server/src/routes/nodeinfo.rs b/server/src/routes/nodeinfo.rs index 246596083..2b7135fba 100644 --- a/server/src/routes/nodeinfo.rs +++ b/server/src/routes/nodeinfo.rs @@ -13,7 +13,7 @@ pub fn config(cfg: &mut web::ServiceConfig) { .route("/.well-known/nodeinfo", web::get().to(node_info_well_known)); } -pub fn node_info_well_known() -> HttpResponse { +async fn node_info_well_known() -> HttpResponse { let json = json!({ "links": { "rel": "http://nodeinfo.diaspora.software/ns/schema/2.0", @@ -26,7 +26,7 @@ pub fn node_info_well_known() -> HttpResponse { .body(json.to_string()) } -fn node_info() -> HttpResponse { +async fn node_info() -> HttpResponse { let conn = establish_connection(); let site_view = match SiteView::read(&conn) { Ok(site_view) => site_view, diff --git a/server/src/routes/webfinger.rs b/server/src/routes/webfinger.rs index f013f3efe..c538f5b1d 100644 --- a/server/src/routes/webfinger.rs +++ b/server/src/routes/webfinger.rs @@ -37,7 +37,7 @@ lazy_static! { /// /// You can also view the webfinger response that Mastodon sends: /// https://radical.town/.well-known/webfinger?resource=acct:felix@radical.town -fn get_webfinger_response(info: Query) -> HttpResponse { +async fn get_webfinger_response(info: Query) -> HttpResponse { let regex_parsed = WEBFINGER_COMMUNITY_REGEX .captures(&info.resource) .map(|c| c.get(1));