mirror of
https://github.com/actix/examples
synced 2025-08-19 05:25:36 +02:00
standardize examples
This commit is contained in:
@@ -4,9 +4,11 @@ version = "1.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
env_logger.workspace = true
|
||||
actix-web-lab.workspace = true
|
||||
actix-web.workspace = true
|
||||
askama = "0.11.0"
|
||||
askama = "0.12"
|
||||
env_logger.workspace = true
|
||||
log.workspace = true
|
||||
|
||||
[build-dependencies]
|
||||
askama = "0.11.0"
|
||||
askama = "0.12"
|
||||
|
@@ -1,6 +1,7 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use actix_web::{middleware, web, App, HttpResponse, HttpServer, Result};
|
||||
use actix_web::{middleware, web, App, HttpServer, Responder, Result};
|
||||
use actix_web_lab::respond::Html;
|
||||
use askama::Template;
|
||||
|
||||
#[derive(Template)]
|
||||
@@ -14,26 +15,27 @@ struct UserTemplate<'a> {
|
||||
#[template(path = "index.html")]
|
||||
struct Index;
|
||||
|
||||
async fn index(query: web::Query<HashMap<String, String>>) -> Result<HttpResponse> {
|
||||
let s = if let Some(name) = query.get("name") {
|
||||
async fn index(query: web::Query<HashMap<String, String>>) -> Result<impl Responder> {
|
||||
let html = if let Some(name) = query.get("name") {
|
||||
UserTemplate {
|
||||
name,
|
||||
text: "Welcome!",
|
||||
}
|
||||
.render()
|
||||
.unwrap()
|
||||
.expect("template should be valid")
|
||||
} else {
|
||||
Index.render().unwrap()
|
||||
Index.render().expect("template should be valid")
|
||||
};
|
||||
Ok(HttpResponse::Ok().content_type("text/html").body(s))
|
||||
|
||||
Ok(Html(html))
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||
env_logger::init();
|
||||
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||||
|
||||
log::info!("starting HTTP server at http://localhost:8080");
|
||||
|
||||
// start http server
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.wrap(middleware::Logger::default())
|
||||
|
@@ -10,5 +10,5 @@ actix-utils.workspace = true
|
||||
|
||||
env_logger.workspace = true
|
||||
log.workspace = true
|
||||
minijinja = { version = "0.28", features = ["source"] }
|
||||
minijinja-autoreload = "0.28"
|
||||
minijinja = { version = "0.30", features = ["source"] }
|
||||
minijinja-autoreload = "0.30"
|
||||
|
@@ -9,4 +9,4 @@ actix-web-lab.workspace = true
|
||||
|
||||
env_logger.workspace = true
|
||||
log.workspace = true
|
||||
sailfish = "0.5"
|
||||
sailfish = "0.6"
|
||||
|
@@ -8,4 +8,5 @@ actix-web.workspace = true
|
||||
actix-web-lab.workspace = true
|
||||
|
||||
env_logger.workspace = true
|
||||
tera = "1.8.0"
|
||||
log.workspace = true
|
||||
tera = "1.8"
|
||||
|
@@ -33,16 +33,16 @@ async fn index(
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||
env_logger::init();
|
||||
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||||
|
||||
log::info!("starting HTTP server at http://localhost:8080");
|
||||
|
||||
println!("Listening on: 127.0.0.1:8080, open browser and visit have a try!");
|
||||
HttpServer::new(|| {
|
||||
let tera = Tera::new(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")).unwrap();
|
||||
|
||||
App::new()
|
||||
.app_data(web::Data::new(tera))
|
||||
.wrap(middleware::Logger::default()) // enable logger
|
||||
.wrap(middleware::Logger::default())
|
||||
.service(web::resource("/").route(web::get().to(index)))
|
||||
.service(web::scope("").wrap(error_handlers()))
|
||||
})
|
||||
|
@@ -4,7 +4,8 @@ version = "1.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
env_logger.workspace = true
|
||||
tinytemplate = "1.1"
|
||||
actix-web.workspace = true
|
||||
env_logger.workspace = true
|
||||
log.workspace = true
|
||||
serde_json.workspace = true
|
||||
tinytemplate = "1.1"
|
||||
|
@@ -34,8 +34,9 @@ async fn index(
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||
env_logger::init();
|
||||
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||||
|
||||
log::info!("starting HTTP server at http://localhost:8080");
|
||||
|
||||
HttpServer::new(|| {
|
||||
let mut tt = TinyTemplate::new();
|
||||
@@ -45,7 +46,7 @@ async fn main() -> std::io::Result<()> {
|
||||
|
||||
App::new()
|
||||
.app_data(web::Data::new(tt))
|
||||
.wrap(middleware::Logger::default()) // enable logger
|
||||
.wrap(middleware::Logger::default())
|
||||
.service(web::resource("/").route(web::get().to(index)))
|
||||
.service(web::scope("").wrap(error_handlers()))
|
||||
})
|
||||
|
@@ -6,9 +6,10 @@ edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
actix-web.workspace = true
|
||||
env_logger.workspace = true
|
||||
yarte = { version = "0.15", features = ["bytes-buf", "html-min"] }
|
||||
derive_more.workspace = true
|
||||
env_logger.workspace = true
|
||||
log.workspace = true
|
||||
yarte = { version = "0.15", features = ["bytes-buf", "html-min"] }
|
||||
|
||||
[build-dependencies.yarte_helpers]
|
||||
version = "0.15.6"
|
||||
|
@@ -23,10 +23,10 @@ async fn index(query: web::Query<HashMap<String, String>>) -> Result<HttpRespons
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||
env_logger::init();
|
||||
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||||
|
||||
log::info!("starting HTTP server at http://localhost:8080");
|
||||
|
||||
// start http server
|
||||
HttpServer::new(move || App::new().wrap(Logger::default()).service(index))
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
|
Reference in New Issue
Block a user