mirror of
https://github.com/actix/examples
synced 2024-11-23 14:31:07 +01:00
resolved formatting
This commit is contained in:
parent
da72aef69c
commit
04cebf6899
@ -1,6 +1,6 @@
|
||||
mod config {
|
||||
use serde::Deserialize;
|
||||
pub use ::config::ConfigError;
|
||||
use serde::Deserialize;
|
||||
#[derive(Deserialize)]
|
||||
pub struct Config {
|
||||
pub server_addr: String,
|
||||
@ -49,7 +49,9 @@ mod errors {
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
match *self {
|
||||
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(),
|
||||
}
|
||||
}
|
||||
@ -98,7 +100,7 @@ mod handlers {
|
||||
|
||||
let client: Client =
|
||||
db_pool.get().await.map_err(|err| MyError::PoolError(err))?;
|
||||
|
||||
|
||||
let new_user = db::add_user(&client, user_info).await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(new_user))
|
||||
@ -108,9 +110,8 @@ mod handlers {
|
||||
use actix_web::{web, App, HttpServer};
|
||||
use dotenv::dotenv;
|
||||
use handlers::add_user;
|
||||
use tokio_postgres::NoTls;
|
||||
use tokio::signal::unix::{signal, SignalKind};
|
||||
|
||||
use tokio_postgres::NoTls;
|
||||
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
@ -129,14 +130,14 @@ async fn main() -> std::io::Result<()> {
|
||||
println!("Server running at http://{}/", config.server_addr);
|
||||
|
||||
let srv = server.clone();
|
||||
let mut stream = signal(SignalKind::interrupt())?;
|
||||
let mut stream = signal(SignalKind::interrupt())?;
|
||||
actix_rt::spawn(async move {
|
||||
loop {
|
||||
stream.recv().await;
|
||||
println!("\nSIGINT Received. Stopping server.\n");
|
||||
srv.stop(true).await;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
server.await
|
||||
}
|
||||
|
@ -92,13 +92,11 @@ async fn main() -> io::Result<()> {
|
||||
_ => HttpResponse::NotFound(),
|
||||
}),
|
||||
)
|
||||
.service(web::resource("/error").to(|| {
|
||||
async {
|
||||
error::InternalError::new(
|
||||
io::Error::new(io::ErrorKind::Other, "test"),
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
)
|
||||
}
|
||||
.service(web::resource("/error").to(|| async {
|
||||
error::InternalError::new(
|
||||
io::Error::new(io::ErrorKind::Other, "test"),
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
)
|
||||
}))
|
||||
// static files
|
||||
.service(fs::Files::new("/static", "static").show_files_listing())
|
||||
|
@ -30,13 +30,11 @@ async fn main() -> std::io::Result<()> {
|
||||
res
|
||||
})
|
||||
})
|
||||
.service(web::resource("/login").to(|| {
|
||||
async {
|
||||
"You are on /login. Go to src/redirect.rs to change this behavior."
|
||||
}
|
||||
.service(web::resource("/login").to(|| async {
|
||||
"You are on /login. Go to src/redirect.rs to change this behavior."
|
||||
}))
|
||||
.service(web::resource("/").to(|| {
|
||||
async { "Hello, middleware! Check the console where the server is run." }
|
||||
.service(web::resource("/").to(|| async {
|
||||
"Hello, middleware! Check the console where the server is run."
|
||||
}))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
|
@ -1,9 +1,8 @@
|
||||
use std::{sync::mpsc, thread};
|
||||
use actix_web::{get, middleware, post, web, App, HttpResponse, HttpServer};
|
||||
use futures::executor;
|
||||
use std::{sync::mpsc, thread};
|
||||
use tokio::signal::unix::{signal, SignalKind};
|
||||
|
||||
|
||||
#[get("/hello")]
|
||||
async fn hello() -> &'static str {
|
||||
"Hello world!"
|
||||
@ -59,7 +58,7 @@ async fn main() -> std::io::Result<()> {
|
||||
println!("\n*** SIGINT received. Stopping server, gracefully. ***\n");
|
||||
stopper.send(()).unwrap();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// run server
|
||||
server.await
|
||||
|
Loading…
Reference in New Issue
Block a user