feat: serve previews
This commit is contained in:
parent
4d136f2407
commit
c51df7daf2
2 changed files with 50 additions and 18 deletions
|
@ -29,6 +29,7 @@ mod errors;
|
||||||
mod git;
|
mod git;
|
||||||
mod meta;
|
mod meta;
|
||||||
mod page;
|
mod page;
|
||||||
|
mod preview;
|
||||||
mod routes;
|
mod routes;
|
||||||
mod serve;
|
mod serve;
|
||||||
mod settings;
|
mod settings;
|
||||||
|
|
37
src/serve.rs
37
src/serve.rs
|
@ -56,10 +56,42 @@ async fn index(req: HttpRequest, ctx: AppCtx) -> ServiceResult<impl Responder> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if host == ctx.settings.server.domain || host == "localhost" {
|
if host == ctx.settings.server.domain || host == "localhost" {
|
||||||
Ok(HttpResponse::Ok()
|
return Ok(HttpResponse::Ok()
|
||||||
.content_type(ContentType::html())
|
.content_type(ContentType::html())
|
||||||
.body("Welcome to Librepages!"))
|
.body("Welcome to Librepages!"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if host.contains(&ctx.settings.server.domain) {
|
||||||
|
let extractor = crate::preview::Preview::new(&ctx);
|
||||||
|
if let Some(preview_branch) = extractor.extract(host) {
|
||||||
|
unimplemented!(
|
||||||
|
"map a local subdomain on settings.server.domain and use it to fetch page"
|
||||||
|
);
|
||||||
|
let res = match find_page(host, &ctx) {
|
||||||
|
Some(page) => {
|
||||||
|
log::debug!("Page found");
|
||||||
|
let content = crate::git::read_preview_file(
|
||||||
|
&page.path,
|
||||||
|
preview_branch,
|
||||||
|
req.uri().path(),
|
||||||
|
)?;
|
||||||
|
let mime = if let Some(mime) = content.mime.first_raw() {
|
||||||
|
mime
|
||||||
} else {
|
} else {
|
||||||
|
"text/html; charset=utf-8"
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(HttpResponse::Ok()
|
||||||
|
//.content_type(ContentType::html())
|
||||||
|
.content_type(mime)
|
||||||
|
.body(content.content.bytes()))
|
||||||
|
}
|
||||||
|
None => Err(ServiceError::WebsiteNotFound),
|
||||||
|
};
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
match find_page(host, &ctx) {
|
match find_page(host, &ctx) {
|
||||||
Some(page) => {
|
Some(page) => {
|
||||||
log::debug!("Page found");
|
log::debug!("Page found");
|
||||||
|
@ -78,7 +110,6 @@ async fn index(req: HttpRequest, ctx: AppCtx) -> ServiceResult<impl Responder> {
|
||||||
None => Err(ServiceError::WebsiteNotFound),
|
None => Err(ServiceError::WebsiteNotFound),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
pub fn services(cfg: &mut web::ServiceConfig) {
|
pub fn services(cfg: &mut web::ServiceConfig) {
|
||||||
cfg.service(index);
|
cfg.service(index);
|
||||||
|
|
Loading…
Add table
Reference in a new issue