-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
128 lines (106 loc) · 3.59 KB
/
Copy pathmain.rs
File metadata and controls
128 lines (106 loc) · 3.59 KB
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
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate rocket;
extern crate api;
use api::iam;
use rocket::fairing::AdHoc;
use std::env;
use api::controllers::auth;
use api::controllers::channels;
use api::controllers::common::*;
use api::controllers::lists;
use api::controllers::settings;
use api::controllers::subscriptions;
use api::controllers::updates;
use lib_messaging::sendgrid;
#[catch(500)]
fn internal_error() -> JsonResponse {
JsonResponse::InternalServerError
}
#[catch(429)]
fn too_many_requests() -> JsonResponse {
JsonResponse::TooManyRequests
}
#[catch(422)]
fn unprocessable_entity() -> JsonResponse {
JsonResponse::TooManyRequests
}
#[catch(404)]
fn not_found() -> JsonResponse {
JsonResponse::NotFound
}
#[catch(403)]
fn forbidden() -> JsonResponse {
JsonResponse::Forbidden
}
#[catch(401)]
fn unauthorized() -> JsonResponse {
JsonResponse::Unauthorized
}
#[catch(400)]
fn bad_request() -> JsonResponse {
JsonResponse::BadRequest("I don't understand what you want".to_owned())
}
fn main() -> Result<(), String> {
let github_identity_provider = AdHoc::on_attach("Github Identity Provider", |rocket| {
let github = iam::github::Github::from_rocket_config(rocket.config())
.expect("Failed to read github config");
Ok(rocket.manage(github))
});
let facebook_identity_provider = AdHoc::on_attach("Facebook Identity Provider", |rocket| {
let facebook = iam::facebook::Facebook::from_rocket_config(rocket.config())
.expect("Failed to read facebook config");
Ok(rocket.manage(facebook))
});
let github_api_token = AdHoc::on_attach("Github Api Token", |rocket| {
let name = "GITHUB_API_TOKEN";
let api_token =
env::var(name).unwrap_or_else(|_| panic!("Failed to read env variable {}", name));
Ok(rocket.manage(channels::GithubApiToken(api_token)))
});
let sendgrid_api_key = AdHoc::on_attach("Sendgrid Api Key", |rocket| {
let name = "SENDGRID_API_KEY";
let api_key =
env::var(name).unwrap_or_else(|_| panic!("Failed to read env variable {}", name));
Ok(rocket.manage(sendgrid::SendgridCredentials { api_key }))
});
let twitter_tokens = AdHoc::on_attach("Twitter Tokens", |rocket| {
let read_env = |name: &'static str| {
env::var(name).unwrap_or_else(|_| panic!("Failed to read env variable {}", name))
};
Ok(rocket.manage(channels::TwitterTokens {
api_key: read_env("TWITTER_API_KEY"),
api_secret_key: read_env("TWITTER_API_SECRET_KEY"),
access_token: read_env("TWITTER_ACCESS_TOKEN"),
access_token_secret: read_env("TWITTER_ACCESS_TOKEN_SECRET"),
}))
});
let mut rocket = rocket::ignite();
let cors_fairing = cors_fairing(rocket.config())?;
rocket = auth::mount(rocket);
rocket = subscriptions::mount(rocket);
rocket = lists::mount(rocket);
rocket = channels::mount(rocket);
rocket = settings::mount(rocket);
rocket = updates::mount(rocket);
rocket
.attach(DigesterDbConn::fairing())
.attach(Redis::fairing())
.attach(cors_fairing)
.attach(github_identity_provider)
.attach(facebook_identity_provider)
.attach(github_api_token)
.attach(twitter_tokens)
.attach(sendgrid_api_key)
.register(catchers![
internal_error,
not_found,
unauthorized,
forbidden,
too_many_requests,
unprocessable_entity,
bad_request
])
.launch();
Ok(())
}