Modifica la definición de apps como un módulo más
This commit is contained in:
parent
61813a44e5
commit
5c65642ec0
13 changed files with 384 additions and 137 deletions
|
|
@ -1,16 +1,25 @@
|
|||
use pagetop::prelude::*;
|
||||
|
||||
pub_const_handler!(APP_PAGETOP_WEBSITE);
|
||||
|
||||
mod mdbook;
|
||||
|
||||
struct PageTopWebSite;
|
||||
|
||||
impl AppTrait for PageTopWebSite {
|
||||
fn enable_modules(&self) -> Vec<ModuleStaticRef> {
|
||||
vec![&mdbook::MdBook]
|
||||
impl ModuleTrait for PageTopWebSite {
|
||||
fn handler(&self) -> Handler {
|
||||
APP_PAGETOP_WEBSITE
|
||||
}
|
||||
|
||||
fn dependencies(&self) -> Vec<ModuleStaticRef> {
|
||||
vec![
|
||||
&mdbook::MdBook,
|
||||
&pagetop::base::module::homepage::DefaultHomePage,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
Application::prepare(PageTopWebSite).await?.run()?.await
|
||||
Application::prepare(&PageTopWebSite).await?.run()?.await
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,12 +5,12 @@ use static_files::Resource;
|
|||
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub_const_handler!(MODULE_MDBOOK);
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/mdbook.rs"));
|
||||
|
||||
static MDBOOK: LazyStatic<HashMap<&'static str, Resource>> = LazyStatic::new(generate);
|
||||
|
||||
pub_const_handler!(MODULE_MDBOOK);
|
||||
|
||||
pub struct MdBook;
|
||||
|
||||
impl ModuleTrait for MdBook {
|
||||
|
|
@ -19,19 +19,41 @@ impl ModuleTrait for MdBook {
|
|||
}
|
||||
|
||||
fn configure_service(&self, cfg: &mut app::web::ServiceConfig) {
|
||||
cfg.service(
|
||||
app::web::scope("/doc")
|
||||
.route("{tail:.*html$}", app::web::get().to(mdbook_page))
|
||||
.route("{tail:.*$}", app::web::get().to(mdbook_resource)),
|
||||
);
|
||||
configure_mdbook_service(cfg, "/doc/", &MDBOOK);
|
||||
}
|
||||
}
|
||||
|
||||
async fn mdbook_page(request: app::HttpRequest) -> ResultPage<Markup, FatalError> {
|
||||
// Remove initial "/doc/" from path:
|
||||
let path = &request.path()[5..];
|
||||
fn configure_mdbook_service(
|
||||
cfg: &mut app::web::ServiceConfig,
|
||||
url_root: &'static str,
|
||||
book: &'static HashMap<&'static str, Resource>,
|
||||
) {
|
||||
let url = url_root.trim_end_matches('/');
|
||||
let url_len = url.len() + 1;
|
||||
cfg.service(
|
||||
app::web::scope(url)
|
||||
.route(
|
||||
"{tail:.*html$}",
|
||||
app::web::get()
|
||||
.to(move |request: app::HttpRequest| mdbook_page(request, url_len, book)),
|
||||
)
|
||||
.route(
|
||||
"{tail:.*$}",
|
||||
app::web::get()
|
||||
.to(move |request: app::HttpRequest| mdbook_resource(request, url_len, book)),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if let Some(content) = MDBOOK.get(path) {
|
||||
async fn mdbook_page(
|
||||
request: app::HttpRequest,
|
||||
v: usize,
|
||||
book: &HashMap<&'static str, Resource>,
|
||||
) -> ResultPage<Markup, FatalError> {
|
||||
// Remove initial "/doc/" from path:
|
||||
let path = &request.path()[v..];
|
||||
|
||||
if let Some(content) = book.get(path) {
|
||||
if let Ok(html) = std::str::from_utf8(content.data) {
|
||||
let _lang = extract("Lang", html);
|
||||
let title = match extract("Title", html) {
|
||||
|
|
@ -93,13 +115,17 @@ async fn mdbook_page(request: app::HttpRequest) -> ResultPage<Markup, FatalError
|
|||
}
|
||||
}
|
||||
|
||||
async fn mdbook_resource(request: app::HttpRequest) -> app::HttpResponse {
|
||||
async fn mdbook_resource(
|
||||
request: app::HttpRequest,
|
||||
v: usize,
|
||||
book: &HashMap<&'static str, Resource>,
|
||||
) -> app::HttpResponse {
|
||||
// Remove initial "/doc/" from path:
|
||||
let path = &request.path()[5..];
|
||||
let path = &request.path()[v..];
|
||||
|
||||
// From https://github.com/kilork/actix-web-static-files/blob/master/src/resource_files.rs, see
|
||||
// functions respond_to(), any_match() and none_match().
|
||||
if let Some(file) = &MDBOOK.get(path) {
|
||||
if let Some(file) = &book.get(path) {
|
||||
let etag = Some(app::http::header::EntityTag::new_strong(format!(
|
||||
"{:x}:{:x}",
|
||||
file.data.len(),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue