From: Felix Date: Sat, 11 Jan 2020 12:30:45 +0000 (+0100) Subject: Make various functions async X-Git-Url: http://these/git/readmes/%7B%60%24%7BwebArchiveUrl%7D/save/static/gitweb.css?a=commitdiff_plain;h=4361f48b98fc07c6abf14ecfe54953929fe38130;p=lemmy.git Make various functions async --- diff --git a/server/Cargo.lock b/server/Cargo.lock index ea5d172a..16a51fab 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 a4d068ef..5d00d6ee 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 504e1790..fac6088e 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 9de2c36c..cf9a9797 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 8ee9f4ef..763f540f 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 0b2ccac1..ae1631e2 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 24659608..2b7135fb 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 f013f3ef..c538f5b1 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));