use actix_web::{HttpRequest, HttpResponse};
use actix_web::http::StatusCode;
use minijinja::Template;
use crate::constant;
pub fn log_connection(request: &HttpRequest, session: &constant::Session) -> (String, String) {
let host = request.connection_info().host().to_string();
let mut tracker = session.tracker.lock().unwrap();
if tracker.get(&host).is_none() {
tracker.insert(host.clone(), "".to_string());
log::info!("Connection received from {}", host);
if let Some(user_agent) = request.headers().get("user-agent") {
log::info!("User agent: {}", user_agent.to_str().unwrap())
}
}
return (host.clone(), tracker.get(&host).map_or("".to_string(), |s| s.to_string()));
}
pub fn error(title: &str,
error: Template,
version: &String,
description: String,
status_code: StatusCode) -> HttpResponse {
HttpResponse::build(status_code)
.content_type("text/html; charset=utf-8")
.body(error.render(minijinja::context!(
version => version,
title => title,
description => description,
help => r"Lost your way?\n\nHit the HOME button to navigate back to home page.",
button_text => "HOME", button_link => "/home",
block_navigation => true
)).unwrap())
}