Browse Source

Bugfix & Refactoring

Bugfix: Moved required body settings to global.css
Refactoring: Now use templates instead of direct html in sites.rs
integrate_codes_to_fields
anghenfil 3 years ago
parent
commit
26775dfe1c
  1. 233
      src/webserver/sites.rs
  2. 4
      src/webserver/templates.rs
  3. 6
      style/global.css

233
src/webserver/sites.rs

@ -3,7 +3,7 @@ use rocket::http::RawStr;
use rocket::request::Form;
use webserver;
use rocket::response::status;
use webserver::session_manager;
use webserver::{session_manager, templates};
use rocket::http::Status;
use webserver::formdata::FormData;
use std::fs::OpenOptions;
@ -25,28 +25,7 @@ pub fn poll_post(code: &RawStr, data: Form<FormData>, sessionid: Option<&RawStr>
let poll: String = if code_management::CODE_MANAGER.read().unwrap().contains_key(&codetemp) {
code_management::CODE_MANAGER.read().unwrap().get(&codetemp).unwrap().clone()
} else {
return Err(status::Custom(Status::NotFound, content::Html(String::from("<!DOCTYPE html>
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\">
<title>Surveyz</title>
<link href=\"https://fonts.googleapis.com/css?family=Lato\" rel=\"stylesheet\">
<link rel=\"stylesheet\" href=\"style/bootstrap.css\">
<link rel=\"stylesheet\" href=\"style/global.css\">
<link rel=\"stylesheet\" href=\"style/poll.css\">
</head>
<body class=\"centered\">
<div class=\"mainframe text-center align-middle\">
<br><br><br><h1>Error 404: Invalid Code</h1>
<p>The Code you entered is invalid.</p>
</div>
<div class=\"footer\">
<p>Powered by Surveyz - Project created by Keanu Dölle</p>
</div>
</body>
</html>
"))));
return Err(status::Custom(Status::NotFound, content::Html(templates::get_error("Error 404: Invalid Code", "The Code you entered is invalid!", vec!("style/bootstrap.css".to_string(), "style/global.css".to_string(), "style/poll.css".to_string()), Vec::new()))))
};
//Obtain current step:
@ -67,28 +46,7 @@ pub fn poll_post(code: &RawStr, data: Form<FormData>, sessionid: Option<&RawStr>
if temp.is_none() {
return Err(status::Custom(Status::BadRequest, content::Html(String::from("<!DOCTYPE html>
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\">
<title>Surveyz</title>
<link href=\"https://fonts.googleapis.com/css?family=Lato\" rel=\"stylesheet\">
<link rel=\"stylesheet\" href=\"style/bootstrap.css\">
<link rel=\"stylesheet\" href=\"style/global.css\">
<link rel=\"stylesheet\" href=\"style/poll.css\">
</head>
<body class=\"centered\">
<div class=\"mainframe text-center align-middle\">
<br><br><br><h1>Error 400.</h1>
<p>Submitted data were invalid.</p>
</div>
<div class=\"footer\">
<p>Powered by Surveyz - Project created by Keanu Dölle</p>
</div>
</body>
</html>
"))));
return Err(status::Custom(Status::BadRequest, content::Html(templates::get_error("Error 400", "Submitted data were invalid", vec!("style/bootstrap.css".to_string(), "style/global.css".to_string(), "style/poll.css".to_string()), Vec::new()))))
} else {
submitted = temp.unwrap();
}
@ -162,84 +120,28 @@ pub fn poll_post(code: &RawStr, data: Form<FormData>, sessionid: Option<&RawStr>
}
if session_manager::SESSIONS.read().unwrap().get(&sessionid).unwrap().complete {
return Ok(content::Html(format!("<!DOCTYPE html>
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\">
<title>Surveyz</title>
<link href=\"https://fonts.googleapis.com/css?family=Lato\" rel=\"stylesheet\">
<link rel=\"stylesheet\" href=\"style/bootstrap.css\">
<link rel=\"stylesheet\" href=\"style/global.css\">
<link rel=\"stylesheet\" href=\"style/poll.css\">
</head>
<body class=\"centered\">
{}
let template_content = format!("{}
<div class=\"mainframe text-center align-middle\">
<h1>Completed!</h1>
<p>Thank you for your participation!</p>
</div>
<div class=\"footer\">
<p>Powered by Surveyz - Project created by Keanu Dölle</p>
</div>
</body>
</html>
", logo)));
</div>", logo);
return Ok(content::Html(templates::get_main(&template_content, vec!("style/bootstrap.css".to_string(), "style/global.css".to_string(), "style/poll.css".to_string()), Vec::new())))
}
let cache = webserver::delivery::CACHE.read().unwrap();
let content = cache.get_file(format!("{}/{}", &poll, step));
if content.is_ok() {
return Ok(content::Html(format!("<!DOCTYPE html>
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\">
<title>Surveyz</title>
<link href=\"https://fonts.googleapis.com/css?family=Lato\" rel=\"stylesheet\">
<link rel=\"stylesheet\" href=\"style/bootstrap.css\">
<link rel=\"stylesheet\" href=\"style/global.css\">
<link rel=\"stylesheet\" href=\"style/poll.css\">
</head>
<body class=\"centered\">
{}
let template_content = format!("{}
<div class=\"mainframe text-center align-middle\">
<form action=\"/poll?code={}&sessionid={}\" method=\"post\">
{}
<button type=\"submit\" name=\"submit\" value=\"{}\" onclick=\"on_submit()\">Continue</button>
</form>
</div>
<div class=\"footer\">
<p>Powered by Surveyz - Project created by Keanu Dölle</p>
</div>
<script src=\"script/poll.js\"></script>
</body>
</html>
", logo, code, sessionid, content.unwrap().content, step)));
</div>", logo, code, sessionid, content.unwrap().content, step);
return Ok(content::Html(templates::get_main(&template_content, vec!("style/bootstrap.css".to_string(), "style/global.css".to_string(), "style/poll.css".to_string()),vec!("script/poll.js".to_string()))))
} else {
return Err(status::Custom(Status::NotFound, content::Html(String::from("<!DOCTYPE html>
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\">
<title>Surveyz</title>
<link href=\"https://fonts.googleapis.com/css?family=Lato\" rel=\"stylesheet\">
<link rel=\"stylesheet\" href=\"style/bootstrap.css\">
<link rel=\"stylesheet\" href=\"style/global.css\">
<link rel=\"stylesheet\" href=\"style/poll.css\">
</head>
<body class=\"centered\">
<div class=\"mainframe text-center align-middle\">
<br><br><br><h1>Error 404.</h1>
<p>Couldn't found the requested poll</p>
</div>
<div class=\"footer\">
<p>Powered by Surveyz - Project created by Keanu Dölle</p>
</div>
</body>
</html>
"))));
return Err(status::Custom(Status::NotFound, content::Html(templates::get_error("Error 404: Not found", "Couldn't find requested survey", vec!("style/bootstrap.css".to_string(), "style/global.css".to_string(), "style/poll.css".to_string()), Vec::new()))))
}
//}
}
@ -256,28 +158,7 @@ pub fn poll(code: &RawStr, sessionid: Option<&RawStr>) -> Result<content::Html<S
let poll : String = if code_management::CODE_MANAGER.read().unwrap().contains_key(&codetemp) {
code_management::CODE_MANAGER.read().unwrap().get(&codetemp).unwrap().clone()
}else{
return Err(status::Custom(Status::NotFound, content::Html(String::from("<!DOCTYPE html>
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\">
<title>Surveyz</title>
<link href=\"https://fonts.googleapis.com/css?family=Lato\" rel=\"stylesheet\">
<link rel=\"stylesheet\" href=\"style/bootstrap.css\">
<link rel=\"stylesheet\" href=\"style/global.css\">
<link rel=\"stylesheet\" href=\"style/poll.css\">
</head>
<body class=\"centered\">
<div class=\"mainframe text-center align-middle\">
<br><br><br><h1>Error 404: Invalid Code</h1>
<p>The Code you entered is invalid.</p>
</div>
<div class=\"footer\">
<p>Powered by Surveyz - Project created by Keanu Dölle</p>
</div>
</body>
</html>
"))));
return Err(status::Custom(Status::BadRequest, content::Html(templates::get_error("Error 400: Invalid Code", "The Code your entered is invalid.", vec!("style/bootstrap.css".to_string(), "style/global.css".to_string(), "style/poll.css".to_string()), Vec::new()))))
};
let mut logo = String::new();
@ -297,84 +178,28 @@ pub fn poll(code: &RawStr, sessionid: Option<&RawStr>) -> Result<content::Html<S
}
if session_manager::SESSIONS.read().unwrap().get(&sessionid).unwrap().complete {
return Ok(content::Html(String::from("<!DOCTYPE html>
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\">
<title>Surveyz</title>
<link href=\"https://fonts.googleapis.com/css?family=Lato\" rel=\"stylesheet\">
<link rel=\"stylesheet\" href=\"style/bootstrap.css\">
<link rel=\"stylesheet\" href=\"style/global.css\">
<link rel=\"stylesheet\" href=\"style/poll.css\">
</head>
<body class=\"centered\">
{}
let template_content = format!("{}
<div class=\"mainframe text-center align-middle\">
<h1>Completed!</h1>
<p>Thank you for your participation!</p>
</div>
<div class=\"footer\">
<p>Powered by Surveyz - Project created by Keanu Dölle</p>
</div>
</body>
</html>
")));
</div>", logo);
return Ok(content::Html(templates::get_main(&template_content, vec!("style/bootstrap.css".to_string(), "style/global.css".to_string(), "style/poll.css".to_string()), Vec::new())))
}
let cache = webserver::delivery::CACHE.read().unwrap();
let content = cache.get_file(format!("{}/{}", &poll, step));
if content.is_ok() {
return Ok(content::Html(format!("<!DOCTYPE html>
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\">
<title>Surveyz</title>
<link href=\"https://fonts.googleapis.com/css?family=Lato\" rel=\"stylesheet\">
<link rel=\"stylesheet\" href=\"style/bootstrap.css\">
<link rel=\"stylesheet\" href=\"style/global.css\">
<link rel=\"stylesheet\" href=\"style/poll.css\">
</head>
<body class=\"centered\">
{}
let template_content = format!("{}
<div class=\"mainframe text-center align-middle\">
<form action=\"/poll?code={}&sessionid={}\" method=\"post\">
{}
<button type=\"submit\" name=\"submit\" value=\"{}\" onclick=\"on_submit()\">Continue</button>
</form>
</div>
<div class=\"footer\">
<p>Powered by Surveyz - Project created by Keanu Dölle</p>
</div>
<script src=\"script/poll.js\"></script>
</body>
</html>
", logo, code, sessionid, content.unwrap().content, step)));
</div>", logo, code, sessionid, content.unwrap().content, step);
return Ok(content::Html(templates::get_main(&template_content, vec!("style/bootstrap.css".to_string(), "style/global.css".to_string(), "style/poll.css".to_string()),vec!("script/poll.js".to_string()))))
} else {
return Err(status::Custom(Status::NotFound, content::Html(String::from("<!DOCTYPE html>
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\">
<title>Surveyz</title>
<link href=\"https://fonts.googleapis.com/css?family=Lato\" rel=\"stylesheet\">
<link rel=\"stylesheet\" href=\"style/bootstrap.css\">
<link rel=\"stylesheet\" href=\"style/global.css\">
<link rel=\"stylesheet\" href=\"style/poll.css\">
</head>
<body class=\"centered\">
<div class=\"mainframe text-center align-middle\">
<br><br><br><h1>Error 404.</h1>
<p>Couldn't found the requested poll</p>
</div>
<div class=\"footer\">
<p>Powered by Surveyz - Project created by Keanu Dölle</p>
</div>
</body>
</html>
"))));
return Err(status::Custom(Status::NotFound, content::Html(templates::get_error("Error 404: Not found", "Couldn't find requested survey", vec!("style/bootstrap.css".to_string(), "style/global.css".to_string(), "style/poll.css".to_string()), Vec::new()))))
}
}
@ -394,18 +219,7 @@ pub fn index(error: Option<&RawStr>) -> content::Html<String> {
format!("<div class=\"alert alert-danger\" role=\"alert\">{}</div>", String::from(error_str))
}).unwrap_or_else(|| String::from(""));
content::Html(format!("<!DOCTYPE html>
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\">
<title>Surveyz</title>
<link href=\"https://fonts.googleapis.com/css?family=Lato\" rel=\"stylesheet\">
<link rel=\"stylesheet\" href=\"style/bootstrap.css\">
<link rel=\"stylesheet\" href=\"style/global.css\">
<link rel=\"stylesheet\" href=\"style/dashboard.css\">
</head>
<body class=\"centered\">
let template_content = format!("
<div class=\"mainframe text-center align-middle\">
<h1 class=join>Join Poll</h1>{}
<form action=\"poll\" method=\"get\">
@ -414,11 +228,6 @@ pub fn index(error: Option<&RawStr>) -> content::Html<String> {
<button type=\"submit\" class=\"btn go\">GO</button>
</div>
</form>
</div>
<div class=\"footer\">
<p>Powered by Surveyz - Project created by Keanu Dölle</p>
</div>
</body>
</html>
", error_str))
</div>", error_str);
content::Html(templates::get_main(&template_content, vec!("style/bootstrap.css".to_string(), "style/global.css".to_string(), "style/dashboard.css".to_string()), Vec::new()))
}

4
src/webserver/templates.rs

@ -1,9 +1,9 @@
pub fn get_main(content : &str, stylesheets : Vec<String>, scripts : Vec<String>) -> String{ //main template
let stylesheets_list : String = stylesheets.iter().map(|stylesheet|{
format!("<link rel=\"stylesheet\" type=\"text/css\" href=\"{}\"", stylesheet)
format!("<link rel=\"stylesheet\" type=\"text/css\" href=\"{}\">", stylesheet)
}).collect();
let scripts_list : String = scripts.iter().map(|script| {
format!("<script type=\"text/javascript\" src=\"{}\"", script)
format!("<script type=\"text/javascript\" src=\"{}\">", script)
}).collect();
format!("

6
style/global.css

@ -1,3 +1,9 @@
body{
height: 100%;
display: flex;
flex-direction: column;
}
@font-face{
font-family: 'Lora';
src: url('fonts/Lora/Lora-Bold.ttf') format('woff');

Loading…
Cancel
Save