new laptop setup

This commit is contained in:
2022-12-24 16:34:17 +01:00
parent 5b95621d04
commit 31b47e892d
55 changed files with 789 additions and 5 deletions
+23
View File
@@ -0,0 +1,23 @@
use std::fs;
pub fn read_file(file_path: &str) -> String {
let data: String = fs::read_to_string(file_path)
.expect(format!("Unable to read file {}", file_path).as_str());
return data;
}
pub fn add_component(component_tag: String, html_data: String) -> String {
let css_tag: String = component_tag.to_uppercase() + &String::from("_CSS");
let html_tag: String = component_tag.to_uppercase() + &String::from("_HTML");
let css_path = String::from("./templates/components/")
+ &component_tag.to_lowercase()
+ &String::from(".css");
let css_loaded = read_file(&css_path);
let html_path = String::from("./templates/components/")
+ &component_tag.to_lowercase()
+ &String::from(".html");
let html_loaded = read_file(&html_path);
let html_data = html_data.replace(html_tag.as_str(), &html_loaded);
let html_data = html_data.replace(css_tag.as_str(),&css_loaded);
return html_data
}
+17
View File
@@ -0,0 +1,17 @@
use super::content_loader::read_file;
use actix_web::HttpResponse;
pub async fn login() -> HttpResponse {
let mut html_data = read_file(&String::from("./templates/login.html"));
let javascript_data = read_file(&String::from("./javascript/login.js"));
let css_data = read_file(&String::from("./css/main.css"));
let base_css_data = read_file(&String::from("./css/base.css"));
html_data = html_data.replace("{{JAVASCRIPT}}", &javascript_data);
html_data = html_data.replace("{{CSS}}", &css_data);
html_data = html_data.replace("{{BASE_CSS}}", &base_css_data);
HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(html_data)
}
+15
View File
@@ -0,0 +1,15 @@
use actix_web::HttpResponse;
pub async fn logout() -> HttpResponse {
HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(
"<html>\
<script>\
localStorage.removeItem('user-token'); \
window.location.replace(document.location.origin);\
</script>\
</html>
",
)
}
+35
View File
@@ -0,0 +1,35 @@
use actix_web::web;
mod content_loader;
mod login;
mod logout;
mod reader;
use super::path::Path;
/// This function adds the app views to the web server serving HTML.
///
/// # Arguments
/// * (&mut web::ServiceConfig): reference to the app for configuration
///
/// # Returns
/// None
pub fn app_factory(app: &mut web::ServiceConfig) {
// define the path struct
let base_path: Path = Path {
prefix: String::from("/"),
backend: false,
};
// define the routes for the app
app.route(
&base_path.define(String::from("")),
web::get().to(reader::reader),
);
app.route(
&base_path.define(String::from("login")),
web::get().to(login::login),
);
app.route(
&base_path.define(String::from("logout")),
web::get().to(logout::logout),
);
}
+18
View File
@@ -0,0 +1,18 @@
use super::content_loader::{add_component, read_file};
use actix_web::HttpResponse;
pub async fn reader() -> HttpResponse {
let mut html_data = read_file("./templates/reader.html");
let javascript = read_file("./javascript/main.js");
let css = read_file("./css/main.css");
let base_css = read_file("./css/base.css");
html_data = html_data.replace("JAVASCRIPT", &javascript);
html_data = html_data.replace("CSS", &css);
html_data = html_data.replace("BASE_CSS", &base_css);
// html_data = add_component(String::from("header"), html_data);
HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(html_data)
}
+44
View File
@@ -0,0 +1,44 @@
use crate::database::establish_connection;
use crate::diesel;
use crate::json_serialization::login::Login;
use crate::models::user::user::User;
use crate::schema::users;
use crate::{auth::jwt::JwtToken, schema::users::username};
use actix_web::{web, HttpResponse};
use diesel::prelude::*;
pub async fn login(credentials: web::Json<Login>) -> HttpResponse {
let username_cred: String = credentials.username.clone();
let password: String = credentials.password.clone();
let mut connection = establish_connection();
let users = users::table
.filter(username.eq(username_cred.as_str()))
.load::<User>(&mut connection)
.unwrap();
if users.is_empty() {
return HttpResponse::NotFound().await.unwrap();
} else if users.len() > 1 {
log::error!(
"multiple user have the usernam: {}",
credentials.username.clone()
);
return HttpResponse::Conflict().await.unwrap();
}
let user: &User = &users[0];
match user.clone().verify(password) {
true => {
log::info!("verified password successfully");
let token: String = JwtToken::encode(user.clone().id);
HttpResponse::Ok()
.insert_header(("token", token))
.await
.unwrap()
}
false => HttpResponse::Unauthorized().await.unwrap(),
}
}
+3
View File
@@ -0,0 +1,3 @@
pub async fn logout() -> String {
format!("logout view")
}
+23
View File
@@ -0,0 +1,23 @@
use actix_web::web;
use actix_web::web::ServiceConfig;
use super::path::Path;
mod login;
mod logout;
pub fn auth_factory(app: &mut ServiceConfig) {
let base_path: Path = Path {
prefix: String::from("/auth"),
backend: true,
};
app.route(
&base_path.define(String::from("/login")),
web::post().to(login::login),
);
app.route(
&base_path.define(String::from("/logout")),
web::post().to(logout::logout),
);
}
Regular → Executable
+7 -3
View File
@@ -1,10 +1,14 @@
use actix_web::web;
use crate::reader;
mod app;
mod auth;
pub(crate) mod path;
mod users;
pub fn views_factory(app: &mut web::ServiceConfig) {
// auth::auth_factory(app);
// to_do::item_factory(app);
// app::app_factory(app);
auth::auth_factory(app);
app::app_factory(app);
users::user_factory(app);
reader::feed_factory(app);
}
Regular → Executable
+1
View File
@@ -14,3 +14,4 @@ impl Path {
}
}
}
+25
View File
@@ -0,0 +1,25 @@
use crate::database::establish_connection;
use crate::diesel;
use crate::json_serialization::new_user::NewUserSchema;
use crate::models::user::new_user::NewUser;
use crate::schema::users;
use actix_web::{web, HttpResponse};
use diesel::prelude::*;
pub async fn create(new_user: web::Json<NewUserSchema>) -> HttpResponse {
let mut connection = establish_connection();
let name: String = new_user.name.clone();
let email: String = new_user.email.clone();
let new_password: String = new_user.password.clone();
let new_user = NewUser::new(name, email, new_password);
let insert_result = diesel::insert_into(users::table)
.values(&new_user)
.execute(&mut connection);
match insert_result {
Ok(_) => HttpResponse::Created().await.unwrap(),
Err(_) => HttpResponse::Conflict().await.unwrap(),
}
}
+15
View File
@@ -0,0 +1,15 @@
use super::path::Path;
use actix_web::web;
mod create;
pub fn user_factory(app: &mut web::ServiceConfig) {
let base_path: Path = Path {
prefix: String::from("/user"),
backend: true,
};
app.route(
&base_path.define(String::from("/create")),
web::post().to(create::create),
);
}