From 8383914aa5abc232fa56ce437e777d5024eb63e1 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Thu, 1 Apr 2021 22:30:45 -0400 Subject: Unix domain socket http server (#352) * enable ipv6 support Signed-off-by: Christine Dodrill * enable unix socket powers Signed-off-by: Christine Dodrill * unix domain socket post Signed-off-by: Christine Dodrill * bump rust Signed-off-by: Christine Dodrill --- src/app/markdown.rs | 12 +++++++----- src/main.rs | 39 ++++++++++++++++++++++++++++----------- 2 files changed, 35 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/app/markdown.rs b/src/app/markdown.rs index fe33a21..1410776 100644 --- a/src/app/markdown.rs +++ b/src/app/markdown.rs @@ -1,8 +1,8 @@ +use crate::templates::Html; use color_eyre::eyre::{Result, WrapErr}; use comrak::nodes::{Ast, AstNode, NodeValue}; -use comrak::{format_html, parse_document, markdown_to_html, Arena, ComrakOptions}; +use comrak::{format_html, markdown_to_html, parse_document, Arena, ComrakOptions}; use std::cell::RefCell; -use crate::templates::Html; use url::Url; pub fn render(inp: &str) -> Result { @@ -29,6 +29,7 @@ pub fn render(inp: &str) -> Result { if u.scheme() != "conversation" { return Ok(()); } + let smol = u.query().unwrap_or("").contains("smol"); let parent = node.parent().unwrap(); node.detach(); let mut message = vec![]; @@ -41,10 +42,11 @@ pub fn render(inp: &str) -> Result { let name = u.host_str().unwrap_or("Mara"); let mut html = vec![]; - crate::templates::mara(&mut html, mood, name, Html(message))?; + crate::templates::mara(&mut html, mood, name, Html(message.trim().into()), smol)?; - let new_node = - arena.alloc(AstNode::new(RefCell::new(Ast::new(NodeValue::HtmlInline(html))))); + let new_node = arena.alloc(AstNode::new(RefCell::new(Ast::new( + NodeValue::HtmlInline(html), + )))); parent.append(new_node); Ok(()) diff --git a/src/main.rs b/src/main.rs index 7ebfbdf..cac19cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,11 @@ extern crate tracing; use color_eyre::eyre::Result; use hyper::{header::CONTENT_TYPE, Body, Response}; use prometheus::{Encoder, TextEncoder}; +use std::net::IpAddr; +use std::str::FromStr; use std::sync::Arc; +use tokio::net::UnixListener; +use tokio_stream::wrappers::UnixListenerStream; use warp::{path, Filter}; pub mod app; @@ -232,17 +236,30 @@ async fn main() -> Result<()> { } } - warp::serve(site) - .run(( - [0, 0, 0, 0], - std::env::var("PORT") - .unwrap_or("3030".into()) - .parse::() - .unwrap(), - )) - .await; - - Ok(()) + let server = warp::serve(site); + + match std::env::var("SOCKPATH") { + Ok(sockpath) => { + let _ = std::fs::remove_file(&sockpath); + let listener = UnixListener::bind(sockpath)?; + let incoming = UnixListenerStream::new(listener); + server.run_incoming(incoming).await; + + Ok(()) + } + Err(_) => { + server + .run(( + IpAddr::from_str(&std::env::var("HOST").unwrap_or("::".into()))?, + std::env::var("PORT") + .unwrap_or("3030".into()) + .parse::()?, + )) + .await; + + Ok(()) + } + } } include!(concat!(env!("OUT_DIR"), "/templates.rs")); -- cgit v1.2.3