widget static resources are now built locally

This commit is contained in:
realaravinth
2021-07-07 00:28:32 +05:30
parent 5d55971f19
commit bfebca6e0e
36 changed files with 2086 additions and 168 deletions

View File

@@ -14,13 +14,9 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use std::borrow::Cow;
use actix_web::body::Body;
use actix_web::{get, http::header, web, HttpResponse, Responder};
//! User facing CAPTCHA widget
use actix_web::{web, HttpResponse, Responder};
use lazy_static::lazy_static;
use mime_guess::from_path;
use rust_embed::RustEmbed;
use sailfish::TemplateOnce;
use crate::errors::*;
@@ -30,16 +26,12 @@ pub const WIDGET_ROUTES: routes::Widget = routes::Widget::new();
pub mod routes {
pub struct Widget {
pub verification_widget: &'static str,
pub js: &'static str,
pub wasm: &'static str,
}
impl Widget {
pub const fn new() -> Self {
Widget {
verification_widget: "/widget",
js: "/widget/bundle.js",
wasm: "/widget/1476099975f2b060264c.module.wasm",
}
}
}
@@ -69,37 +61,9 @@ async fn show_widget() -> PageResult<impl Responder> {
.body(&*INDEX_PAGE))
}
#[derive(RustEmbed)]
#[folder = "static/widget/"]
struct WidgetAssets;
fn handle_widget_assets(path: &str) -> HttpResponse {
match WidgetAssets::get(path) {
Some(content) => {
let body: Body = match content {
Cow::Borrowed(bytes) => bytes.into(),
Cow::Owned(bytes) => bytes.into(),
};
HttpResponse::Ok()
.insert_header(header::CacheControl(vec![
header::CacheDirective::MaxAge(crate::CACHE_AGE),
]))
.content_type(from_path(path).first_or_octet_stream().as_ref())
.body(body)
}
None => HttpResponse::NotFound().body("404 Not Found"),
}
}
#[get("/widget/{_:.*}")]
pub async fn widget_assets(path: web::Path<String>) -> impl Responder {
handle_widget_assets(&path)
}
/// widget services
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(show_widget);
cfg.service(widget_assets);
}
#[cfg(test)]
@@ -112,17 +76,6 @@ mod test {
#[actix_rt::test]
async fn captcha_widget_route_works() {
let mut app = get_app!().await;
// let list_sitekey_resp = test::call_service(
// &mut app,
// test::TestRequest::get()
// .uri(crate::WIDGET_ROUTES.verification_widget)
// .to_request(),
// )
// .await;
// assert_eq!(list_sitekey_resp.status(), StatusCode::OK);
get_works!(app, crate::WIDGET_ROUTES.verification_widget);
get_works!(app, crate::WIDGET_ROUTES.js);
get_works!(app, crate::WIDGET_ROUTES.wasm);
}
}