From 8bd5c13e09103e15740ca44cc0e5763a756459f9 Mon Sep 17 00:00:00 2001 From: Abdul Gofur Date: Wed, 14 Aug 2024 20:00:29 +0700 Subject: [PATCH] Task homepage and github --- assignment-homepage-abdul/Cargo.toml | 9 ++ assignment-homepage-abdul/run.sh | 3 + assignment-homepage-abdul/src/main.rs | 205 ++++++++++++++++++++++++++ iwakbetok.txt | 1 + 4 files changed, 218 insertions(+) create mode 100644 assignment-homepage-abdul/Cargo.toml create mode 100755 assignment-homepage-abdul/run.sh create mode 100644 assignment-homepage-abdul/src/main.rs create mode 100644 iwakbetok.txt diff --git a/assignment-homepage-abdul/Cargo.toml b/assignment-homepage-abdul/Cargo.toml new file mode 100644 index 0000000..9f649ec --- /dev/null +++ b/assignment-homepage-abdul/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "assignment1" +version = "0.1.0" +edition = "2021" + +[dependencies] +actix-web = "4.0" +actix-session = { version = "0.7", features = ["cookie-session"] } +serde = { version = "1.0", features = ["derive"] } diff --git a/assignment-homepage-abdul/run.sh b/assignment-homepage-abdul/run.sh new file mode 100755 index 0000000..a7c43ec --- /dev/null +++ b/assignment-homepage-abdul/run.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +cargo watch -x run \ No newline at end of file diff --git a/assignment-homepage-abdul/src/main.rs b/assignment-homepage-abdul/src/main.rs new file mode 100644 index 0000000..f2b9cb4 --- /dev/null +++ b/assignment-homepage-abdul/src/main.rs @@ -0,0 +1,205 @@ +use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder}; +use actix_session::{Session, SessionMiddleware, storage::CookieSessionStore}; +use actix_web::cookie::Key; +use serde::Deserialize; +use actix_web::http::header::ContentType; + +#[derive(Deserialize)] +struct LoginForm { + username: String, + password: String, +} + +// Hardcoded credentials +const VALID_USERNAME: &str = "admin"; +const VALID_PASSWORD: &str = "secret"; + +#[get("/")] +async fn home(session: Session) -> impl Responder { + if let Some(username) = session.get::("username").unwrap() { + HttpResponse::Ok().content_type(ContentType::html()).body(format!( + r#" + + + + + + Home + + + +
+

Welcome to the home page, {username}!

+ View Profile +
+ +
+
+ + + + "# + )) + } else { + HttpResponse::Ok().content_type(ContentType::html()).body( + r#" + + + + + + Home + + + +
+

Welcome to the home page!

+ Login +
+ + + + "# + ) + } +} + +#[get("/profile")] +async fn profile(session: Session) -> impl Responder { + if let Some(username) = session.get::("username").unwrap() { + HttpResponse::Ok().content_type(ContentType::html()).body(format!( + r#" + + + + + Profile + + + +
+

Profile

+

This is the profile page for {}.

+
+ +
+
+ + + "#, + username + )) + } else { + HttpResponse::Unauthorized().content_type(ContentType::html()).body( + r#" + + + + + Unauthorized + + + +
+

Unauthorized

+

Please login to view your profile.

+
+ + + "# + ) + } +} + +#[get("/login")] +async fn login_page() -> impl Responder { + HttpResponse::Ok() + .content_type(ContentType::html()) + .body( + r#" + + + + + + Login + + + +
+

Login

+
+
+ + +
+
+ + +
+ +
+
+ + + + "# + ) +} + +#[post("/login")] +async fn login(form: web::Form, session: Session) -> impl Responder { + if form.username == VALID_USERNAME && form.password == VALID_PASSWORD { + session.insert("username", &form.username).unwrap(); + HttpResponse::SeeOther().append_header(("Location", "/")) + .finish() + } else { + HttpResponse::Unauthorized().body( + r#" + + + + + + Login + + + +
+ + Try Again +
+ + + + "# + ) + } +} + +#[post("/logout")] +async fn logout(session: Session) -> impl Responder { + session.purge(); + HttpResponse::SeeOther().insert_header(("Location", "/")).finish() +} + +#[actix_web::main] +async fn main() -> std::io::Result<()> { + HttpServer::new(|| { + App::new() + .wrap(SessionMiddleware::new( + CookieSessionStore::default(), + Key::from(&[0; 64]), // In production, use a proper secret key + )) + .service(home) + .service(profile) + .service(login_page) + .service(login) + .service(logout) + }) + .bind("127.0.0.1:8080")? + .run() + .await +} \ No newline at end of file diff --git a/iwakbetok.txt b/iwakbetok.txt new file mode 100644 index 0000000..cf028de --- /dev/null +++ b/iwakbetok.txt @@ -0,0 +1 @@ +Abdul Gofur - Web Developer \ No newline at end of file