Added main page and static files
This commit is contained in:
19
src/pages/files.rs
Normal file
19
src/pages/files.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
use actix_web::web::Path;
|
||||
use actix_web::{HttpResponse, Responder, get};
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[get("/static/{file_type}/{file_name}")]
|
||||
async fn file(file: Path<(String, String)>) -> impl Responder {
|
||||
//TODO use assets dir
|
||||
let mut path = PathBuf::from("assets");
|
||||
path.push(&file.0.replace("/", ""));
|
||||
path.push(&file.1.replace("/", ""));
|
||||
let mut file = File::open(path).unwrap();
|
||||
let mut bytes = Vec::new();
|
||||
if file.read_to_end(&mut bytes).is_err() {
|
||||
return HttpResponse::NotFound().body("File not found");
|
||||
};
|
||||
HttpResponse::Ok().body(bytes)
|
||||
}
|
||||
45
src/pages/mod.rs
Normal file
45
src/pages/mod.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
pub(crate) mod files;
|
||||
|
||||
use crate::html::elements::{a, div, h1, h2, img, link, p};
|
||||
use crate::html::{Page, Render};
|
||||
use actix_web::http::header::ContentType;
|
||||
use actix_web::mime::TEXT_HTML;
|
||||
use actix_web::{HttpResponse, Responder, get};
|
||||
|
||||
#[get("/")]
|
||||
async fn index() -> impl Responder {
|
||||
let mut page = Page::new("AINDUSTRIES".to_string());
|
||||
// header
|
||||
let mut header = div::new("header".to_string(), vec!["header".to_string()]);
|
||||
let name = p::new("name".to_string(), vec!["name".to_string()], "AINDUSTRIES".to_string());
|
||||
let mut buttons = div::new("buttons".to_string(), vec!["nav-buttons".to_string()]);
|
||||
let home = a::new("home".to_string(), vec!["nav-button".to_string()], "/".to_string(), "Home".to_string());
|
||||
buttons.append_element(home);
|
||||
header.append_element(name);
|
||||
header.append_element(buttons);
|
||||
// background
|
||||
let image = img::new("background".to_string(), vec!["background".to_string()], "static/img/background.png".to_string());
|
||||
// introduction
|
||||
let mut intro = div::new("intro".to_string(), vec!["intro".to_string()]);
|
||||
let hi = h1::new("Hello and welcome!".to_string());
|
||||
let detail = h2::new("This here is a small website to show the passion I have for computers.<br>\
|
||||
I have always had a good time creating and discovering new things.<br>\
|
||||
Your may discover some of my projects here on this showcase.".to_string());
|
||||
let note = p::new("note".to_string(), vec!["note".to_string()], "Website in construction.".to_string());
|
||||
intro.append_element(hi);
|
||||
intro.append_element(detail);
|
||||
intro.append_element(note);
|
||||
// css
|
||||
let base = link::new("stylesheet".to_string(), "static/css/base.css".to_string());
|
||||
let index = link::new("stylesheet".to_string(), "static/css/index.css".to_string());
|
||||
// add elements to the page in order
|
||||
page.append_element_to_head(base);
|
||||
page.append_element_to_head(index);
|
||||
page.append_element_to_body(image);
|
||||
page.append_element_to_body(header);
|
||||
page.append_element_to_body(intro);
|
||||
let response = HttpResponse::Ok()
|
||||
.insert_header(ContentType(TEXT_HTML))
|
||||
.body(page.render());
|
||||
response
|
||||
}
|
||||
Reference in New Issue
Block a user