diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..51c8681 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "markdown" +version = "0.1.0" +edition = "2021" + +[dependencies] +notify = "6.1.1" +blocking = "1.6.1" +failure = "0.1.8" +hotwatch = "0.5.0" +time = "0.3.36" +rocket = { version = "0.5.1", features = ["json"] } +serde = { version = "1.0.204", features = ["derive"] } + + +[[bin]] +name = "markdown" +path = "src/main.rs" \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..4f68b26 --- /dev/null +++ b/index.html @@ -0,0 +1,37 @@ + + + + File Content + + + + + +

+
+
+
+
\ No newline at end of file
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..ef4adbf
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,84 @@
+use core::time::Duration;
+use std::{env, process};
+use std::ffi::OsStr;
+use hotwatch::{Hotwatch};
+use rocket::{State, Shutdown};
+use rocket::fs::NamedFile;
+use rocket::response::stream::{Event as SSEvent, EventStream};
+use rocket::serde::Serialize;
+use rocket::tokio::sync::broadcast::{channel, Sender};
+use rocket::tokio::select;
+use std::path::{Path, PathBuf};
+use std::sync::{Arc, Mutex};
+use time::Error;
+
+#[macro_use] extern crate rocket;
+
+#[derive(Debug, Clone, Serialize)]
+#[serde(crate = "rocket::serde")]
+struct FileChange {
+    content: String,
+}
+
+
+#[get("/")]
+async fn index() -> NamedFile {
+    NamedFile::open("index.html").await.unwrap()
+}
+
+#[get("/content")]
+async fn content(file_content: &State>>) -> String {
+    file_content.lock().unwrap().clone()
+}
+
+#[get("/events")]
+async fn events(queue: &State>, mut end: Shutdown) -> EventStream![] {
+    let mut rx = queue.subscribe();
+    EventStream! {
+        loop {
+            let msg = select! {
+                msg = rx.recv() => match msg {
+                    Ok(msg) => msg,
+                    Err(_) => break,
+                },
+                _ = &mut end => break,
+            };
+            yield SSEvent::json(&msg);
+        }
+    }
+}
+
+#[launch]
+fn rocket() -> _ {
+    let file_path = Path::new("/tmp/file.md");
+    
+    let file_content = Arc::new(Mutex::new(std::fs::read_to_string(file_path).unwrap()));
+    let file_content_clone = Arc::clone(&file_content);
+
+    let (tx, _) = channel::(1024);
+    let tx_clone = tx.clone();
+
+
+
+    std::thread::spawn(move || {
+        let mut watch = Hotwatch::new().unwrap();
+        watch.watch(file_path, move |_| {
+
+            let content = std::fs::read_to_string(file_path).unwrap();
+
+            *file_content_clone.lock().unwrap() = content.clone();
+            let _ = tx_clone.send(FileChange { content });
+
+            std::thread::sleep(Duration::from_secs(1));
+        }).unwrap();
+
+        loop { // IDK how to sleep forever
+            std::thread::sleep(Duration::from_secs(9223372036854775807));
+        }
+    });
+
+    rocket::build()
+        .manage(tx)
+        .manage(file_content)
+        .mount("/", routes![index, content, events])
+}
\ No newline at end of file