rustream/squire/
custom.rs1use actix_web::{HttpRequest, HttpResponse};
2use actix_web::http::StatusCode;
3use minijinja::Template;
4
5use crate::constant;
6
7pub fn log_connection(request: &HttpRequest, session: &constant::Session) -> (String, String) {
20 let host = request.connection_info().host().to_string();
21 let mut tracker = session.tracker.lock().unwrap();
22 if tracker.get(&host).is_none() {
23 tracker.insert(host.clone(), "".to_string());
24 log::info!("Connection received from {}", host);
25 if let Some(user_agent) = request.headers().get("user-agent") {
26 log::info!("User agent: {}", user_agent.to_str().unwrap())
27 }
28 }
29 return (host.clone(), tracker.get(&host).map_or("".to_string(), |s| s.to_string()));
30}
31
32pub fn error(title: &str,
46 error: Template,
47 version: &String,
48 description: String,
49 status_code: StatusCode) -> HttpResponse {
50 HttpResponse::build(status_code)
51 .content_type("text/html; charset=utf-8")
52 .body(error.render(minijinja::context!(
53 version => version,
54 title => title,
55 description => description,
56 help => r"Lost your way?\n\nHit the HOME button to navigate back to home page.",
57 button_text => "HOME", button_link => "/home",
58 block_navigation => true
59 )).unwrap())
60}