From 288b6e9ec42747c344fdb4f2eea70a72e6995462 Mon Sep 17 00:00:00 2001 From: Dominic Grimm Date: Tue, 28 Feb 2023 12:16:33 +0100 Subject: [PATCH] Update --- .env.example | 18 +++++------ bvplan/Dockerfile | 7 +++-- bvplan/js/bvplan.js | 43 ++++++++++++++++++++++++++ bvplan/scss/bvplan.scss | 4 +++ bvplan/src/bin/web.rs | 25 ++++++++++++--- bvplan/src/bin/worker.rs | 11 ++----- bvplan/src/config.rs | 28 ++++++++--------- bvplan/src/templates.rs | 6 ++++ bvplan/src/worker/get_substitutions.rs | 11 +++---- bvplan/static/.keep | 0 bvplan/templates/bvplan.html | 43 +------------------------- docker-compose.yml | 24 +++++++------- 12 files changed, 119 insertions(+), 101 deletions(-) create mode 100644 bvplan/js/bvplan.js delete mode 100644 bvplan/static/.keep diff --git a/.env.example b/.env.example index 32e16f6..08646ab 100644 --- a/.env.example +++ b/.env.example @@ -4,12 +4,12 @@ POSTGRES_PASSWORD="bvplan" RABBITMQ_USER="bvplan" RABBITMQ_PASSWORD="bvplan" -BACKEND_UNTIS_API_URL="https://mese.webuntis.com/WebUntis/" -BACKEND_UNTIS_RPC_URL="https://mese.webuntis.com/WebUntis/jsonrpc.do" -BACKEND_UNTIS_CLIENT_NAME="bvplan" -BACKEND_UNTIS_SCHOOL= -BACKEND_UNTIS_USERNAME= -BACKEND_UNTIS_PASSWORD= -BACKEND_UNTIS_VPLAN_URL= -BACKEND_UNTIS_VPLAN_USERNAME= -BACKEND_UNTIS_VPLAN_PASSWORD= +BVPLAN_UNTIS_API_URL="https://mese.webuntis.com/WebUntis/" +BVPLAN_UNTIS_RPC_URL="https://mese.webuntis.com/WebUntis/jsonrpc.do" +BVPLAN_UNTIS_CLIENT_NAME="bvplan" +BVPLAN_UNTIS_SCHOOL= +BVPLAN_UNTIS_USERNAME= +BVPLAN_UNTIS_PASSWORD= +BVPLAN_UNTIS_VPLAN_URL= +BVPLAN_UNTIS_VPLAN_USERNAME= +BVPLAN_UNTIS_VPLAN_PASSWORD= diff --git a/bvplan/Dockerfile b/bvplan/Dockerfile index d88cb15..f8dafd1 100644 --- a/bvplan/Dockerfile +++ b/bvplan/Dockerfile @@ -8,6 +8,7 @@ RUN find . -name "*.scss" -type f | xargs -I % sh -c 'sassc % > ../dist/$(basena FROM tdewolff/minify:latest as static WORKDIR /usr/src/static COPY --from=css /usr/src/scss/dist ./css +COPY ./js ./js RUN minify . -r -o . FROM docker.io/lukemathwalker/cargo-chef:latest-rust-1.65.0 as chef @@ -24,13 +25,13 @@ RUN cargo chef prepare --recipe-path recipe.json FROM chef as builder WORKDIR /usr/src/backend COPY --from=planner /usr/src/backend/recipe.json . -RUN cargo chef cook --recipe-path recipe.json +RUN cargo chef cook --release --recipe-path recipe.json RUN rm -rf ./src COPY ./build.rs . COPY --from=static /usr/src/static ./static COPY ./templates ./templates COPY ./src ./src -RUN cargo build +RUN cargo build --release FROM docker.io/debian:bullseye-slim as runner LABEL maintainer="Dominic Grimm " \ @@ -51,6 +52,6 @@ WORKDIR /usr/src/backend COPY ./run.sh . RUN chmod +x ./run.sh COPY ./migrations ./migrations -COPY --from=builder /usr/src/backend/target/debug/web /usr/src/backend/target/debug/worker ./bin/ +COPY --from=builder /usr/src/backend/target/release/web /usr/src/backend/target/release/worker ./bin/ EXPOSE 80 ENTRYPOINT [ "./run.sh" ] diff --git a/bvplan/js/bvplan.js b/bvplan/js/bvplan.js new file mode 100644 index 0000000..aaa18ad --- /dev/null +++ b/bvplan/js/bvplan.js @@ -0,0 +1,43 @@ +window.onerror = function () { + console.log("js error detected"); + setTimeout(function () { + console.log("reloading page because of error"); + window.location.reload(); + }, 30_000); +}; + +const elementCount = JSON.parse($("#data-element-count").text()); +const waitDelay = 10_000; +const scrollDuration = elementCount * 200; + +function reload() { + window.location.reload(); +} + +function scrollUp() { + $("html, body").animate( + { + scrollTop: 0, + }, + scrollDuration, + "linear" + ); + setTimeout(reload, scrollDuration); +} + +function scrollDown() { + $("html, body").animate( + { + scrollTop: $(document).height() - $(window).height(), + }, + scrollDuration, + "linear" + ); + setTimeout(scrollUp, scrollDuration + waitDelay); +} + +window.scrollTo(0, 0); +setTimeout( + $(document).height() > $(window).height() ? scrollDown : reload, + waitDelay +); diff --git a/bvplan/scss/bvplan.scss b/bvplan/scss/bvplan.scss index 0662226..e9b9b1f 100644 --- a/bvplan/scss/bvplan.scss +++ b/bvplan/scss/bvplan.scss @@ -98,5 +98,9 @@ body { .element { margin: 0.5rem 0; + + &:last-of-type { + margin-bottom: 0; + } } } diff --git a/bvplan/src/bin/web.rs b/bvplan/src/bin/web.rs index dac6453..a3b7dba 100644 --- a/bvplan/src/bin/web.rs +++ b/bvplan/src/bin/web.rs @@ -11,7 +11,6 @@ use r2d2_redis::redis; use std::ops::DerefMut; use bvplan::*; -use templates::TemplateToResponseWithStatusCode; pub mod static_dir { include!(concat!(env!("OUT_DIR"), "/generated.rs")); @@ -22,14 +21,20 @@ async fn not_found() -> HttpResponse { status_code: http::StatusCode::NOT_FOUND, message: None, } - .to_response_with_status_code(http::StatusCode::NOT_FOUND) + .to_response() } #[get("/")] async fn index(redis_pool: web::Data) -> HttpResponse { let redis_conn = &mut match redis_pool.get() { Ok(x) => x, - Err(_) => return HttpResponse::InternalServerError().finish(), + Err(_) => { + return templates::StatusCode { + status_code: http::StatusCode::INTERNAL_SERVER_ERROR, + message: None, + } + .to_response() + } }; if let Some(html) = match redis::cmd("GET") @@ -37,13 +42,23 @@ async fn index(redis_pool: web::Data) -> HttpResponse { .query::>(redis_conn.deref_mut()) { Ok(x) => x, - Err(_) => return HttpResponse::InternalServerError().finish(), + Err(_) => { + return templates::StatusCode { + status_code: http::StatusCode::INTERNAL_SERVER_ERROR, + message: None, + } + .to_response() + } } { HttpResponse::Accepted() .content_type("text/html; charset=utf-8") .body(html) } else { - HttpResponse::InternalServerError().finish() + templates::StatusCode { + status_code: http::StatusCode::INTERNAL_SERVER_ERROR, + message: None, + } + .to_response() } } diff --git a/bvplan/src/bin/worker.rs b/bvplan/src/bin/worker.rs index 5b067e0..8a568af 100644 --- a/bvplan/src/bin/worker.rs +++ b/bvplan/src/bin/worker.rs @@ -17,19 +17,12 @@ async fn main() { worker::init_blocking(); thread::spawn(|| { tokio::runtime::Runtime::new().unwrap().block_on(async { - worker::beat() - .await - .unwrap() - .start() - .await - .unwrap(); + worker::beat().await.unwrap().start().await.unwrap(); }); }); let app = worker::APP.lock().unwrap(); let app = app.as_ref().unwrap(); app.display_pretty().await; - app.consume_from(&[worker::QUEUE_NAME]) - .await - .unwrap(); + app.consume_from(&[worker::QUEUE_NAME]).await.unwrap(); } diff --git a/bvplan/src/config.rs b/bvplan/src/config.rs index d04fa39..a561fba 100644 --- a/bvplan/src/config.rs +++ b/bvplan/src/config.rs @@ -5,40 +5,40 @@ use url::Url; #[derive(Envconfig, Debug)] pub struct Config { - #[envconfig(from = "BACKEND_DB_URL")] + #[envconfig(from = "BVPLAN_DB_URL")] pub db_url: String, - #[envconfig(from = "BACKEND_AMQP_URL")] + #[envconfig(from = "BVPLAN_AMQP_URL")] pub amqp_url: String, - #[envconfig(from = "BACKEND_REDIS_URL")] + #[envconfig(from = "BVPLAN_REDIS_URL")] pub redis_url: String, - #[envconfig(from = "BACKEND_UNTIS_API_URL")] + #[envconfig(from = "BVPLAN_UNTIS_API_URL")] pub untis_api_url: String, - #[envconfig(from = "BACKEND_UNTIS_RPC_URL")] + #[envconfig(from = "BVPLAN_UNTIS_RPC_URL")] pub untis_rpc_url: String, - #[envconfig(from = "BACKEND_UNTIS_CLIENT_NAME")] + #[envconfig(from = "BVPLAN_UNTIS_CLIENT_NAME")] pub untis_client_name: String, - #[envconfig(from = "BACKEND_UNTIS_SCHOOL")] + #[envconfig(from = "BVPLAN_UNTIS_SCHOOL")] pub untis_school: String, - #[envconfig(from = "BACKEND_UNTIS_USERNAME")] + #[envconfig(from = "BVPLAN_UNTIS_USERNAME")] pub untis_username: String, - #[envconfig(from = "BACKEND_UNTIS_PASSWORD")] + #[envconfig(from = "BVPLAN_UNTIS_PASSWORD")] pub untis_password: String, - #[envconfig(from = "BACKEND_UNTIS_VPLAN_URL")] + #[envconfig(from = "BVPLAN_UNTIS_VPLAN_URL")] pub untis_vplan_url: String, - #[envconfig(from = "BACKEND_UNTIS_VPLAN_USERNAME")] + #[envconfig(from = "BVPLAN_UNTIS_VPLAN_USERNAME")] pub untis_vplan_username: String, - #[envconfig(from = "BACKEND_UNTIS_VPLAN_PASSWORD")] + #[envconfig(from = "BVPLAN_UNTIS_VPLAN_PASSWORD")] pub untis_vplan_password: String, } @@ -48,15 +48,13 @@ lazy_static! { pub fn untis_from_env() -> Result { Ok(untis::Client { - // api_url: Url::parse(&CONFIG.untis_api_url)?, webuntis_url: Url::parse(&CONFIG.untis_api_url)?, rpc_url: untis::Client::gen_rpc_url( &Url::parse(&CONFIG.untis_rpc_url)?, &CONFIG.untis_school, )?, client_name: CONFIG.untis_client_name.to_owned(), - user_agent: "Mozilla/5.0 (Windows NT 10.0; rv:108.0) Gecko/20100101 Firefox/108.0" - .to_string(), + user_agent: "".to_string(), username: CONFIG.untis_username.to_owned(), password: CONFIG.untis_password.to_owned(), session: None, diff --git a/bvplan/src/templates.rs b/bvplan/src/templates.rs index da47eb2..59be487 100644 --- a/bvplan/src/templates.rs +++ b/bvplan/src/templates.rs @@ -49,3 +49,9 @@ pub struct StatusCode { pub status_code: http::StatusCode, pub message: Option, } + +impl StatusCode { + pub fn to_response(&self) -> HttpResponse { + self.to_response_with_status_code(self.status_code) + } +} diff --git a/bvplan/src/worker/get_substitutions.rs b/bvplan/src/worker/get_substitutions.rs index 18743a8..061273c 100644 --- a/bvplan/src/worker/get_substitutions.rs +++ b/bvplan/src/worker/get_substitutions.rs @@ -240,7 +240,7 @@ async fn fetch_substitutions( type StartEndTime = (NaiveTime, NaiveTime); -fn get_period(times: &Vec, start: bool, time: NaiveTime) -> Option { +fn get_period(times: &[StartEndTime], start: bool, time: NaiveTime) -> Option { times .iter() .position(|x| (if start { x.0 } else { x.1 }) == time) @@ -417,14 +417,13 @@ fn cache_substitutions( .collect::>>() .unwrap() .into_iter() - .filter_map(|s| s) + .flatten() .collect::>(); substitutions.sort_by_key(|x| { ( - x.classes.get(0).and_then(|x| { - x.split_once(' ') - .and_then(|y| y.0.parse::().map_or(None, |s| Some(s))) - }), + x.classes + .get(0) + .and_then(|x| x.split_once(' ').and_then(|y| y.0.parse::().ok())), x.period, ) }); diff --git a/bvplan/static/.keep b/bvplan/static/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/bvplan/templates/bvplan.html b/bvplan/templates/bvplan.html index beb2529..f1b7982 100644 --- a/bvplan/templates/bvplan.html +++ b/bvplan/templates/bvplan.html @@ -144,47 +144,6 @@

