1
0
mirror of https://github.com/actix/examples synced 2025-06-26 17:17:42 +02:00
This commit is contained in:
Rob Ede
2022-02-18 02:44:02 +00:00
parent aca1dab890
commit fbd3b228e9
48 changed files with 103 additions and 261 deletions

View File

@ -25,10 +25,7 @@ async fn add_user(client: web::Data<Client>, form: web::Form<User>) -> HttpRespo
/// Gets the user with the supplied username.
#[get("/get_user/{username}")]
async fn get_user(
client: web::Data<Client>,
username: web::Path<String>,
) -> HttpResponse {
async fn get_user(client: web::Data<Client>, username: web::Path<String>) -> HttpResponse {
let username = username.into_inner();
let collection: Collection<User> = client.database(DB_NAME).collection(COLL_NAME);
match collection
@ -36,8 +33,9 @@ async fn get_user(
.await
{
Ok(Some(user)) => HttpResponse::Ok().json(user),
Ok(None) => HttpResponse::NotFound()
.body(format!("No user found with username {}", username)),
Ok(None) => {
HttpResponse::NotFound().body(format!("No user found with username {}", username))
}
Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
}
}
@ -59,8 +57,7 @@ async fn create_username_index(client: &Client) {
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let uri = std::env::var("MONGODB_URI")
.unwrap_or_else(|_| "mongodb://localhost:27017".into());
let uri = std::env::var("MONGODB_URI").unwrap_or_else(|_| "mongodb://localhost:27017".into());
let client = Client::with_uri_str(uri).await.expect("failed to connect");
create_username_index(&client).await;

View File

@ -9,8 +9,7 @@ use super::*;
#[actix_web::test]
#[ignore = "requires MongoDB instance running"]
async fn test() {
let uri = std::env::var("MONGODB_URI")
.unwrap_or_else(|_| "mongodb://localhost:27017".into());
let uri = std::env::var("MONGODB_URI").unwrap_or_else(|_| "mongodb://localhost:27017".into());
let client = Client::with_uri_str(uri).await.expect("failed to connect");