sound-cards/src/main.rs

77 lines
1.6 KiB
Rust
Raw Normal View History

2017-02-10 12:41:41 +00:00
#![feature(plugin)]
#![plugin(rocket_codegen)]
extern crate glob;
extern crate rand;
extern crate rocket;
extern crate rocket_contrib;
#[macro_use]
extern crate serde_derive;
mod static_files;
// mod tunes;
use glob::glob;
use rand::Rng;
use rocket_contrib::Template;
#[derive(Serialize)]
struct IndexContext {
title: String,
}
#[derive(Serialize)]
struct RandomContext {
title: String,
music_file: String,
music_title: String,
interval: u8,
}
#[get("/")]
fn index() -> Template {
2017-02-10 14:36:02 +00:00
let context = IndexContext { title: String::from("Gäll de kennsch mi nit?") };
2017-02-10 12:41:41 +00:00
Template::render("index", &context)
}
#[get("/random")]
fn random() -> Template {
println!("looking for a random tune...");
// let (file_name, file_title) = tunes::random_tune();
2017-02-10 21:14:15 +00:00
let files = glob("static/sound/*.mp3")
2017-02-10 12:41:41 +00:00
.expect("Failed to read glob pattern")
.collect::<Vec<_>>();
let chosen_one = match *rand::thread_rng().choose(&files).unwrap() {
Ok(ref path) => path,
Err(_) => panic!(""),
};
2017-02-12 17:13:57 +00:00
println!("getting tune information...");
2017-02-10 12:41:41 +00:00
let file_name = chosen_one.file_name().unwrap().to_str().unwrap();
let file_title = chosen_one.file_stem().unwrap().to_str().unwrap();
2017-02-12 17:13:57 +00:00
println!("building context...");
2017-02-10 12:41:41 +00:00
let context = RandomContext {
2017-02-10 14:36:02 +00:00
title: String::from(format!("{}", file_title)),
2017-02-10 12:41:41 +00:00
music_file: String::from(file_name),
music_title: String::from(file_title),
2017-02-10 14:36:02 +00:00
interval: 3,
2017-02-10 12:41:41 +00:00
};
Template::render("random", &context)
}
fn main() {
rocket::ignite()
.mount("/", routes![index, random])
.mount("/static", routes![static_files::all])
.launch();
}