2019-06-08 23:42:51 +02:00
|
|
|
#![recursion_limit = "256"]
|
2019-06-08 14:16:55 +02:00
|
|
|
#![feature(proc_macro_hygiene, decl_macro)]
|
|
|
|
|
2019-06-08 23:42:51 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel;
|
2019-06-21 21:02:58 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel_migrations;
|
2019-06-08 23:42:51 +02:00
|
|
|
|
2019-06-21 21:02:58 +02:00
|
|
|
use rocket::fairing::AdHoc;
|
2019-06-09 18:16:55 +02:00
|
|
|
use rocket::{catchers, routes, Rocket};
|
2019-06-20 20:01:29 +02:00
|
|
|
use rocket_contrib::database;
|
2019-06-08 16:13:41 +02:00
|
|
|
use rocket_contrib::serve::StaticFiles;
|
2019-06-08 14:16:55 +02:00
|
|
|
|
2019-07-09 12:20:53 +02:00
|
|
|
#[macro_use]
|
|
|
|
pub mod helpers;
|
|
|
|
|
2019-06-09 18:16:55 +02:00
|
|
|
pub mod catchers;
|
2019-07-09 12:24:43 +02:00
|
|
|
pub mod graphql;
|
2019-06-08 16:15:14 +02:00
|
|
|
pub mod handlers;
|
2019-07-09 12:24:43 +02:00
|
|
|
pub mod models;
|
|
|
|
pub mod schema;
|
2019-06-08 14:16:55 +02:00
|
|
|
|
2019-06-21 21:02:58 +02:00
|
|
|
// 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!();
|
|
|
|
|
2019-06-20 20:01:29 +02:00
|
|
|
#[database("stoptime_db")]
|
|
|
|
pub struct DbConn(diesel::SqliteConnection);
|
|
|
|
|
2019-06-21 21:02:58 +02:00
|
|
|
/// Runs the database migrations.
|
|
|
|
fn run_db_migrations(rocket: Rocket) -> Result<Rocket, Rocket> {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-20 20:01:29 +02:00
|
|
|
/// Sets up the Rocket application.
|
2019-06-08 14:16:55 +02:00
|
|
|
fn rocket() -> Rocket {
|
2019-06-08 16:13:41 +02:00
|
|
|
let static_dir = concat!(env!("CARGO_MANIFEST_DIR"), "/static");
|
|
|
|
let static_files = StaticFiles::from(static_dir);
|
2019-06-08 16:15:14 +02:00
|
|
|
|
2019-06-08 16:13:41 +02:00
|
|
|
rocket::ignite()
|
2019-06-20 20:01:29 +02:00
|
|
|
.attach(DbConn::fairing())
|
2019-06-21 21:02:58 +02:00
|
|
|
.attach(AdHoc::on_attach("Database Migrations", run_db_migrations))
|
2019-07-09 12:24:43 +02:00
|
|
|
.manage(graphql::Schema::new(graphql::Query, graphql::Mutation))
|
2019-06-08 16:15:14 +02:00
|
|
|
.mount(
|
2019-07-10 20:54:43 +02:00
|
|
|
"/",
|
2019-06-08 16:15:14 +02:00
|
|
|
routes![
|
2019-07-10 20:54:43 +02:00
|
|
|
handlers::index,
|
|
|
|
handlers::graphql_playground,
|
|
|
|
handlers::graphql_get,
|
|
|
|
handlers::graphql_post,
|
2019-06-08 16:15:14 +02:00
|
|
|
],
|
|
|
|
)
|
2019-06-08 16:13:41 +02:00
|
|
|
.mount("/static", static_files)
|
2019-07-09 12:24:43 +02:00
|
|
|
.register(catchers![catchers::not_found])
|
2019-06-08 14:16:55 +02:00
|
|
|
}
|
|
|
|
|
2019-06-20 20:01:29 +02:00
|
|
|
/// Runs the Rocket application.
|
2019-06-08 14:16:55 +02:00
|
|
|
fn main() {
|
|
|
|
rocket().launch();
|
|
|
|
}
|