Separate into mod

This commit is contained in:
Daniel 2023-05-18 11:01:04 -04:00
parent 402fe18e18
commit fb80fdb6e2
5 changed files with 57 additions and 18 deletions

View file

@ -2,7 +2,7 @@
refresh_rate = "1s" refresh_rate = "1s"
[server] [server]
address="127.0.0.1:8020" address="0.0.0.0:8020"
#url = "https://rustypaste.shuttleapp.rs" #url = "https://rustypaste.shuttleapp.rs"
#workers=4 #workers=4
max_content_length = "10MB" max_content_length = "10MB"
@ -10,6 +10,7 @@ upload_path = "./upload"
timeout = "30s" timeout = "30s"
expose_version = false expose_version = false
style="monokai" style="monokai"
pretty_default = true
landing_page = """ landing_page = """

View file

@ -48,6 +48,8 @@ pub struct ServerConfig {
pub landing_page: Option<String>, pub landing_page: Option<String>,
/// Expose version. /// Expose version.
pub expose_version: Option<bool>, pub expose_version: Option<bool>,
/// Default to pretty when Accept: allows it.
pub pretty_default: Option<bool>,
/// Highlight.js style /// Highlight.js style
pub style: Option<String>, pub style: Option<String>,
} }

View file

@ -10,6 +10,9 @@ pub mod random;
/// Server routes. /// Server routes.
pub mod server; pub mod server;
/// Pretty renderer.
pub mod pretty;
/// HTTP headers. /// HTTP headers.
pub mod header; pub mod header;

47
src/pretty.rs Normal file
View file

@ -0,0 +1,47 @@
use crate::config::Config;
use actix_web::{web, HttpRequest, HttpResponse, Error, http::header};
use mime::Mime;
use text_template::*;
use std::collections::HashMap;
use std::str;
/// Render a pretty HttpResponse.
pub fn render_pretty(
file: web::Path<String>,
mime_type: Mime,
config: &Config,
) -> Result<HttpResponse, Error> {
let mut template_values = HashMap::new();
let tmpl_bytes = str::from_utf8(include_bytes!("pretty.html")).unwrap();
let tmpl = Template::from(tmpl_bytes);
template_values.insert("file", file.as_str());
template_values.insert("style", match &config.server.style {
Some(style) => style.as_str(),
None => "default",
});
let mime_str = mime_type.to_string();
if let Some(overrides) = &config.paste.highlight_override {
template_values.insert("type", if overrides.contains_key(&mime_str) { overrides[&mime_str].as_str() } else { "" });
}
let rendered = tmpl.fill_in(&template_values);
Ok(HttpResponse::Ok().content_type(mime::TEXT_HTML).body(rendered.to_string()))
}
/// Check whether the request wants pretty mode.
pub fn want_pretty(
request: &HttpRequest,
by_default: bool,
) -> bool {
if request.query_string() == "nopretty" {
return false;
}
let mut accepts_html = false;
if let Some(accept) = request.headers().get(header::ACCEPT) {
accepts_html = accept.to_str().unwrap_or_default().find("text/html").is_some();
}
request.query_string() == "pretty" || (by_default && accepts_html)
}

View file

@ -4,9 +4,9 @@ use crate::file::Directory;
use crate::header::{self, ContentDisposition}; use crate::header::{self, ContentDisposition};
use crate::mime as mime_util; use crate::mime as mime_util;
use crate::paste::{Paste, PasteType}; use crate::paste::{Paste, PasteType};
use crate::pretty;
use crate::util; use crate::util;
use crate::AUTH_TOKEN_ENV; use crate::AUTH_TOKEN_ENV;
use text_template::*;
use actix_files::NamedFile; use actix_files::NamedFile;
use actix_multipart::Multipart; use actix_multipart::Multipart;
use actix_web::{error, get, post, web, Error, HttpRequest, HttpResponse}; use actix_web::{error, get, post, web, Error, HttpRequest, HttpResponse};
@ -19,7 +19,6 @@ use std::env;
use std::fs; use std::fs;
use std::str; use std::str;
use std::sync::RwLock; use std::sync::RwLock;
use std::collections::HashMap;
/// Shows the landing page. /// Shows the landing page.
#[get("/")] #[get("/")]
@ -85,21 +84,8 @@ async fn serve(
.map_err(error::ErrorInternalServerError)? .map_err(error::ErrorInternalServerError)?
}; };
if request.query_string() == "pretty" { if pretty::want_pretty(&request, config.server.pretty_default.unwrap_or(false)) {
let mut values = HashMap::new(); return pretty::render_pretty(file, mime_type, &config)
let tmpl_bytes = str::from_utf8(include_bytes!("pretty.html")).unwrap();
let tmpl = Template::from(tmpl_bytes);
values.insert("file", file.as_str());
values.insert("style", match &config.server.style {
Some(style) => style.as_str(),
None => "default",
});
let mime_str = mime_type.to_string();
if let Some(overrides) = &config.paste.highlight_override {
values.insert("type", if overrides.contains_key(&mime_str) { overrides[&mime_str].as_str() } else { "" });
}
let rendered = tmpl.fill_in(&values);
return Ok(HttpResponse::Ok().content_type(mime::TEXT_HTML).body(rendered.to_string()))
} }
let response = NamedFile::open(&path)? let response = NamedFile::open(&path)?