-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
182 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use crate::crawler::CrawledInstance; | ||
|
||
pub fn sort_crawled_instances(l: &[CrawledInstance]) -> askama::Result<Vec<CrawledInstance>> { | ||
let mut new = l.to_owned(); | ||
new.sort_by(|a, b| a.status.as_isize().cmp(&b.status.as_isize())); | ||
Ok(new) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use actix_web::{cookie::Cookie, get, http::header::LOCATION, web, HttpRequest, Responder, Scope}; | ||
use askama::Template; | ||
use base64::prelude::*; | ||
|
||
use crate::{ | ||
config::AppConfig, | ||
errors::RedirectError, | ||
serde_types::{LoadedData, UserConfig}, | ||
utils::user_config::load_settings_cookie, | ||
}; | ||
|
||
pub fn scope(_config: &AppConfig) -> Scope { | ||
web::scope("/configure") | ||
.service(configure_page) | ||
.service(configure_save) | ||
} | ||
|
||
#[derive(Template)] | ||
#[template(path = "configure.html")] | ||
pub struct ConfigureTemplate<'a> { | ||
current_config: &'a str, | ||
} | ||
|
||
#[get("")] | ||
async fn configure_page( | ||
req: HttpRequest, | ||
loaded_data: web::Data<LoadedData>, | ||
) -> actix_web::Result<impl Responder> { | ||
let user_config = load_settings_cookie(&req, &loaded_data.default_settings); | ||
let json: String = serde_json::to_string(&user_config).map_err(RedirectError::Serialization)?; | ||
let data = BASE64_STANDARD.encode(json.as_bytes()); | ||
|
||
let template = ConfigureTemplate { | ||
current_config: &data, | ||
}; | ||
|
||
Ok(actix_web::HttpResponse::Ok() | ||
.content_type("text/html; charset=utf-8") | ||
.body(template.render().expect("failed to render error page"))) | ||
} | ||
|
||
#[get("/save")] | ||
async fn configure_save(req: HttpRequest) -> actix_web::Result<impl Responder> { | ||
let query_string = req.query_string(); | ||
let b64_decoded = BASE64_STANDARD | ||
.decode(query_string.as_bytes()) | ||
.map_err(RedirectError::Base64Decode)?; | ||
let user_config: UserConfig = | ||
serde_json::from_slice(&b64_decoded).map_err(RedirectError::Serialization)?; | ||
let json: String = serde_json::to_string(&user_config).map_err(RedirectError::Serialization)?; | ||
let data = BASE64_STANDARD.encode(json.as_bytes()); | ||
let cookie = Cookie::build("config", data).path("/").finish(); | ||
Ok(actix_web::HttpResponse::TemporaryRedirect() | ||
.cookie(cookie) | ||
.insert_header((LOCATION, "/configure?success")) | ||
.finish()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use std::collections::HashMap; | ||
|
||
use actix_web::{get, web, Responder, Scope}; | ||
use askama::Template; | ||
use chrono::{DateTime, Utc}; | ||
|
||
use crate::{ | ||
config::AppConfig, | ||
crawler::{CrawledService, Crawler}, | ||
errors::RedirectError, | ||
filters, | ||
search::SearchError, | ||
serde_types::{LoadedData, ServicesData}, | ||
}; | ||
|
||
use super::{config, redirect}; | ||
|
||
pub fn scope(app_config: &AppConfig) -> Scope { | ||
web::scope("") | ||
.service(index) | ||
.service(config::scope(app_config)) | ||
.service(redirect::scope(app_config)) | ||
} | ||
|
||
#[derive(Template)] | ||
#[template(path = "index.html")] | ||
pub struct IndexTemplate<'a> { | ||
pub crawled_services: &'a HashMap<String, CrawledService>, | ||
pub services: &'a ServicesData, | ||
pub time: &'a DateTime<Utc>, | ||
} | ||
|
||
#[get("/")] | ||
async fn index( | ||
crawler: web::Data<Crawler>, | ||
loaded_data: web::Data<LoadedData>, | ||
) -> actix_web::Result<impl Responder> { | ||
let data = crawler.read().await; | ||
let Some(crawled_services) = data.as_ref() else { | ||
return Err(RedirectError::from(SearchError::CrawlerNotFetchedYet))?; | ||
}; | ||
let template = IndexTemplate { | ||
services: &loaded_data.services, | ||
crawled_services: &crawled_services.services, | ||
time: &crawled_services.time, | ||
}; | ||
|
||
Ok(actix_web::HttpResponse::Ok() | ||
.content_type("text/html; charset=utf-8") | ||
.body(template.render().expect("failed to render error page"))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
mod config; | ||
mod index; | ||
mod redirect; | ||
|
||
use actix_web::{web, Scope}; | ||
use actix_web::Scope; | ||
|
||
use crate::config::AppConfig; | ||
|
||
pub fn main_scope(config: &AppConfig) -> Scope { | ||
web::scope("").service(redirect::scope(config)) | ||
index::scope(config) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod user_config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use actix_web::HttpRequest; | ||
use base64::prelude::*; | ||
|
||
use crate::serde_types::UserConfig; | ||
|
||
pub fn load_settings_cookie(req: &HttpRequest, default: &UserConfig) -> UserConfig { | ||
let cookie = match req.cookie("config") { | ||
Some(cookie) => cookie, | ||
None => { | ||
debug!("Cookie not found"); | ||
return default.clone(); | ||
} | ||
}; | ||
let data = match BASE64_STANDARD.decode(cookie.value().as_bytes()) { | ||
Ok(data) => data, | ||
Err(_) => { | ||
debug!("invalid cookie data"); | ||
return default.clone(); | ||
} | ||
}; | ||
match serde_json::from_slice(&data) { | ||
Ok(user_config) => user_config, | ||
Err(_) => { | ||
debug!("invalid cookie query string"); | ||
default.clone() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters