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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. use dotenv;
  2. use tide::utils::After;
  3. use middleware::*;
  4. mod fs;
  5. mod middleware;
  6. mod post;
  7. mod routes;
  8. #[derive(Clone)]
  9. pub struct State {
  10. pub login_path: String,
  11. }
  12. #[async_std::main]
  13. async fn main() -> std::io::Result<()> {
  14. dotenv::dotenv().ok();
  15. tide::log::start();
  16. let login_path = std::env::var("LOGIN_PATH").unwrap_or(String::from("/login"));
  17. let mut app = tide::with_state(State {
  18. login_path: login_path.clone(),
  19. });
  20. app.at("/static").serve_dir("static")?;
  21. app.with(After(errors));
  22. app.with(session());
  23. app.at("/").get(routes::index);
  24. app.at("/posts")
  25. .with(require_auth)
  26. .post(routes::create_post);
  27. app.at("/posts/:id")
  28. .get(routes::single_post)
  29. .post(routes::update_post)
  30. .delete(routes::delete_post);
  31. app.at("/posts/:id/edit")
  32. .with(require_auth)
  33. .get(routes::edit_post);
  34. app.at(&login_path)
  35. .with(require_guest)
  36. .get(routes::login_page)
  37. .post(routes::login);
  38. app.at("/logout").with(require_auth).post(routes::logout);
  39. app.listen("127.0.0.1:8080").await?;
  40. Ok(())
  41. }