Starting to work out the web server initial implementation and html returns

This commit is contained in:
Awstin 2023-12-15 17:57:46 -05:00
parent 21b80a682f
commit 917bb24fc4
4 changed files with 1338 additions and 2 deletions

1272
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -6,3 +6,5 @@ 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"

View file

@ -1,3 +1,43 @@
fn main() {
println!("Hello, world!");
use actix_htmx::{Htmx, HtmxMiddleware, TriggerType};
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
#[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)
}

22
static/index.html Normal file
View file

@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<script src="https://unpkg.com/htmx.org@1.9.9"
integrity="sha384-QFjmbokDn2DjBjq+fM+8LUIVrAgqcNW2s0PjAxHETgRn9l4fvX31ZxDxvwQnyMOX"
crossorigin="anonymous"></script>
</head>
<body>
<div hx-target="this" hx-swap="outerHTML">
<div><label>First Name</label>: Joe</div>
<div><label>Last Name</label>: Blow</div>
<div><label>Email</label>: joe@blow.com</div>
<button hx-get="/contact/1/edit" class="btn btn-primary">
Click To Edit
</button>
</div>
</body>
</html>