1
0
mirror of https://github.com/actix/examples synced 2025-06-07 09:48:21 +02:00

refactor: improve basics example

This commit is contained in:
Rob Ede 2025-05-12 06:40:45 +01:00
parent 92b0688df6
commit 03da1cb967
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
6 changed files with 29 additions and 29 deletions

26
Cargo.lock generated
View File

@ -1705,19 +1705,6 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "basics"
version = "0.0.0"
dependencies = [
"actix-files",
"actix-session",
"actix-web",
"actix-web-lab",
"async-stream",
"env_logger",
"log",
]
[[package]] [[package]]
name = "bb8" name = "bb8"
version = "0.8.6" version = "0.8.6"
@ -3142,6 +3129,19 @@ version = "2.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0"
[[package]]
name = "example-basics"
version = "0.0.0"
dependencies = [
"actix-files",
"actix-session",
"actix-web",
"actix-web-lab",
"async-stream",
"examples-common",
"tracing",
]
[[package]] [[package]]
name = "example-cert-watch" name = "example-cert-watch"
version = "0.0.0" version = "0.0.0"

View File

@ -98,6 +98,7 @@ actix-web-lab = "0.24"
actix-ws = "0.3" actix-ws = "0.3"
awc = "3.7" awc = "3.7"
async-stream = "0.3"
chrono = { version = "0.4.30", features = ["serde"] } chrono = { version = "0.4.30", features = ["serde"] }
color-eyre = "0.6" color-eyre = "0.6"
derive_more = "2" derive_more = "2"

View File

@ -1,14 +1,13 @@
[package] [package]
name = "basics" name = "example-basics"
edition.workspace = true edition.workspace = true
rust-version.workspace = true rust-version.workspace = true
[dependencies] [dependencies]
actix-files.workspace = true actix-files = { workspace = true }
actix-session = { workspace = true, features = ["cookie-session"] } actix-session = { workspace = true, features = ["cookie-session"] }
actix-web.workspace = true actix-web = { workspace = true }
actix-web-lab.workspace = true actix-web-lab = { workspace = true }
async-stream = { workspace = true }
async-stream = "0.3" examples-common = { workspace = true }
env_logger.workspace = true tracing = { workspace = true }
log.workspace = true

View File

@ -1,8 +1,8 @@
# basics # Basics
## Usage ## Usage
### server ### Server
```sh ```sh
cd basics/basics cd basics/basics
@ -10,7 +10,7 @@ cargo run
# Started http server: 127.0.0.1:8080 # Started http server: 127.0.0.1:8080
``` ```
### web client ### Browser
- [http://localhost:8080/async-body/bob](http://localhost:8080/async-body/bob) - [http://localhost:8080/async-body/bob](http://localhost:8080/async-body/bob)
- [http://localhost:8080/user/bob/](http://localhost:8080/user/bob) text/plain download - [http://localhost:8080/user/bob/](http://localhost:8080/user/bob) text/plain download

View File

@ -78,12 +78,12 @@ async fn with_param(req: HttpRequest, Path((name,)): Path<(String,)>) -> HttpRes
#[actix_web::main] #[actix_web::main]
async fn main() -> io::Result<()> { async fn main() -> io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); examples_common::init_standard_logger();
// random key means that restarting server will invalidate existing session cookies // random key means that restarting server will invalidate existing session cookies
let key = actix_web::cookie::Key::from(SESSION_SIGNING_KEY); let key = actix_web::cookie::Key::from(SESSION_SIGNING_KEY);
log::info!("starting HTTP server at http://localhost:8080"); tracing::info!("Starting HTTP server at http://localhost:8080");
HttpServer::new(move || { HttpServer::new(move || {
App::new() App::new()
@ -96,7 +96,7 @@ async fn main() -> io::Result<()> {
.build(), .build(),
) )
// enable logger - always register Actix Web Logger middleware last // enable logger - always register Actix Web Logger middleware last
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default().log_target("@"))
// register favicon // register favicon
.service(favicon) .service(favicon)
// register simple route, handle all methods // register simple route, handle all methods
@ -114,7 +114,7 @@ async fn main() -> io::Result<()> {
) )
.service(web::resource("/error").to(|| async { .service(web::resource("/error").to(|| async {
error::InternalError::new( error::InternalError::new(
io::Error::new(io::ErrorKind::Other, "test"), io::Error::other("test"),
StatusCode::INTERNAL_SERVER_ERROR, StatusCode::INTERNAL_SERVER_ERROR,
) )
})) }))

View File

@ -9,7 +9,7 @@ pub fn init_standard_logger() {
.with_default_directive(LevelFilter::INFO.into()) .with_default_directive(LevelFilter::INFO.into())
.from_env_lossy(), .from_env_lossy(),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer().without_time())
.init(); .init();
} }