From e38854c7db0fe6f006304d7f638b6aa190fc2d87 Mon Sep 17 00:00:00 2001 From: mrw1593 Date: Mon, 15 May 2023 21:42:47 -0400 Subject: Started on frontend --- src/resources/scripts.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/resources/scripts.rs (limited to 'src/resources/scripts.rs') diff --git a/src/resources/scripts.rs b/src/resources/scripts.rs new file mode 100644 index 0000000..3e2d869 --- /dev/null +++ b/src/resources/scripts.rs @@ -0,0 +1,37 @@ +use std::path::{Path, PathBuf}; + +use actix_web::{get, http::StatusCode, web, HttpResponse, ResponseError}; +use exun::{Expect, ResultErrorExt}; +use raise::yeet; +use serde::Serialize; +use thiserror::Error; + +#[derive(Debug, Clone, Error, Serialize)] +pub enum LoadScriptError { + #[error("The requested script does not exist")] + FileNotFound(Box), +} + +impl ResponseError for LoadScriptError { + fn status_code(&self) -> StatusCode { + match self { + Self::FileNotFound(..) => StatusCode::NOT_FOUND, + } + } +} + +fn load(script: &str) -> Result> { + let path = PathBuf::from(format!("static/scripts/{}.js", script)); + if !path.exists() { + yeet!(LoadScriptError::FileNotFound(path.into()).into()); + } + let js = std::fs::read_to_string(format!("static/scripts/{}.js", script)).unexpect()?; + Ok(js) +} + +#[get("/{script}.js")] +pub async fn get_js(script: web::Path>) -> Result { + let js = load(&script).map_err(|e| e.unwrap())?; + let response = HttpResponse::Ok().content_type("text/javascript").body(js); + Ok(response) +} -- cgit v1.2.3