1
0
mirror of https://github.com/actix/examples synced 2025-02-17 07:23:29 +01:00

resolved formatting

This commit is contained in:
dowwie 2020-01-31 07:21:39 -05:00
parent da72aef69c
commit 04cebf6899
4 changed files with 19 additions and 23 deletions

View File

@ -1,6 +1,6 @@
mod config { mod config {
use serde::Deserialize;
pub use ::config::ConfigError; pub use ::config::ConfigError;
use serde::Deserialize;
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct Config { pub struct Config {
pub server_addr: String, pub server_addr: String,
@ -49,7 +49,9 @@ mod errors {
fn error_response(&self) -> HttpResponse { fn error_response(&self) -> HttpResponse {
match *self { match *self {
MyError::NotFound => HttpResponse::NotFound().finish(), MyError::NotFound => HttpResponse::NotFound().finish(),
MyError::PoolError(ref err) => HttpResponse::InternalServerError().body(err.to_string()), MyError::PoolError(ref err) => {
HttpResponse::InternalServerError().body(err.to_string())
}
_ => HttpResponse::InternalServerError().finish(), _ => HttpResponse::InternalServerError().finish(),
} }
} }
@ -98,7 +100,7 @@ mod handlers {
let client: Client = let client: Client =
db_pool.get().await.map_err(|err| MyError::PoolError(err))?; db_pool.get().await.map_err(|err| MyError::PoolError(err))?;
let new_user = db::add_user(&client, user_info).await?; let new_user = db::add_user(&client, user_info).await?;
Ok(HttpResponse::Ok().json(new_user)) Ok(HttpResponse::Ok().json(new_user))
@ -108,9 +110,8 @@ mod handlers {
use actix_web::{web, App, HttpServer}; use actix_web::{web, App, HttpServer};
use dotenv::dotenv; use dotenv::dotenv;
use handlers::add_user; use handlers::add_user;
use tokio_postgres::NoTls;
use tokio::signal::unix::{signal, SignalKind}; use tokio::signal::unix::{signal, SignalKind};
use tokio_postgres::NoTls;
#[actix_rt::main] #[actix_rt::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
@ -129,14 +130,14 @@ async fn main() -> std::io::Result<()> {
println!("Server running at http://{}/", config.server_addr); println!("Server running at http://{}/", config.server_addr);
let srv = server.clone(); let srv = server.clone();
let mut stream = signal(SignalKind::interrupt())?; let mut stream = signal(SignalKind::interrupt())?;
actix_rt::spawn(async move { actix_rt::spawn(async move {
loop { loop {
stream.recv().await; stream.recv().await;
println!("\nSIGINT Received. Stopping server.\n"); println!("\nSIGINT Received. Stopping server.\n");
srv.stop(true).await; srv.stop(true).await;
} }
}); });
server.await server.await
} }

View File

@ -92,13 +92,11 @@ async fn main() -> io::Result<()> {
_ => HttpResponse::NotFound(), _ => HttpResponse::NotFound(),
}), }),
) )
.service(web::resource("/error").to(|| { .service(web::resource("/error").to(|| async {
async { error::InternalError::new(
error::InternalError::new( io::Error::new(io::ErrorKind::Other, "test"),
io::Error::new(io::ErrorKind::Other, "test"), StatusCode::INTERNAL_SERVER_ERROR,
StatusCode::INTERNAL_SERVER_ERROR, )
)
}
})) }))
// static files // static files
.service(fs::Files::new("/static", "static").show_files_listing()) .service(fs::Files::new("/static", "static").show_files_listing())

View File

@ -30,13 +30,11 @@ async fn main() -> std::io::Result<()> {
res res
}) })
}) })
.service(web::resource("/login").to(|| { .service(web::resource("/login").to(|| async {
async { "You are on /login. Go to src/redirect.rs to change this behavior."
"You are on /login. Go to src/redirect.rs to change this behavior."
}
})) }))
.service(web::resource("/").to(|| { .service(web::resource("/").to(|| async {
async { "Hello, middleware! Check the console where the server is run." } "Hello, middleware! Check the console where the server is run."
})) }))
}) })
.bind("127.0.0.1:8080")? .bind("127.0.0.1:8080")?

View File

@ -1,9 +1,8 @@
use std::{sync::mpsc, thread};
use actix_web::{get, middleware, post, web, App, HttpResponse, HttpServer}; use actix_web::{get, middleware, post, web, App, HttpResponse, HttpServer};
use futures::executor; use futures::executor;
use std::{sync::mpsc, thread};
use tokio::signal::unix::{signal, SignalKind}; use tokio::signal::unix::{signal, SignalKind};
#[get("/hello")] #[get("/hello")]
async fn hello() -> &'static str { async fn hello() -> &'static str {
"Hello world!" "Hello world!"
@ -59,7 +58,7 @@ async fn main() -> std::io::Result<()> {
println!("\n*** SIGINT received. Stopping server, gracefully. ***\n"); println!("\n*** SIGINT received. Stopping server, gracefully. ***\n");
stopper.send(()).unwrap(); stopper.send(()).unwrap();
} }
}); });
// run server // run server
server.await server.await