Skip to content

Commit

Permalink
WIP ws
Browse files Browse the repository at this point in the history
  • Loading branch information
treeman committed Feb 2, 2024
1 parent 3eaca68 commit 1be4853
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 58 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.markdown
*.dj
49 changes: 40 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@ xml-rs = "0.8.19"
sxd-xpath = "0.4.2"
sxd-document = "0.3.2"
jotdown = "0.3.2"
tokio-websockets = { version = "0.5.1", features = ["server", "openssl"] }
futures-util = "0.3.30"
51 changes: 3 additions & 48 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,25 @@ mod site;
mod site_url;
mod upload;
mod util;
mod watch;

#[cfg(test)]
mod tests;

use crate::site_url::{HrefUrl, ImgUrl};
use axum::{routing::get_service, Router};
use camino::Utf8PathBuf;
use clap::{Parser, Subcommand};
use colored::Colorize;
use eyre::Result;
use futures::future::join_all;
use hotwatch::Hotwatch;
use lazy_static::lazy_static;
use paths::AbsPath;
use reqwest::Client;
use s3::creds::Credentials;
use s3::Bucket;
use s3::Region;
use site::{Site, SiteOptions};
use std::{collections::HashSet, net::SocketAddr, thread, time::Duration};
use tower_http::{services::ServeDir, trace::TraceLayer};
use tracing::{error, info};
use std::collections::HashSet;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use upload::SyncOpts;
use url::Url;
Expand Down Expand Up @@ -113,7 +110,7 @@ async fn main() -> Result<()> {
build()?;
}
Commands::Watch => {
watch().await?;
watch::watch(&OUTPUT_DIR, &CURRENT_DIR).await?;
}
Commands::Post { title } => {
gen::new_post(title.join(" "))?;
Expand Down Expand Up @@ -188,48 +185,6 @@ fn build() -> Result<()> {
Ok(())
}

async fn watch() -> Result<()> {
let mut site = Site::load_content(SiteOptions {
output_dir: OUTPUT_DIR.clone(),
input_dir: CURRENT_DIR.clone(),
clear_output_dir: true,
include_drafts: true,
generate_feed: false,
})?;

site.render_all()?;

tokio::task::spawn_blocking(move || {
let mut hotwatch = Hotwatch::new_with_custom_delay(Duration::from_millis(100))
.expect("hotwatch failed to initialize!");

hotwatch
.watch(".", move |event| {
if let Err(err) = site.file_changed(event) {
error!("{err}");
}
})
.expect("failed to watch folder!");

loop {
thread::sleep(Duration::from_secs(1));
}
});

let app: _ = Router::new()
.fallback(get_service(ServeDir::new(&*OUTPUT_DIR)))
.layer(TraceLayer::new_for_http());

let addr = SocketAddr::from(([127, 0, 0, 1], 8080));

info!("serving site on {addr}");
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await?;

Ok(())
}

async fn check_external_links() -> Result<()> {
build()?;
let files = parse_html_files(&*OUTPUT_DIR)?;
Expand Down
1 change: 0 additions & 1 deletion src/markup/syntax_highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use syntect::{
parsing::SyntaxSet, util::LinesWithEndings,
};
use tracing::{info, warn};
use tracing_subscriber::field::display;

lazy_static! {
static ref SS: SyntaxSet = syntax_set();
Expand Down
87 changes: 87 additions & 0 deletions src/watch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use axum::{routing::get_service, Router};
use eyre::Result;
use futures_util::StreamExt;
use hotwatch::Hotwatch;
use std::{net::SocketAddr, thread, time::Duration};
use tokio::net::TcpListener;
use tokio_websockets::ServerBuilder;
use tower_http::{services::ServeDir, trace::TraceLayer};
use tracing::{error, info};

use crate::paths::AbsPath;
use crate::site::{Site, SiteOptions};

pub async fn watch(output_dir: &AbsPath, current_dir: &AbsPath) -> Result<()> {
let site = Site::load_content(SiteOptions {
output_dir: output_dir.clone(),
input_dir: current_dir.clone(),
clear_output_dir: true,
include_drafts: true,
generate_feed: false,
})?;

site.render_all()?;

start_ws().await;
start_hotwatch(site);
start_watch_server(output_dir).await?;

Ok(())
}

fn start_hotwatch(mut site: Site) {
tokio::task::spawn_blocking(move || {
let mut hotwatch = Hotwatch::new_with_custom_delay(Duration::from_millis(100))
.expect("hotwatch failed to initialize!");

hotwatch
.watch(".", move |event| {
if let Err(err) = site.file_changed(event) {
error!("{err}");
}
})
.expect("failed to watch folder!");

loop {
thread::sleep(Duration::from_secs(1));
}
});
}

async fn run_ws() -> Result<()> {
let addr = "127.0.0.1:8081";
let listener = TcpListener::bind(addr).await?;
info!("events socked on {addr}");

loop {
let (conn, _) = listener.accept().await?;
let mut server = ServerBuilder::new().accept(conn).await?;

while let Some(Ok(msg)) = server.next().await {
info!("Received: {msg:?}");
}
}
}

async fn start_ws() {
tokio::spawn(async move {
run_ws()
.await
.unwrap_or_else(|e| panic!("Websocket crashed {e:?}"));
});
}

async fn start_watch_server(output_dir: &AbsPath) -> Result<()> {
let app: _ = Router::new()
.fallback(get_service(ServeDir::new(&*output_dir)))
.layer(TraceLayer::new_for_http());

let addr = SocketAddr::from(([127, 0, 0, 1], 8080));

info!("serving site on {addr}");
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await?;

Ok(())
}

0 comments on commit 1be4853

Please sign in to comment.