1
0
mirror of https://github.com/actix/examples synced 2025-06-26 09:17:41 +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

@ -9,8 +9,7 @@ use actix_web::{
header::{self, ContentType},
Method, StatusCode,
},
middleware, web, App, Either, HttpRequest, HttpResponse, HttpServer, Responder,
Result,
middleware, web, App, Either, HttpRequest, HttpResponse, HttpServer, Responder, Result,
};
use async_stream::stream;
@ -44,8 +43,7 @@ async fn welcome(req: HttpRequest, session: Session) -> Result<HttpResponse> {
async fn default_handler(req_method: Method) -> Result<impl Responder> {
match req_method {
Method::GET => {
let file = NamedFile::open("static/404.html")?
.set_status_code(StatusCode::NOT_FOUND);
let file = NamedFile::open("static/404.html")?.set_status_code(StatusCode::NOT_FOUND);
Ok(Either::Left(file))
}
_ => Ok(Either::Right(HttpResponse::MethodNotAllowed().finish())),
@ -93,9 +91,7 @@ async fn main() -> io::Result<()> {
// with path parameters
.service(web::resource("/user/{name}").route(web::get().to(with_param)))
// async response body
.service(
web::resource("/async-body/{name}").route(web::get().to(response_body)),
)
.service(web::resource("/async-body/{name}").route(web::get().to(response_body)))
.service(
web::resource("/test").to(|req: HttpRequest| match *req.method() {
Method::GET => HttpResponse::Ok(),
@ -112,14 +108,14 @@ async fn main() -> io::Result<()> {
// static files
.service(Files::new("/static", "static").show_files_listing())
// redirect
.service(web::resource("/").route(web::get().to(
|req: HttpRequest| async move {
.service(
web::resource("/").route(web::get().to(|req: HttpRequest| async move {
println!("{:?}", req);
HttpResponse::Found()
.insert_header((header::LOCATION, "static/welcome.html"))
.finish()
},
)))
})),
)
// default
.default_service(web::to(default_handler))
})

View File

@ -93,8 +93,7 @@ async fn main() -> std::io::Result<()> {
env_logger::init();
HttpServer::new(move || {
App::new()
.service(web::resource("/something").route(web::get().to(do_something)))
App::new().service(web::resource("/something").route(web::get().to(do_something)))
})
.bind("127.0.0.1:8088")?
.run()

View File

@ -2,15 +2,11 @@ use actix_web::{web, Error, HttpResponse};
use crate::common::{Part, Product};
pub async fn get_products(
_query: web::Query<Option<Part>>,
) -> Result<HttpResponse, Error> {
pub async fn get_products(_query: web::Query<Option<Part>>) -> Result<HttpResponse, Error> {
Ok(HttpResponse::Ok().finish())
}
pub async fn add_product(
_new_product: web::Json<Product>,
) -> Result<HttpResponse, Error> {
pub async fn add_product(_new_product: web::Json<Product>) -> Result<HttpResponse, Error> {
Ok(HttpResponse::Ok().finish())
}

View File

@ -49,10 +49,7 @@ pub async fn create(
session: Session,
) -> Result<HttpResponse, Error> {
if params.description.is_empty() {
session::set_flash(
&session,
FlashMessage::error("Description cannot be empty"),
)?;
session::set_flash(&session, FlashMessage::error("Description cannot be empty"))?;
Ok(redirect_to("/"))
} else {
db::create_task(params.into_inner().description, &pool)
@ -133,9 +130,7 @@ pub fn not_found<B>(res: dev::ServiceResponse<B>) -> Result<ErrorHandlerResponse
Ok(ErrorHandlerResponse::Response(res.into_response(new_resp)))
}
pub fn internal_server_error<B>(
res: dev::ServiceResponse<B>,
) -> Result<ErrorHandlerResponse<B>> {
pub fn internal_server_error<B>(res: dev::ServiceResponse<B>) -> Result<ErrorHandlerResponse<B>> {
let new_resp = NamedFile::open("static/errors/500.html")?
.set_status_code(res.status())
.into_response(res.request())

View File

@ -32,8 +32,7 @@ async fn main() -> io::Result<()> {
HttpServer::new(move || {
log::debug!("Constructing the App");
let mut templates =
Tera::new("templates/**/*").expect("errors in tera templates");
let mut templates = Tera::new("templates/**/*").expect("errors in tera templates");
templates.autoescape_on(vec!["tera"]);
let session_store = CookieSession::signed(SESSION_SIGNING_KEY).secure(false);

View File

@ -28,10 +28,7 @@ impl Task {
Ok(tasks)
}
pub async fn insert(
todo: NewTask,
connection: &SqlitePool,
) -> Result<(), sqlx::Error> {
pub async fn insert(todo: NewTask, connection: &SqlitePool) -> Result<(), sqlx::Error> {
sqlx::query!(
r#"
INSERT INTO tasks (description)
@ -45,10 +42,7 @@ impl Task {
Ok(())
}
pub async fn toggle_with_id(
id: i32,
connection: &SqlitePool,
) -> Result<(), sqlx::Error> {
pub async fn toggle_with_id(id: i32, connection: &SqlitePool) -> Result<(), sqlx::Error> {
sqlx::query!(
r#"
UPDATE tasks
@ -63,10 +57,7 @@ impl Task {
Ok(())
}
pub async fn delete_with_id(
id: i32,
connection: &SqlitePool,
) -> Result<(), sqlx::Error> {
pub async fn delete_with_id(id: i32, connection: &SqlitePool) -> Result<(), sqlx::Error> {
sqlx::query!(
r#"
DELETE FROM tasks