stoptime-rs/src/main.rs

123 lines
3.7 KiB
Rust

#![recursion_limit = "256"]
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate diesel;
#[macro_use]
extern crate diesel_migrations;
use rocket::fairing::AdHoc;
use rocket::{catchers, routes, Rocket};
use rocket_contrib::database;
use rocket_contrib::serve::StaticFiles;
#[macro_use]
mod rest_helpers;
pub mod catchers;
pub mod handlers;
pub mod models;
/// The application database schema
pub mod schema;
// This macro from `diesel_migrations` defines an `embedded_migrations` module containing a
// function named `run`. This allows the example to be run and tested without any outside setup of
// the database.
embed_migrations!();
#[cfg(not(test))]
#[database("stoptime_db")]
pub struct DbConn(diesel::SqliteConnection);
#[cfg(test)]
#[database("stoptime_test_db")]
pub struct DbConn(diesel::SqliteConnection);
#[cfg(test)]
pub mod seed;
/// Runs the database migrations.
fn run_db_migrations(rocket: Rocket) -> Result<Rocket, Rocket> {
println!(" => Running database migrations…");
let conn = DbConn::get_one(&rocket).expect("Failed to get a database connection");
match embedded_migrations::run(&*conn) {
Ok(()) => Ok(rocket),
Err(e) => {
eprintln!("Failed to run database migrations: {:?}", e);
Err(rocket)
}
}
}
/// Run the database seeding (only during tests).
fn run_db_seeding(rocket: Rocket) -> Result<Rocket, Rocket> {
#[cfg(test)]
{
println!(" => Running database seeding…");
let conn = DbConn::get_one(&rocket).expect("Failed to get a database connection");
if let Err(e) = crate::seed::run(conn) {
eprintln!("Failed to run database seeding: {:?}", e);
return Err(rocket);
}
}
Ok(rocket)
}
/// Sets up the Rocket application.
fn rocket() -> Rocket {
let static_dir = concat!(env!("CARGO_MANIFEST_DIR"), "/static");
let static_files = StaticFiles::from(static_dir);
rocket::ignite()
.attach(DbConn::fairing())
.attach(AdHoc::on_attach("Database Migrations", run_db_migrations))
.attach(AdHoc::on_attach("Database Seed", run_db_seeding))
.mount("/", routes![handlers::index])
.mount(
"/company",
routes![handlers::company::index, handlers::company::create],
)
.mount(
"/customers",
routes![
handlers::customers::index,
handlers::customers::create,
handlers::customers::new,
handlers::customers::show,
handlers::customers::update,
handlers::customers::destroy,
handlers::customers::invoices::create,
handlers::customers::invoices::new,
handlers::customers::invoices::show,
handlers::customers::invoices::update,
handlers::customers::tasks::create,
handlers::customers::tasks::new,
handlers::customers::tasks::show,
handlers::customers::tasks::update,
handlers::customers::tasks::destroy,
],
)
.mount("/invoices", routes![handlers::invoices::index])
.mount("/static", static_files)
.mount(
"/timeline",
routes![
handlers::timeline::index,
handlers::timeline::create,
handlers::timeline::new,
handlers::timeline::show,
handlers::timeline::update,
handlers::timeline::destroy,
],
)
.register(catchers![
catchers::not_found,
catchers::unprocessable_entity
])
}
/// Runs the Rocket application.
fn main() {
rocket().launch();
}