2015-11-20 01:43:04 +03:00
|
|
|
#[macro_use] extern crate nickel;
|
2015-11-21 18:21:44 +03:00
|
|
|
#[macro_use] extern crate log;
|
2015-11-20 01:43:04 +03:00
|
|
|
extern crate env_logger;
|
2015-11-21 18:21:44 +03:00
|
|
|
extern crate rustorm;
|
|
|
|
extern crate rustc_serialize;
|
2015-11-22 15:01:39 +03:00
|
|
|
extern crate typemap;
|
|
|
|
extern crate plugin;
|
|
|
|
extern crate image;
|
|
|
|
extern crate hyper;
|
|
|
|
extern crate time;
|
2015-11-20 01:43:04 +03:00
|
|
|
|
2015-11-23 08:48:09 +03:00
|
|
|
mod models;
|
2015-11-22 15:01:39 +03:00
|
|
|
use hyper::header::{Expires, HttpDate};
|
2015-11-23 08:48:09 +03:00
|
|
|
use image::open as image_open;
|
2015-11-29 20:53:09 +03:00
|
|
|
use image::{FilterType, ImageFormat, GenericImage};
|
2015-11-30 00:38:40 +03:00
|
|
|
use models::{Photo, Tag, Person, Place, query_for};
|
2015-11-27 15:05:43 +03:00
|
|
|
use nickel::{MediaType, Nickel, StaticFilesHandler};
|
|
|
|
use plugin::{Pluggable};
|
2015-11-23 08:48:09 +03:00
|
|
|
use rustc_serialize::Encodable;
|
2015-11-27 15:05:43 +03:00
|
|
|
use rustorm::database::{Database};
|
2015-11-23 08:48:09 +03:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use time::Duration;
|
2015-11-21 18:21:44 +03:00
|
|
|
|
2015-11-23 08:48:09 +03:00
|
|
|
mod env;
|
|
|
|
use env::dburl;
|
2015-11-21 18:21:44 +03:00
|
|
|
|
2015-11-27 15:05:43 +03:00
|
|
|
mod rustormmiddleware;
|
|
|
|
use rustormmiddleware::{RustormMiddleware, RustormRequestExtensions};
|
2015-11-23 08:48:09 +03:00
|
|
|
|
2015-11-30 22:31:23 +03:00
|
|
|
mod requestloggermiddleware;
|
|
|
|
use requestloggermiddleware::RequestLoggerMiddleware;
|
|
|
|
|
2015-11-23 08:48:09 +03:00
|
|
|
#[derive(Debug, Clone, RustcEncodable)]
|
|
|
|
struct DetailsData {
|
|
|
|
photo: Photo,
|
2015-11-28 11:53:12 +03:00
|
|
|
people: Vec<Person>,
|
2015-11-30 00:38:40 +03:00
|
|
|
places: Vec<Place>,
|
2015-11-23 08:48:09 +03:00
|
|
|
tags: Vec<Tag>
|
2015-11-22 15:01:39 +03:00
|
|
|
}
|
|
|
|
|
2015-11-27 00:47:39 +03:00
|
|
|
#[derive(Debug, Clone, RustcEncodable)]
|
|
|
|
struct TagData {
|
|
|
|
tag: Tag,
|
|
|
|
photos: Vec<Photo>
|
|
|
|
}
|
|
|
|
|
2015-11-28 11:53:12 +03:00
|
|
|
#[derive(Debug, Clone, RustcEncodable)]
|
|
|
|
struct PersonData {
|
|
|
|
person: Person,
|
|
|
|
photos: Vec<Photo>
|
|
|
|
}
|
|
|
|
|
2015-11-30 00:38:40 +03:00
|
|
|
#[derive(Debug, Clone, RustcEncodable)]
|
|
|
|
struct PlaceData {
|
|
|
|
place: Place,
|
|
|
|
photos: Vec<Photo>
|
|
|
|
}
|
|
|
|
|
2015-11-22 15:01:39 +03:00
|
|
|
fn get_scaled_image(photo: Photo, width: u32, height: u32) -> Vec<u8> {
|
|
|
|
let path = format!("/home/kaj/Bilder/foto/{}", photo.path);
|
|
|
|
info!("Should open {}", path);
|
|
|
|
let img = image_open(path).unwrap();
|
2015-11-29 20:53:09 +03:00
|
|
|
let img =
|
|
|
|
if width < img.width() || height < img.height() {
|
|
|
|
img.resize(width, height, FilterType::Nearest)
|
|
|
|
} else {
|
|
|
|
img
|
|
|
|
};
|
|
|
|
let img = match photo.rotation {
|
2015-11-30 00:38:40 +03:00
|
|
|
_x @ 0...44 => img,
|
|
|
|
_x @ 45...134 => img.rotate90(),
|
|
|
|
_x @ 135...224 => img.rotate180(),
|
|
|
|
_x @ 225...314 => img.rotate270(),
|
|
|
|
_x @ 315...360 => img,
|
2015-11-29 20:34:54 +03:00
|
|
|
x => {
|
|
|
|
warn!("Should rotate photo {} deg, which is unsupported", x);
|
2015-11-29 20:53:09 +03:00
|
|
|
img
|
2015-11-29 20:34:54 +03:00
|
|
|
}
|
|
|
|
};
|
2015-11-22 15:01:39 +03:00
|
|
|
// TODO Put the icon in some kind of cache!
|
|
|
|
let mut buf : Vec<u8> = Vec::new();
|
2015-11-29 20:53:09 +03:00
|
|
|
img.save(&mut buf, ImageFormat::JPEG).unwrap();
|
2015-11-22 15:01:39 +03:00
|
|
|
buf
|
|
|
|
}
|
2015-11-20 01:43:04 +03:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
env_logger::init().unwrap();
|
2015-11-21 18:21:44 +03:00
|
|
|
info!("Initalized logger");
|
|
|
|
// NOTE pool will need to be mut if we do db writes?
|
2015-11-22 15:01:39 +03:00
|
|
|
// let pool = ManagedPool::init(&dburl(), 1).unwrap();
|
2015-11-21 18:21:44 +03:00
|
|
|
info!("Initalized pool");
|
|
|
|
|
2015-11-20 01:43:04 +03:00
|
|
|
let mut server = Nickel::new();
|
2015-11-30 22:31:23 +03:00
|
|
|
server.utilize(RequestLoggerMiddleware);
|
2015-11-27 01:20:36 +03:00
|
|
|
server.utilize(StaticFilesHandler::new("static/"));
|
2015-11-22 15:01:39 +03:00
|
|
|
server.utilize(RustormMiddleware::new(&dburl()));
|
2015-11-20 01:43:04 +03:00
|
|
|
server.utilize(router! {
|
2015-11-22 15:01:39 +03:00
|
|
|
get "/" => |req, res| {
|
2015-11-29 22:06:11 +03:00
|
|
|
let photos: Vec<Photo> = query_for::<Photo>()
|
|
|
|
.filter_gte("grade", &4_i16)
|
|
|
|
.limit(24)
|
2015-11-22 15:01:39 +03:00
|
|
|
.collect(req.db_conn()).unwrap();
|
2015-11-20 01:43:04 +03:00
|
|
|
let mut data = HashMap::new();
|
2015-11-21 18:21:44 +03:00
|
|
|
data.insert("photos", &photos);
|
2015-11-30 22:31:23 +03:00
|
|
|
info!("About to render for /");
|
2015-11-20 01:43:04 +03:00
|
|
|
return res.render("templates/index.tpl", &data);
|
|
|
|
}
|
2015-11-22 15:01:39 +03:00
|
|
|
get "/details/:id" => |req, res| {
|
2015-11-23 08:48:09 +03:00
|
|
|
if let Ok(id) = req.param("id").unwrap().parse::<i32>() {
|
2015-11-27 00:47:39 +03:00
|
|
|
if let Ok(photo) = req.orm_get::<Photo>("id", &id) {
|
|
|
|
|
2015-11-29 19:18:47 +03:00
|
|
|
let people = req.orm_get_related(&photo, "photo_person").unwrap();
|
2015-11-30 00:38:40 +03:00
|
|
|
let places = req.orm_get_related(&photo, "photo_place").unwrap();
|
|
|
|
let tags = req.orm_get_related(&photo, "photo_tag").unwrap();
|
2015-11-27 00:47:39 +03:00
|
|
|
return res.render("templates/details.tpl", &DetailsData {
|
|
|
|
photo: photo,
|
2015-11-28 11:53:12 +03:00
|
|
|
people: people,
|
2015-11-30 00:38:40 +03:00
|
|
|
places: places,
|
2015-11-27 00:47:39 +03:00
|
|
|
tags: tags
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-27 02:02:18 +03:00
|
|
|
get "/tag/" => |req, res| {
|
|
|
|
let tags: Vec<Tag> = query_for::<Tag>().asc("tag")
|
|
|
|
.collect(req.db_conn()).unwrap();
|
|
|
|
let mut data = HashMap::new();
|
|
|
|
data.insert("tags", &tags);
|
|
|
|
return res.render("templates/tags.tpl", &data);
|
|
|
|
}
|
2015-11-27 00:47:39 +03:00
|
|
|
get "/tag/:tag" => |req, res| {
|
|
|
|
let slug = req.param("tag").unwrap();
|
|
|
|
if let Ok(tag) = req.orm_get::<Tag>("slug", &slug) {
|
2015-11-29 19:18:47 +03:00
|
|
|
let photos = req.orm_get_related(&tag, "photo_tag").unwrap();
|
2015-11-27 00:47:39 +03:00
|
|
|
return res.render("templates/tag.tpl", &TagData {
|
|
|
|
tag: tag,
|
|
|
|
photos: photos
|
2015-11-28 11:53:12 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
get "/person/" => |req, res| {
|
|
|
|
let people: Vec<Person> = query_for::<Person>().asc("name")
|
|
|
|
.collect(req.db_conn()).unwrap();
|
|
|
|
let mut data = HashMap::new();
|
|
|
|
data.insert("people", &people);
|
|
|
|
return res.render("templates/people.tpl", &data);
|
|
|
|
}
|
|
|
|
get "/person/:slug" => |req, res| {
|
|
|
|
let slug = req.param("slug").unwrap();
|
|
|
|
if let Ok(person) = req.orm_get::<Person>("slug", &slug) {
|
2015-11-29 19:18:47 +03:00
|
|
|
let photos = req.orm_get_related(&person, "photo_person").unwrap();
|
2015-11-28 11:53:12 +03:00
|
|
|
return res.render("templates/person.tpl", &PersonData {
|
|
|
|
person: person,
|
|
|
|
photos: photos
|
2015-11-30 00:38:40 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
get "/place/" => |req, res| {
|
|
|
|
let places: Vec<Place> = query_for::<Place>().asc("place")
|
|
|
|
.collect(req.db_conn()).unwrap();
|
|
|
|
let mut data = HashMap::new();
|
|
|
|
data.insert("places", &places);
|
|
|
|
return res.render("templates/places.tpl", &data);
|
|
|
|
}
|
|
|
|
get "/place/:slug" => |req, res| {
|
|
|
|
let slug = req.param("slug").unwrap();
|
|
|
|
if let Ok(place) = req.orm_get::<Place>("slug", &slug) {
|
|
|
|
let photos = req.orm_get_related(&place, "photo_place").unwrap();
|
|
|
|
return res.render("templates/place.tpl", &PlaceData {
|
|
|
|
place: place,
|
|
|
|
photos: photos
|
2015-11-23 08:48:09 +03:00
|
|
|
});
|
|
|
|
}
|
2015-11-22 15:01:39 +03:00
|
|
|
}
|
|
|
|
get "/icon/:id" => |req, mut res| {
|
2015-11-23 08:48:09 +03:00
|
|
|
if let Ok(id) = req.param("id").unwrap().parse::<i32>() {
|
2015-11-27 00:47:39 +03:00
|
|
|
if let Ok(photo) = req.orm_get::<Photo>("id", &id) {
|
|
|
|
let buf = get_scaled_image(photo, 200, 180);
|
|
|
|
res.set(MediaType::Jpeg);
|
2015-11-27 20:38:46 +03:00
|
|
|
res.set(Expires(HttpDate(time::now() + Duration::days(14))));
|
2015-11-27 00:47:39 +03:00
|
|
|
return res.send(buf);
|
|
|
|
}
|
2015-11-23 08:48:09 +03:00
|
|
|
}
|
2015-11-22 15:01:39 +03:00
|
|
|
}
|
|
|
|
get "/view/:id" => |req, mut res| {
|
2015-11-23 08:48:09 +03:00
|
|
|
if let Ok(id) = req.param("id").unwrap().parse::<i32>() {
|
2015-11-27 00:47:39 +03:00
|
|
|
if let Ok(photo) = req.orm_get::<Photo>("id", &id) {
|
|
|
|
let buf = get_scaled_image(photo, 800, 600);
|
|
|
|
res.set(MediaType::Jpeg);
|
2015-11-27 20:38:46 +03:00
|
|
|
res.set(Expires(HttpDate(time::now() + Duration::days(14))));
|
2015-11-27 00:47:39 +03:00
|
|
|
return res.send(buf);
|
|
|
|
}
|
2015-11-23 08:48:09 +03:00
|
|
|
}
|
2015-11-22 15:01:39 +03:00
|
|
|
}
|
2015-11-20 01:43:04 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
server.listen("127.0.0.1:6767");
|
|
|
|
}
|