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

@ -36,8 +36,7 @@ async fn get_user(
if let Some(user) = user {
Ok(HttpResponse::Ok().json(user))
} else {
let res = HttpResponse::NotFound()
.body(format!("No user found with uid: {}", user_uid));
let res = HttpResponse::NotFound().body(format!("No user found with uid: {}", user_uid));
Ok(res)
}
}

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");

View File

@ -46,9 +46,7 @@ async fn cache_stuff(
}
}
async fn del_stuff(
redis: web::Data<Addr<RedisActor>>,
) -> actix_web::Result<HttpResponse> {
async fn del_stuff(redis: web::Data<Addr<RedisActor>>) -> actix_web::Result<HttpResponse> {
let res = redis
.send(Command(resp_array![
"DEL",

View File

@ -63,13 +63,8 @@ async fn main() -> io::Result<()> {
// store db pool as Data object
.app_data(web::Data::new(pool.clone()))
.wrap(middleware::Logger::default())
.service(
web::resource("/asyncio_weather").route(web::get().to(asyncio_weather)),
)
.service(
web::resource("/parallel_weather")
.route(web::get().to(parallel_weather)),
)
.service(web::resource("/asyncio_weather").route(web::get().to(asyncio_weather)))
.service(web::resource("/parallel_weather").route(web::get().to(parallel_weather)))
})
.bind(("127.0.0.1", 8080))?
.workers(2)