diff options
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 50 |
1 files changed, 42 insertions, 8 deletions
diff --git a/src/main.rs b/src/main.rs index b5665b9..a4f5946 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,30 +24,59 @@ extern crate image; extern crate rocket; #[macro_use] extern crate rocket_contrib; +#[macro_use] +extern crate serde_derive; mod database; -use database::TagDatabase; +use database::{Query, TagDatabase}; use image::{GenericImageView, ImageFormat}; +use rocket::State; use rocket_contrib::json::{Json, JsonValue}; use std::env; use std::fs; use std::fs::File; use std::os::unix::fs::symlink; use std::path::Path; +use std::sync::Mutex; #[get("/")] -fn index() -> &'static str { +fn index(tb: State<Mutex<TagDatabase>>) -> &'static str { "Hello, world!" } +#[derive(Serialize)] +struct ImageResult { + filename: String, + tags: Vec<String>, +} + #[get("/posts?<tags>")] -fn posts(tags: String) -> Json<Vec<String>> { - let tags: Vec<String> = tags.split(",").map(|s| s.into()).collect(); - Json(tags) +fn posts(tb: State<Mutex<TagDatabase>>, tags: String) -> Json<Vec<ImageResult>> { + let tb = tb.inner().lock().unwrap(); + + let tags = tags.split(",").collect::<Vec<&str>>(); + let results = tb + .query(Query { + tags: &tags[..], + last_id: Some(0), + limit: Some(50), + }) + .unwrap(); + + Json( + results + .iter() + .map(|image| ImageResult { + filename: image.filename.clone(), + tags: tb.tags_for_image(image.id).unwrap(), + }) + .collect(), + ) } -fn create_image_directory() { +/// Maybe initialize the directory for storing image symlinks and thumbnails. +fn create_image_directory() -> TagDatabase { let bin = env::current_exe().unwrap(); let image_dir = bin.parent().unwrap().join("image/"); fs::create_dir(&image_dir).ok(); @@ -72,9 +101,14 @@ fn create_image_directory() { .unwrap(); } } + + tb } fn main() { - create_image_directory(); - rocket::ignite().mount("/", routes![index, posts]).launch(); + let tb = create_image_directory(); + rocket::ignite() + .manage(Mutex::new(tb)) + .mount("/", routes![index, posts]) + .launch(); } |