You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.rs 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. use std::env;
  2. use std::ffi::OsStr;
  3. use std::fs::{read_dir, File};
  4. use std::io::prelude::*;
  5. use std::io::{Error, ErrorKind};
  6. use std::path::PathBuf;
  7. use async_std::fs::read_to_string;
  8. use chrono::prelude::Local;
  9. use dotenv;
  10. use serde::{Deserialize, Serialize};
  11. use serde_json;
  12. use tera::{Context, Tera};
  13. use tide::utils::After;
  14. use tide::{Body, Response, StatusCode};
  15. use uuid::Uuid;
  16. #[derive(Debug, Serialize, Deserialize)]
  17. struct Post {
  18. id: String,
  19. title: String,
  20. body: String,
  21. date: String,
  22. }
  23. #[derive(Debug, Serialize, Deserialize)]
  24. struct User {
  25. username: String,
  26. password: String,
  27. }
  28. impl Post {
  29. fn save(&mut self) -> std::io::Result<()> {
  30. let mut path: PathBuf = get_posts_directory()?;
  31. let filename = format!("{}.json", self.id);
  32. path = path.join(&filename);
  33. let mut file = File::create(&path)?;
  34. file.write_all(serde_json::to_string(&self)?.as_bytes())?;
  35. Ok(())
  36. }
  37. }
  38. fn get_posts_directory() -> Result<PathBuf, Error> {
  39. match env::var("POSTS_DIR") {
  40. Ok(dir) => Ok(dir.into()),
  41. Err(_) => Err(Error::new(
  42. ErrorKind::Other,
  43. "Posts directory environment variable not set",
  44. )),
  45. }
  46. }
  47. async fn read_all_posts() -> Result<Vec<Post>, Error> {
  48. let path = match get_posts_directory() {
  49. Ok(p) => Ok(p),
  50. Err(_) => Err(Error::new(ErrorKind::Other, "POSTS_DIR variable is not set"))
  51. }?;
  52. let mut posts: Vec<Post> = vec![];
  53. let files = match read_dir(path) {
  54. Ok(f) => Ok(f),
  55. Err(_) => Err(Error::new(ErrorKind::Other, "Posts directory does not exist"))
  56. }?;
  57. for file in files {
  58. let file = file?;
  59. if let Some("json") = file.path().extension().and_then(OsStr::to_str) {
  60. let contents = match read_to_string(file.path()).await {
  61. Ok(c) => Ok(c),
  62. Err(_) => Err(Error::new(ErrorKind::Other, format!("Error reading post {:?}", file.path())))
  63. }?;
  64. let mut post: Post = match serde_json::from_str(&contents) {
  65. Ok(p) => Ok(p),
  66. Err(_) => Err(Error::new(ErrorKind::Other, format!("Error deserializing post")))
  67. }?;
  68. post.body = post.body.replace("\n", "<br>");
  69. posts.push(post);
  70. }
  71. }
  72. Ok(posts)
  73. }
  74. fn render(template: &str, context: &Context) -> Result<Body, tide::Error> {
  75. let tera = Tera::new("templates/**/*.html")?;
  76. let html = tera.render(template, &context)?;
  77. Ok(Body::from_string(html))
  78. }
  79. #[async_std::main]
  80. async fn main() -> std::io::Result<()> {
  81. dotenv::dotenv().ok();
  82. tide::log::start();
  83. let mut app = tide::new();
  84. app.with(After(|mut res: Response| async {
  85. if let Some(err) = res.downcast_error::<async_std::io::Error>() {
  86. let mut context = Context::new();
  87. context.insert("error", &err.to_string());
  88. res.set_body(render("error.html", &context)?);
  89. res.set_status(StatusCode::InternalServerError);
  90. }
  91. Ok(res)
  92. }));
  93. app.with(After(|mut res: Response| async {
  94. res.set_content_type(tide::http::mime::HTML);
  95. Ok(res)
  96. }));
  97. app.with(tide::sessions::SessionMiddleware::new(
  98. tide::sessions::MemoryStore::new(),
  99. std::env::var("TIDE_SECRET")
  100. .expect(
  101. "Please provide a TIDE_SECRET value of at \
  102. least 32 bytes in order to run this example",
  103. )
  104. .as_bytes(),
  105. ));
  106. app.at("/").get(|req: tide::Request<()>| async move {
  107. let posts = read_all_posts().await?;
  108. let mut context = Context::new();
  109. context.insert("posts", &posts);
  110. let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
  111. context.insert("logged_in", &logged_in);
  112. render("index.html", &context)
  113. });
  114. app.at("/posts")
  115. .post(|mut req: tide::Request<()>| async move {
  116. let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
  117. if !logged_in {
  118. Ok(tide::Redirect::new("/login"))
  119. } else {
  120. let mut post: Post = req.body_form().await?;
  121. post.id = Uuid::new_v4().to_string();
  122. post.date = Local::now().date().naive_local().to_string();
  123. post.body = post.body.trim().to_owned();
  124. post.save()?;
  125. Ok(tide::Redirect::new("/"))
  126. }
  127. });
  128. app.at("/login")
  129. .get(|mut req: tide::Request<()>| async move {
  130. let mut context = Context::new();
  131. let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
  132. context.insert("logged_in", &logged_in);
  133. match req.session_mut().get::<String>("flash_error") {
  134. Some(error) => {
  135. req.session_mut().remove("flash_error");
  136. &context.insert("error", &error);
  137. }
  138. None => {}
  139. }
  140. render("login.html", &context)
  141. })
  142. .post(|mut req: tide::Request<()>| async move {
  143. let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
  144. if logged_in {
  145. return Ok(tide::Redirect::new("/"));
  146. }
  147. let username = env::var("ADMIN_USERNAME")?;
  148. let password = env::var("ADMIN_PASSWORD")?;
  149. let user: User = req.body_form().await?;
  150. if user.username == username && user.password == password {
  151. req.session_mut().remove("logged_in");
  152. req.session_mut().insert("logged_in", true)?;
  153. Ok(tide::Redirect::new("/"))
  154. } else {
  155. req.session_mut().remove("logged_in");
  156. req.session_mut().insert("flash_error", "Invalid credentials")?;
  157. Ok(tide::Redirect::new("/login"))
  158. }
  159. });
  160. app.at("/logout").get(|mut req: tide::Request<()>| async move {
  161. req.session_mut().remove("logged_in");
  162. req.session_mut().insert("logged_in", false)?;
  163. Ok(tide::Redirect::new("/"))
  164. });
  165. app.listen("127.0.0.1:8080").await?;
  166. Ok(())
  167. }