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
#[cfg(all(feature = "backend_actix-web", feature = "backend_poem"))]
compile_error!("feature \"backend_actix-web\" and feature \"backend_poem\" cannot be enabled at the same time");

// #[cfg(not(any(feature = "backend_poem", feature = "backend_actix-web")))]
// compile_error!("Please enable one of the backend features (options: 'backend_actix-web', 'backend-poem')");

mod util;
pub use util::*;

#[macro_use]
extern crate diesel;

#[cfg(feature = "plugin_auth")]
pub mod auth;

#[cfg(all(feature = "plugin_dev", debug_assertions))]
pub mod dev;

mod database;
pub use database::{Database, Pool, Connection};

#[cfg(feature = "backend_poem")]
mod logger;
#[cfg(feature = "backend_poem")]
pub use logger::Logger as PoemLogger;

#[cfg(feature = "plugin_storage")]
mod storage;
#[cfg(feature = "plugin_storage")]
pub use storage::{Storage, Attachment, AttachmentData, AttachmentBlob};

mod mailer;
pub use mailer::Mailer;

// #[cfg(debug_assertions)]
// #[macro_use]
// extern crate diesel_migrations;

#[derive(Clone)]
pub struct AppData {
    pub mailer: Mailer,
    pub database: Database,
    #[cfg(feature= "plugin_storage")]
    pub storage: storage::Storage
}

pub fn setup() -> AppData {
    // Only load dotenv in development
    #[cfg(debug_assertions)]
    {
        if dotenv::dotenv().is_err() {
            panic!("ERROR: Could not load environment variables from dotenv file");
        }

        #[cfg(feature = "backend_actix-web")]
        env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
        // diesel_migrations::embed_migrations!();
    }

    #[cfg(feature="plugin_auth")]
    if std::env::var("SECRET_KEY").is_err() {
        panic!("No SECRET_KEY environment variable set!");
    }

    if std::env::var("DATABASE_URL").is_err() {
        panic!("No DATABASE_URL environment variable set!");
    }

    Mailer::check_environment_variables();

    AppData {
        mailer: Mailer::new(),
        database: Database::new(),
        #[cfg(feature= "plugin_storage")]
        storage: storage::Storage::new()
    }
}

#[cfg(feature = "backend_poem")]
use poem;

#[cfg(feature = "backend_poem")]
pub async fn not_found(_: poem::error::NotFoundError) -> poem::Response {
    let json = serde_json::json!({
        "success": false,
        "message": "Invalid endpoint"
    });

    poem::Response::builder()
        .status(poem::http::StatusCode::NOT_FOUND)
        .header("Content-Type", "application/json")
        .body(json.to_string())
}