mirror of
https://github.com/actix/examples
synced 2024-11-23 22:41:07 +01:00
s/str::to_string/str::to_owned
This commit is contained in:
parent
9a5cd90634
commit
8fd4262136
@ -124,7 +124,7 @@ mod test {
|
|||||||
RedisActorSessionStore::new("127.0.0.1:6379"),
|
RedisActorSessionStore::new("127.0.0.1:6379"),
|
||||||
private_key.clone(),
|
private_key.clone(),
|
||||||
)
|
)
|
||||||
.cookie_name("test-session".to_string())
|
.cookie_name("test-session".to_owned())
|
||||||
.build(),
|
.build(),
|
||||||
)
|
)
|
||||||
.wrap(middleware::Logger::default())
|
.wrap(middleware::Logger::default())
|
||||||
|
@ -45,7 +45,7 @@ impl From<DBError> for ServiceError {
|
|||||||
match error {
|
match error {
|
||||||
DBError::DatabaseError(kind, info) => {
|
DBError::DatabaseError(kind, info) => {
|
||||||
if let DatabaseErrorKind::UniqueViolation = kind {
|
if let DatabaseErrorKind::UniqueViolation = kind {
|
||||||
let message = info.details().unwrap_or_else(|| info.message()).to_string();
|
let message = info.details().unwrap_or_else(|| info.message()).to_owned();
|
||||||
return ServiceError::BadRequest(message);
|
return ServiceError::BadRequest(message);
|
||||||
}
|
}
|
||||||
ServiceError::InternalServerError
|
ServiceError::InternalServerError
|
||||||
|
@ -28,7 +28,7 @@ async fn main() -> std::io::Result<()> {
|
|||||||
let pool: models::Pool = r2d2::Pool::builder()
|
let pool: models::Pool = r2d2::Pool::builder()
|
||||||
.build(manager)
|
.build(manager)
|
||||||
.expect("Failed to create pool.");
|
.expect("Failed to create pool.");
|
||||||
let domain: String = std::env::var("DOMAIN").unwrap_or_else(|_| "localhost".to_string());
|
let domain: String = std::env::var("DOMAIN").unwrap_or_else(|_| "localhost".to_owned());
|
||||||
|
|
||||||
log::info!("starting HTTP server at http://localhost:8080");
|
log::info!("starting HTTP server at http://localhost:8080");
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ fn app_config(config: &mut web::ServiceConfig) {
|
|||||||
config.service(
|
config.service(
|
||||||
web::scope("")
|
web::scope("")
|
||||||
.app_data(web::Data::new(AppState {
|
.app_data(web::Data::new(AppState {
|
||||||
foo: "bar".to_string(),
|
foo: "bar".to_owned(),
|
||||||
}))
|
}))
|
||||||
.service(web::resource("/").route(web::get().to(index)))
|
.service(web::resource("/").route(web::get().to(index)))
|
||||||
.service(web::resource("/post1").route(web::post().to(handle_post_1)))
|
.service(web::resource("/post1").route(web::post().to(handle_post_1)))
|
||||||
@ -96,7 +96,7 @@ mod tests {
|
|||||||
#[actix_web::test]
|
#[actix_web::test]
|
||||||
async fn handle_post_1_unit_test() {
|
async fn handle_post_1_unit_test() {
|
||||||
let params = Form(MyParams {
|
let params = Form(MyParams {
|
||||||
name: "John".to_string(),
|
name: "John".to_owned(),
|
||||||
});
|
});
|
||||||
let resp = handle_post_1(params).await.unwrap();
|
let resp = handle_post_1(params).await.unwrap();
|
||||||
|
|
||||||
@ -116,7 +116,7 @@ mod tests {
|
|||||||
let req = test::TestRequest::post()
|
let req = test::TestRequest::post()
|
||||||
.uri("/post1")
|
.uri("/post1")
|
||||||
.set_form(MyParams {
|
.set_form(MyParams {
|
||||||
name: "John".to_string(),
|
name: "John".to_owned(),
|
||||||
})
|
})
|
||||||
.to_request();
|
.to_request();
|
||||||
let resp: ServiceResponse = test::call_service(&app, req).await;
|
let resp: ServiceResponse = test::call_service(&app, req).await;
|
||||||
@ -134,12 +134,12 @@ mod tests {
|
|||||||
async fn handle_post_2_unit_test() {
|
async fn handle_post_2_unit_test() {
|
||||||
let state = TestRequest::default()
|
let state = TestRequest::default()
|
||||||
.data(AppState {
|
.data(AppState {
|
||||||
foo: "bar".to_string(),
|
foo: "bar".to_owned(),
|
||||||
})
|
})
|
||||||
.to_http_request();
|
.to_http_request();
|
||||||
let data = state.app_data::<actix_web::web::Data<AppState>>().unwrap();
|
let data = state.app_data::<actix_web::web::Data<AppState>>().unwrap();
|
||||||
let params = Form(MyParams {
|
let params = Form(MyParams {
|
||||||
name: "John".to_string(),
|
name: "John".to_owned(),
|
||||||
});
|
});
|
||||||
let resp = handle_post_2(data.clone(), params).await.unwrap();
|
let resp = handle_post_2(data.clone(), params).await.unwrap();
|
||||||
|
|
||||||
@ -161,7 +161,7 @@ mod tests {
|
|||||||
let req = test::TestRequest::post()
|
let req = test::TestRequest::post()
|
||||||
.uri("/post2")
|
.uri("/post2")
|
||||||
.set_form(MyParams {
|
.set_form(MyParams {
|
||||||
name: "John".to_string(),
|
name: "John".to_owned(),
|
||||||
})
|
})
|
||||||
.to_request();
|
.to_request();
|
||||||
let resp: ServiceResponse = test::call_service(&app, req).await;
|
let resp: ServiceResponse = test::call_service(&app, req).await;
|
||||||
@ -183,7 +183,7 @@ mod tests {
|
|||||||
async fn handle_post_3_unit_test() {
|
async fn handle_post_3_unit_test() {
|
||||||
let req = TestRequest::default().to_http_request();
|
let req = TestRequest::default().to_http_request();
|
||||||
let params = Form(MyParams {
|
let params = Form(MyParams {
|
||||||
name: "John".to_string(),
|
name: "John".to_owned(),
|
||||||
});
|
});
|
||||||
let result = handle_post_3(req.clone(), params).await;
|
let result = handle_post_3(req.clone(), params).await;
|
||||||
let resp = result.respond_to(&req);
|
let resp = result.respond_to(&req);
|
||||||
@ -205,7 +205,7 @@ mod tests {
|
|||||||
let req = test::TestRequest::post()
|
let req = test::TestRequest::post()
|
||||||
.uri("/post3")
|
.uri("/post3")
|
||||||
.set_form(MyParams {
|
.set_form(MyParams {
|
||||||
name: "John".to_string(),
|
name: "John".to_owned(),
|
||||||
})
|
})
|
||||||
.to_request();
|
.to_request();
|
||||||
let resp: ServiceResponse = test::call_service(&app, req).await;
|
let resp: ServiceResponse = test::call_service(&app, req).await;
|
||||||
|
@ -14,7 +14,7 @@ pub async fn gen_tls_cert(user_email: &str, user_domain: &str) -> anyhow::Result
|
|||||||
// Create acme-challenge dir.
|
// Create acme-challenge dir.
|
||||||
fs::create_dir("./acme-challenge").unwrap();
|
fs::create_dir("./acme-challenge").unwrap();
|
||||||
|
|
||||||
let domain = user_domain.to_string();
|
let domain = user_domain.to_owned();
|
||||||
|
|
||||||
// Create temporary Actix Web server for ACME challenge.
|
// Create temporary Actix Web server for ACME challenge.
|
||||||
let srv = HttpServer::new(|| {
|
let srv = HttpServer::new(|| {
|
||||||
|
@ -31,7 +31,7 @@ impl ResponseError for Error {
|
|||||||
|
|
||||||
async fn index() -> Result<HttpResponse, Error> {
|
async fn index() -> Result<HttpResponse, Error> {
|
||||||
Err(Error {
|
Err(Error {
|
||||||
msg: "an example error message".to_string(),
|
msg: "an example error message".to_owned(),
|
||||||
status: 400,
|
status: 400,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,6 @@ use std::io;
|
|||||||
|
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
error::ErrorBadRequest,
|
error::ErrorBadRequest,
|
||||||
http::header::ContentType,
|
|
||||||
web::{self, BytesMut},
|
web::{self, BytesMut},
|
||||||
App, Error, HttpResponse, HttpServer,
|
App, Error, HttpResponse, HttpServer,
|
||||||
};
|
};
|
||||||
@ -56,7 +55,7 @@ async fn step_x(data: SomeData, client: &Client) -> actix_web::Result<SomeData>
|
|||||||
body.extend_from_slice(&chunk?);
|
body.extend_from_slice(&chunk?);
|
||||||
}
|
}
|
||||||
|
|
||||||
let body: HttpBinResponse = serde_json::from_slice(&body).unwrap();
|
let body = serde_json::from_slice::<HttpBinResponse>(&body).unwrap();
|
||||||
|
|
||||||
println!("{body:?}");
|
println!("{body:?}");
|
||||||
|
|
||||||
@ -71,9 +70,7 @@ async fn create_something(
|
|||||||
let some_data_3 = step_x(some_data_2, &client).await?;
|
let some_data_3 = step_x(some_data_2, &client).await?;
|
||||||
let d = step_x(some_data_3, &client).await?;
|
let d = step_x(some_data_3, &client).await?;
|
||||||
|
|
||||||
Ok(HttpResponse::Ok()
|
Ok(HttpResponse::Ok().json(d))
|
||||||
.content_type(ContentType::json())
|
|
||||||
.body(serde_json::to_string(&d).unwrap()))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
|
@ -30,7 +30,7 @@ async fn main() -> io::Result<()> {
|
|||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
async fn index() -> impl Responder {
|
async fn index() -> impl Responder {
|
||||||
Html(include_str!("index.html").to_string())
|
Html(include_str!("index.html").to_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/events")]
|
#[get("/events")]
|
||||||
|
@ -77,10 +77,10 @@ fn get_error_response<B>(res: &ServiceResponse<B>, error: &str) -> HttpResponse<
|
|||||||
|
|
||||||
// Provide a fallback to a simple plain text response in case an error occurs during the
|
// Provide a fallback to a simple plain text response in case an error occurs during the
|
||||||
// rendering of the error page.
|
// rendering of the error page.
|
||||||
let fallback = |e: &str| {
|
let fallback = |err: &str| {
|
||||||
HttpResponse::build(res.status())
|
HttpResponse::build(res.status())
|
||||||
.content_type(ContentType::plaintext())
|
.content_type(ContentType::plaintext())
|
||||||
.body(e.to_string())
|
.body(err.to_string())
|
||||||
};
|
};
|
||||||
|
|
||||||
let hb = request
|
let hb = request
|
||||||
|
@ -38,7 +38,7 @@ async fn page(params: web::Path<(i32,)>) -> actix_web::Result<impl Responder> {
|
|||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
async fn hello() -> impl Responder {
|
async fn hello() -> impl Responder {
|
||||||
Html("<p>Hello world!</p>".to_string())
|
Html("<p>Hello world!</p>".to_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
|
@ -71,10 +71,10 @@ fn get_error_response<B>(res: &ServiceResponse<B>, error: &str) -> HttpResponse
|
|||||||
|
|
||||||
// Provide a fallback to a simple plain text response in case an error occurs during the
|
// Provide a fallback to a simple plain text response in case an error occurs during the
|
||||||
// rendering of the error page.
|
// rendering of the error page.
|
||||||
let fallback = |e: &str| {
|
let fallback = |err: &str| {
|
||||||
HttpResponse::build(res.status())
|
HttpResponse::build(res.status())
|
||||||
.content_type(ContentType::plaintext())
|
.content_type(ContentType::plaintext())
|
||||||
.body(e.to_string())
|
.body(err.to_string())
|
||||||
};
|
};
|
||||||
|
|
||||||
let tera = request.app_data::<web::Data<Tera>>().map(|t| t.get_ref());
|
let tera = request.app_data::<web::Data<Tera>>().map(|t| t.get_ref());
|
||||||
|
@ -75,10 +75,10 @@ fn get_error_response<B>(res: &ServiceResponse<B>, error: &str) -> HttpResponse
|
|||||||
|
|
||||||
// Provide a fallback to a simple plain text response in case an error occurs during the
|
// Provide a fallback to a simple plain text response in case an error occurs during the
|
||||||
// rendering of the error page.
|
// rendering of the error page.
|
||||||
let fallback = |e: &str| {
|
let fallback = |err: &str| {
|
||||||
HttpResponse::build(res.status())
|
HttpResponse::build(res.status())
|
||||||
.content_type(ContentType::plaintext())
|
.content_type(ContentType::plaintext())
|
||||||
.body(e.to_string())
|
.body(err.to_string())
|
||||||
};
|
};
|
||||||
|
|
||||||
let tt = request
|
let tt = request
|
||||||
|
@ -76,7 +76,7 @@ impl Handler<JoinRoom> for WsChatServer {
|
|||||||
let id = self.add_client_to_room(&room_name, None, client);
|
let id = self.add_client_to_room(&room_name, None, client);
|
||||||
let join_msg = format!(
|
let join_msg = format!(
|
||||||
"{} joined {room_name}",
|
"{} joined {room_name}",
|
||||||
client_name.unwrap_or_else(|| "anon".to_string()),
|
client_name.unwrap_or_else(|| "anon".to_owned()),
|
||||||
);
|
);
|
||||||
|
|
||||||
self.send_chat_message(&room_name, &join_msg, id);
|
self.send_chat_message(&room_name, &join_msg, id);
|
||||||
|
@ -64,7 +64,7 @@ impl WsChatSession {
|
|||||||
pub fn send_msg(&self, msg: &str) {
|
pub fn send_msg(&self, msg: &str) {
|
||||||
let content = format!(
|
let content = format!(
|
||||||
"{}: {msg}",
|
"{}: {msg}",
|
||||||
self.name.clone().unwrap_or_else(|| "anon".to_string()),
|
self.name.clone().unwrap_or_else(|| "anon".to_owned()),
|
||||||
);
|
);
|
||||||
|
|
||||||
let msg = SendMessage(self.room.clone(), self.id, content);
|
let msg = SendMessage(self.room.clone(), self.id, content);
|
||||||
@ -84,7 +84,7 @@ impl Actor for WsChatSession {
|
|||||||
fn stopped(&mut self, _ctx: &mut Self::Context) {
|
fn stopped(&mut self, _ctx: &mut Self::Context) {
|
||||||
log::info!(
|
log::info!(
|
||||||
"WsChatSession closed for {}({}) in room {}",
|
"WsChatSession closed for {}({}) in room {}",
|
||||||
self.name.clone().unwrap_or_else(|| "anon".to_string()),
|
self.name.clone().unwrap_or_else(|| "anon".to_owned()),
|
||||||
self.id,
|
self.id,
|
||||||
self.room
|
self.room
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user