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.

lib.rs 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. use argon2::{self, Config};
  2. use dotenv::{self};
  3. use tide::utils::After;
  4. mod fs;
  5. mod middleware;
  6. mod post;
  7. mod routes;
  8. mod templates;
  9. mod util;
  10. use middleware::*;
  11. #[derive(Clone)]
  12. pub struct State {
  13. pub login_path: String,
  14. }
  15. pub async fn build_app() -> Result<tide::Server<State>, tide::Error> {
  16. dotenv::dotenv().ok();
  17. let login_path = std::env::var("LOGIN_PATH").unwrap_or(String::from("/login"));
  18. let mut app = tide::with_state(State {
  19. login_path: login_path.clone(),
  20. });
  21. app.with(After(errors));
  22. app.with(session());
  23. app.at("/").get(routes::index);
  24. app.at("/static/:filename").get(routes::static_file);
  25. app.at("/posts")
  26. .with(require_auth)
  27. .post(routes::create_post);
  28. app.at("/posts/new")
  29. .with(require_auth)
  30. .get(routes::new_post);
  31. app.at("/posts/:slug").get(routes::single_post);
  32. app.at("/posts/:slug")
  33. .with(require_auth)
  34. .post(routes::update_post)
  35. .delete(routes::delete_post);
  36. app.at("/posts/:slug/edit")
  37. .with(require_auth)
  38. .get(routes::edit_post);
  39. app.at(&login_path)
  40. .with(require_guest)
  41. .get(routes::login_page)
  42. .post(routes::login);
  43. app.at("/preview")
  44. .with(require_auth)
  45. .post(routes::preview_post);
  46. app.at("/logout").with(require_auth).post(routes::logout);
  47. Ok(app)
  48. }
  49. pub fn hash_password(password: &str, username: &str) -> String {
  50. let password = password.as_bytes();
  51. let salt = format!("{}{}", &username, &username);
  52. let salt = salt.as_bytes();
  53. let config = Config::default();
  54. argon2::hash_encoded(password, salt, &config).unwrap()
  55. }