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
141
142
143
144
145
146
use std::env;
use std::error::Error;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use conduit::{Request, Response};
use conduit_middleware::Middleware;
use git2;
use oauth2;
use r2d2;
use curl::easy::Easy;
use {db, Config};
#[allow(missing_debug_implementations)]
pub struct App {
pub diesel_database: db::DieselPool,
pub github: oauth2::Config,
pub session_key: String,
pub git_repo: Mutex<git2::Repository>,
pub git_repo_checkout: PathBuf,
pub config: Config,
}
#[allow(missing_debug_implementations)]
pub struct AppMiddleware {
app: Arc<App>,
}
impl App {
pub fn new(config: &Config) -> App {
let mut github = oauth2::Config::new(
&config.gh_client_id,
&config.gh_client_secret,
"https://github.com/login/oauth/authorize",
"https://github.com/login/oauth/access_token",
);
github.scopes.push(String::from("read:org"));
let db_pool_size = match (env::var("DB_POOL_SIZE"), config.env) {
(Ok(num), _) => num.parse().expect("couldn't parse DB_POOL_SIZE"),
(_, ::Env::Production) => 10,
_ => 1,
};
let db_min_idle = match (env::var("DB_MIN_IDLE"), config.env) {
(Ok(num), _) => Some(num.parse().expect("couldn't parse DB_MIN_IDLE")),
(_, ::Env::Production) => Some(5),
_ => None,
};
let db_helper_threads = match (env::var("DB_HELPER_THREADS"), config.env) {
(Ok(num), _) => num.parse().expect("couldn't parse DB_HELPER_THREADS"),
(_, ::Env::Production) => 3,
_ => 1,
};
let diesel_db_config = r2d2::Config::builder()
.pool_size(db_pool_size)
.min_idle(db_min_idle)
.helper_threads(db_helper_threads)
.build();
let repo = git2::Repository::open(&config.git_repo_checkout).unwrap();
App {
diesel_database: db::diesel_pool(&config.db_url, diesel_db_config),
github: github,
session_key: config.session_key.clone(),
git_repo: Mutex::new(repo),
git_repo_checkout: config.git_repo_checkout.clone(),
config: config.clone(),
}
}
pub fn handle(&self) -> Easy {
let mut handle = Easy::new();
if let Some(proxy) = self.config.uploader.proxy() {
handle.proxy(proxy).unwrap();
}
handle
}
}
impl AppMiddleware {
pub fn new(app: Arc<App>) -> AppMiddleware {
AppMiddleware { app: app }
}
}
impl Middleware for AppMiddleware {
fn before(&self, req: &mut Request) -> Result<(), Box<Error + Send>> {
req.mut_extensions().insert(self.app.clone());
Ok(())
}
fn after(
&self,
req: &mut Request,
res: Result<Response, Box<Error + Send>>,
) -> Result<Response, Box<Error + Send>> {
req.mut_extensions().pop::<Arc<App>>().unwrap();
res
}
}
pub trait RequestApp {
fn app(&self) -> &Arc<App>;
}
impl<T: Request + ?Sized> RequestApp for T {
fn app(&self) -> &Arc<App> {
self.extensions().find::<Arc<App>>().expect("Missing app")
}
}