Skip to content

Commit

Permalink
Merge pull request #772 from kant003/f4
Browse files Browse the repository at this point in the history
f4 Rust app
  • Loading branch information
kant003 authored Oct 21, 2024
2 parents 1a860c5 + 8d6c84e commit 3cfa35a
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
use std::sync::Mutex;

struct AppState {
tasks: Mutex<Vec<String>>,
}

async fn index(data: web::Data<AppState>) -> impl Responder {
let tasks = data.tasks.lock().unwrap();
let mut response_html = String::from("<h1>Lista de Tareas</h1><ul>");

for task in tasks.iter() {
response_html.push_str(&format!("<li>{}</li>", task));
}

if tasks.is_empty() {
response_html.push_str("<li>No hay tareas pendientes.</li>");
}

response_html.push_str("</ul>
<form action=\"/add\" method=\"POST\">
<input type=\"text\" name=\"task\" placeholder=\"Nueva tarea\" required>
<button type=\"submit\">Agregar Tarea</button>
</form>");

HttpResponse::Ok().body(response_html)
}

async fn add_task(data: web::Data<AppState>, form: web::Form<String>) -> impl Responder {
let mut tasks = data.tasks.lock().unwrap();
tasks.push(form.into_inner());
HttpResponse::SeeOther().header("Location", "/").finish()
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
let app_data = web::Data::new(AppState {
tasks: Mutex::new(Vec::new()),
});

HttpServer::new(move || {
App::new()
.app_data(app_data.clone())
.route("/", web::get().to(index))
.route("/add", web::post().to(add_task))
})
.bind("127.0.0.1:8080")?
.run()
.await
}

0 comments on commit 3cfa35a

Please sign in to comment.