use actix_web::dev::{AppService, HttpServiceFactory};
use actix_web::guard::Get;
use actix_web::web::Data;
use actix_web::{HttpResponse, Resource, Responder};
pub trait UIPluginConfig {
fn build(self: Box<Self>, openapi_path: &str) -> Box<dyn UIPlugin>;
}
pub trait UIPlugin {
fn path(&self) -> String;
fn to_html(&self) -> String;
}
impl UIPlugin for Box<dyn UIPlugin> {
fn path(&self) -> String {
self.as_ref().path()
}
fn to_html(&self) -> String {
self.as_ref().to_html()
}
}
#[doc(hidden)]
pub struct UIPluginWrapper(Box<dyn UIPlugin>);
impl From<Box<dyn UIPlugin>> for UIPluginWrapper {
fn from(value: Box<dyn UIPlugin>) -> Self {
Self(value)
}
}
impl HttpServiceFactory for UIPluginWrapper {
fn register(self, config: &mut AppService) {
async fn handler(html: Data<String>) -> impl Responder {
HttpResponse::Ok().content_type("text/html").body(html.to_string())
}
let html = self.0.to_html();
Resource::new::<&str>(&self.0.path())
.guard(Get())
.app_data(Data::new(html))
.to(handler)
.register(config);
}
}