1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#![feature(decl_macro, never_type, plugin, proc_macro_hygiene, try_from)]

#[macro_use]
pub extern crate bson;

extern crate base64;
extern crate bcrypt;
#[macro_use]
extern crate error_chain;
extern crate jsonwebtoken;
extern crate mongodb;
#[macro_use]
extern crate parker_codegen;
extern crate pulldown_cmark;
#[macro_use]
extern crate rocket;
extern crate rocket_contrib;
#[macro_use]
extern crate serde;

// Re-export macros
pub use bson::{bson, doc};
pub use mongodb::coll::options::FindOptions;

use std::{collections::HashMap as Map, convert::TryFrom};

use mongodb::db::Database;
use rocket::{fairing::AdHoc, routes, Config as RocketConfig, Rocket};
use rocket_contrib::{serve::StaticFiles, templates::Template};

// Local modules
mod config;
mod doc;
mod error;
mod jwt;
mod merge;
mod query;
mod render;
mod routes;
mod user;
mod view;
mod views;
mod webpage;

pub use crate::{query::Query, view::View};

use crate::{
    config::ParkerConfig, error::Result, jwt::JwtHandler, user::UserRolesToken,
    views::Views,
};

#[derive(Default)]
pub struct Parker {
    views: Map<String, View>,
}

impl Parker {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn view(mut self, name: &str, view: View) -> Self {
        self.views.insert(name.to_string(), view);
        self
    }

    pub fn launch(self) {
        // Configure Rocket and start the server
        rocket::ignite()
            // Fairings for additional functionality
            .attach(Template::custom(render::init_template_engines))
            .attach(AdHoc::on_attach("Parker Initialization", |rocket| {
                // Initialize all required objects. If any error occurs, this
                // will fail and scrub the launch.
                match InitObject::init(self, rocket.config()) {
                    Ok(init_obj) => Ok(init_obj.apply(rocket)),
                    Err(_) => {
                        // TODO log error here
                        Err(rocket)
                    }
                }
            }))
            // Routes that don't require configuration
            .mount(
                "/",
                routes![
                    routes::index_get,
                    routes::page_get,
                    routes::page_post,
                    routes::user_post,
                    routes::login_post,
                    routes::logout_post
                ],
            )
            // Up, up, and away!!
            .launch();
    }
}

/// A container for all of the objects that will be passed to Rocket during
/// init. These are kept together so that they can all be created in one
/// function to consolidate failures.
struct InitObject {
    db: Database,
    views: Views,
    roles_jwt: JwtHandler<UserRolesToken>,
    static_files: StaticFiles,
}

impl InitObject {
    /// Create all the components needed for initialization. First,all necessary
    /// data is loaded from the Rocket config. If any data is missing or the
    /// incorrect type, this will fail. Then, each required object is
    /// constructed.
    ///
    /// # Errors
    /// * If the Rocket config is missing key(s) or has incorrect type(s)
    /// * If an error occurs during any object initialization
    fn init(parker: Parker, cfg: &RocketConfig) -> Result<Self> {
        let parker_cfg = ParkerConfig::try_from(cfg)?;
        let db = Database::try_from(&parker_cfg)?;
        let views = Views::new(db.clone(), parker.views);
        let roles_jwt = JwtHandler::try_from(&parker_cfg)?;
        Ok(Self {
            db,
            views,
            roles_jwt,
            static_files: StaticFiles::from(&parker_cfg.static_dir),
        })
    }

    /// Adds all components in this struct to the given Rocket instance.
    fn apply(self, rocket: Rocket) -> Rocket {
        rocket
            .manage(self.db)
            .manage(self.views)
            .manage(self.roles_jwt)
            .mount("/static", self.static_files)
    }
}