The backend of a gist server written in Rust
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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. use diesel::result::Error;
  2. use rocket::http::Status;
  3. use rocket::request::Request;
  4. use rocket::response::status;
  5. use rocket_contrib::json::Json;
  6. use rocket_contrib::templates::Template;
  7. use std::collections::HashMap;
  8. use crate::connection::DbConn;
  9. use crate::snippet;
  10. use crate::snippet::{InsertableSnippet, Snippet};
  11. fn error_response(error: Error) -> Status {
  12. match error {
  13. Error::NotFound => Status::NotFound,
  14. _ => Status::InternalServerError,
  15. }
  16. }
  17. #[get("/")]
  18. pub fn index() -> Template {
  19. let context: HashMap<&str, String> = HashMap::new();
  20. Template::render("index", &context)
  21. }
  22. #[get("/snippets/<id>")]
  23. pub fn show_snippet(id: i32, connection: DbConn) -> Template {
  24. let result = match snippet::get(&connection, id) {
  25. Ok(snippet) => Some(snippet),
  26. Err(_) => None,
  27. };
  28. let (id, title, body) = result.map_or(
  29. (
  30. String::from(""),
  31. String::from("404 - Snippet not found"),
  32. String::from(""),
  33. ),
  34. |snippet| {
  35. (
  36. format!("{}", snippet.id),
  37. snippet.title,
  38. snippet.formatted_body,
  39. )
  40. },
  41. );
  42. let mut context: HashMap<&str, String> = HashMap::new();
  43. context.insert("id", id);
  44. context.insert("title", title);
  45. context.insert("body", body);
  46. Template::render("snippets/show", &context)
  47. }
  48. #[get("/snippets/<id>/raw")]
  49. pub fn show_raw_snippet(id: i32, connection: DbConn) -> String {
  50. let result = match snippet::get(&connection, id) {
  51. Ok(snippet) => Some(snippet),
  52. Err(_) => None,
  53. };
  54. result.map_or(String::from("Snippet not found"), |snippet| snippet.body)
  55. }
  56. #[post("/api/snippets", format = "application/json", data = "<snippet>")]
  57. pub fn create_snippet(
  58. snippet: Json<InsertableSnippet>,
  59. connection: DbConn,
  60. ) -> Result<status::Created<Json<Snippet>>, Status> {
  61. snippet::insert(snippet.into_inner(), &connection)
  62. .map(|snippet| status::Created(String::from(""), Some(Json(snippet))))
  63. .map_err(error_response)
  64. }
  65. #[catch(400)]
  66. pub fn bad_request(req: &Request) -> String {
  67. req.headers().get_one("content-length").map_or(
  68. String::from("{\"message\": \"Bad request\"}"),
  69. |length| {
  70. if length
  71. .parse::<i32>()
  72. .expect("Content length is non-numeric")
  73. > 1_000_000
  74. {
  75. String::from("{\"message\": \"Snippet must be under 1mb\"}")
  76. } else {
  77. String::from("{\"message\": \"Bad request\"}")
  78. }
  79. },
  80. )
  81. }