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 5.6KB

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