- + diff --git a/docker-compose.yml b/docker-compose.yml index f50d09b..d8757b3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,18 +12,18 @@ x-bvplan: - rabbitmq - redis environment: - BACKEND_DB_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_USER} - BACKEND_AMQP_URL: amqp://${RABBITMQ_USER}:${RABBITMQ_PASSWORD}@rabbitmq:5672 - BACKEND_REDIS_URL: redis://redis:6379 - BACKEND_UNTIS_API_URL: ${BACKEND_UNTIS_API_URL} - BACKEND_UNTIS_RPC_URL: ${BACKEND_UNTIS_RPC_URL} - BACKEND_UNTIS_CLIENT_NAME: ${BACKEND_UNTIS_CLIENT_NAME} - BACKEND_UNTIS_SCHOOL: ${BACKEND_UNTIS_SCHOOL} - BACKEND_UNTIS_USERNAME: ${BACKEND_UNTIS_USERNAME} - BACKEND_UNTIS_PASSWORD: ${BACKEND_UNTIS_PASSWORD} - BACKEND_UNTIS_VPLAN_URL: ${BACKEND_UNTIS_VPLAN_URL} - BACKEND_UNTIS_VPLAN_USERNAME: ${BACKEND_UNTIS_VPLAN_USERNAME} - BACKEND_UNTIS_VPLAN_PASSWORD: ${BACKEND_UNTIS_VPLAN_PASSWORD} + BVPLAN_DB_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_USER} + BVPLAN_AMQP_URL: amqp://${RABBITMQ_USER}:${RABBITMQ_PASSWORD}@rabbitmq:5672 + BVPLAN_REDIS_URL: redis://redis:6379 + BVPLAN_UNTIS_API_URL: ${BVPLAN_UNTIS_API_URL} + BVPLAN_UNTIS_RPC_URL: ${BVPLAN_UNTIS_RPC_URL} + BVPLAN_UNTIS_CLIENT_NAME: ${BVPLAN_UNTIS_CLIENT_NAME} + BVPLAN_UNTIS_SCHOOL: ${BVPLAN_UNTIS_SCHOOL} + BVPLAN_UNTIS_USERNAME: ${BVPLAN_UNTIS_USERNAME} + BVPLAN_UNTIS_PASSWORD: ${BVPLAN_UNTIS_PASSWORD} + BVPLAN_UNTIS_VPLAN_URL: ${BVPLAN_UNTIS_VPLAN_URL} + BVPLAN_UNTIS_VPLAN_USERNAME: ${BVPLAN_UNTIS_VPLAN_USERNAME} + BVPLAN_UNTIS_VPLAN_PASSWORD: ${BVPLAN_UNTIS_VPLAN_PASSWORD} services: nginx: