Switching to axum and askama

This commit is contained in:
OrthogonalStar 2023-12-16 17:58:58 -05:00
parent 917bb24fc4
commit 9a44a77120
4 changed files with 505 additions and 659 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
target/

1100
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -6,5 +6,10 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix-web = "4"
actix-htmx = "0.3"
askama = "0.12.1"
axum = "0.7.2"
tokio = { version = "1.35.0", features = ["full"] }
tower = "0.4.13"
tower-http = { version = "0.5.0", features = ["fs"] }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }

View file

@ -1,43 +1,15 @@
use actix_htmx::{Htmx, HtmxMiddleware, TriggerType};
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
use tracing::info;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.wrap(HtmxMiddleware)
.service(web::resource("/contact/1/edit").to(index))
})
.bind("0.0.0.0:8080")?
.run()
.await
}
async fn index(htmx: Htmx) -> impl Responder {
let mut body: &str = "did not work";
if htmx.is_htmx {
body = r#"<form hx-put="/contact/1" hx-target="this" hx-swap="outerHTML">
<div>
<label>First Name</label>
<input type="text" name="firstName" value="Joe">
</div>
<div class="form-group">
<label>Last Name</label>
<input type="text" name="lastName" value="Blow">
</div>
<div class="form-group">
<label>Email Address</label>
<input type="email" name="email" value="joe@blow.com">
</div>
<button class="btn">Submit</button>
<button class="btn" hx-get="/contact/1">Cancel</button>
</form>"#
}
htmx.trigger_event(
"my_event".to_string(),
Some(r#"{"level": "info", "message": "my event message!"}"#.to_string()),
Some(TriggerType::Standard)
);
HttpResponse::Ok().content_type("text/html").body(body)
#[tokio::main]
async fn main() {
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "with_axum_htmx_askama=debug".into()),
)
.with(tracing_subscriber::fmt::layer())
.init();
info!("hello, web server!");
}