Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

main.rs 6.2KB

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. #[async_std::main]
  75. async fn main() -> std::io::Result<()> {
  76. dotenv::dotenv().ok();
  77. tide::log::start();
  78. let mut app = tide::new();
  79. app.with(After(|mut res: Response| async {
  80. if let Some(err) = res.downcast_error::<async_std::io::Error>() {
  81. let tera = Tera::new("templates/**/*.html")?;
  82. let mut context = Context::new();
  83. context.insert("error", &err.to_string());
  84. let html = tera.render("error.html", &context)?;
  85. res.set_body(html);
  86. res.set_status(StatusCode::InternalServerError);
  87. }
  88. Ok(res)
  89. }));
  90. app.with(After(|mut res: Response| async {
  91. res.set_content_type(tide::http::mime::HTML);
  92. Ok(res)
  93. }));
  94. app.with(tide::sessions::SessionMiddleware::new(
  95. tide::sessions::MemoryStore::new(),
  96. std::env::var("TIDE_SECRET")
  97. .expect(
  98. "Please provide a TIDE_SECRET value of at \
  99. least 32 bytes in order to run this example",
  100. )
  101. .as_bytes(),
  102. ));
  103. app.at("/").get(|req: tide::Request<()>| async move {
  104. let tera = Tera::new("templates/**/*.html")?;
  105. let posts = read_all_posts().await?;
  106. let mut context = Context::new();
  107. context.insert("posts", &posts);
  108. let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
  109. context.insert("logged_in", &logged_in);
  110. let html = tera.render("index.html", &context)?;
  111. Ok(Body::from_string(html))
  112. });
  113. app.at("/posts")
  114. .post(|mut req: tide::Request<()>| async move {
  115. let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
  116. if !logged_in {
  117. Ok(tide::Redirect::new("/login"))
  118. } else {
  119. let mut post: Post = req.body_form().await?;
  120. post.id = Uuid::new_v4().to_string();
  121. post.date = Local::now().date().naive_local().to_string();
  122. post.body = post.body.trim().to_owned();
  123. post.save()?;
  124. Ok(tide::Redirect::new("/"))
  125. }
  126. });
  127. app.at("/login")
  128. .get(|mut req: tide::Request<()>| async move {
  129. let tera = Tera::new("templates/**/*.html")?;
  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. let html = tera.render("login.html", &context)?;
  141. Ok(Body::from_string(html))
  142. })
  143. .post(|mut req: tide::Request<()>| async move {
  144. let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
  145. if logged_in {
  146. return Ok(tide::Redirect::new("/"));
  147. }
  148. let username = env::var("ADMIN_USERNAME")?;
  149. let password = env::var("ADMIN_PASSWORD")?;
  150. let user: User = req.body_form().await?;
  151. if user.username == username && user.password == password {
  152. req.session_mut().remove("logged_in");
  153. req.session_mut().insert("logged_in", true)?;
  154. Ok(tide::Redirect::new("/"))
  155. } else {
  156. req.session_mut().remove("logged_in");
  157. req.session_mut().insert("flash_error", "Invalid credentials")?;
  158. Ok(tide::Redirect::new("/login"))
  159. }
  160. });
  161. app.at("/logout").get(|mut req: tide::Request<()>| async move {
  162. req.session_mut().remove("logged_in");
  163. req.session_mut().insert("logged_in", false)?;
  164. Ok(tide::Redirect::new("/"))
  165. });
  166. app.listen("127.0.0.1:8080").await?;
  167. Ok(())
  168. }