-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathapp.rs
More file actions
442 lines (414 loc) · 15 KB
/
app.rs
File metadata and controls
442 lines (414 loc) · 15 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
use crate::handler::answer::AnswerHandler;
use crate::handler::application::ApplicationHandler;
use crate::handler::auth::{google_callback, google_auth_init, DevLoginHandler};
use crate::handler::campaign::CampaignHandler;
use crate::handler::email_template::EmailTemplateHandler;
use crate::handler::offer::OfferHandler;
use crate::handler::organisation::OrganisationHandler;
use crate::handler::question::QuestionHandler;
use crate::handler::rating::RatingHandler;
use crate::handler::role::RoleHandler;
use crate::handler::invite::InviteHandler;
use crate::handler::user::UserHandler;
use crate::models::email::{ChaosEmail, EmailCredentials};
use crate::models::error::ChaosError;
use crate::models::storage::Storage;
use axum::routing::{delete, get, patch, post};
use axum::{Json, Router};
use jsonwebtoken::{Algorithm, DecodingKey, EncodingKey, Header, Validation};
use reqwest::Client as ReqwestClient;
use s3::Bucket;
use snowflake::SnowflakeIdGenerator;
use sqlx::postgres::PgPoolOptions;
use sqlx::{Pool, Postgres};
use std::env;
use axum::http::{header, Method, StatusCode};
use axum::response::IntoResponse;
use oauth2::basic::BasicClient;
use serde::Serialize;
use tower_http::cors::CorsLayer;
use crate::service::oauth2::build_oauth_client;
#[derive(Serialize)]
pub enum AppMessage<T: Serialize> {
OkMessage(T),
BadRequestMessage(T),
NotFoundMessage(T),
ErrorMessage(T),
NotLoggedInMessage(T),
UnauthorizedMessage(T),
}
#[derive(Serialize)]
pub struct MessageWrapper<T: Serialize> {
message: T,
}
#[derive(Serialize)]
pub struct ErrorMessageWrapper<T: Serialize> {
error: T,
}
impl<T: Serialize> IntoResponse for AppMessage<T> {
fn into_response(self) -> axum::response::Response {
match self {
Self::OkMessage(value) => {
(StatusCode::OK, Json(MessageWrapper { message: value })).into_response()
}
Self::BadRequestMessage(value) => (
StatusCode::BAD_REQUEST,
Json(ErrorMessageWrapper { error: value }),
)
.into_response(),
Self::NotFoundMessage(value) => (
StatusCode::NOT_FOUND,
Json(ErrorMessageWrapper { error: value }),
)
.into_response(),
Self::ErrorMessage(value) => (
StatusCode::INTERNAL_SERVER_ERROR,
Json(ErrorMessageWrapper { error: value }),
)
.into_response(),
Self::NotLoggedInMessage(value) => (
StatusCode::UNAUTHORIZED,
Json(ErrorMessageWrapper { error: value }),
)
.into_response(),
Self::UnauthorizedMessage(value) => (
StatusCode::FORBIDDEN,
Json(ErrorMessageWrapper { error: value }),
)
.into_response(),
}
}
}
#[derive(Serialize)]
pub struct IdMessage {
#[serde(serialize_with = "crate::models::serde_string::serialize")]
pub id: i64,
}
#[derive(Clone)]
pub struct AppState {
pub db: Pool<Postgres>,
pub ctx: ReqwestClient,
pub oauth2_client: BasicClient,
pub decoding_key: DecodingKey,
pub encoding_key: EncodingKey,
pub jwt_header: Header,
pub jwt_validator: Validation,
pub snowflake_generator: SnowflakeIdGenerator,
pub storage_bucket: Bucket,
pub is_dev_env: bool,
pub email_credentials: EmailCredentials,
}
pub async fn init_app_state() -> AppState {
// Initialise DB connection
let db_url = env::var("DATABASE_URL")
.expect("Error getting DATABASE_URL")
.to_string();
let pool = PgPoolOptions::new()
.max_connections(5)
.connect(db_url.as_str())
.await
.expect("Cannot connect to database");
// Initialise JWT settings
let jwt_secret = env::var("JWT_SECRET")
.expect("Error getting JWT_SECRET")
.to_string();
// let jwt_secret = "I want to cry";
let encoding_key = EncodingKey::from_secret(jwt_secret.as_bytes());
let decoding_key = DecodingKey::from_secret(jwt_secret.as_bytes());
let jwt_header = Header::new(Algorithm::HS512);
let mut jwt_validator = Validation::new(Algorithm::HS512);
jwt_validator.set_issuer(&["Chaos"]);
jwt_validator.set_audience(&["chaos.devsoc.app"]);
// Initialise reqwest client
let ctx = reqwest::Client::new();
// Initialise oauth2 client
let client_id = env::var("GOOGLE_CLIENT_ID")
.expect("Error getting GOOGLE_CLIENT_ID")
.to_string();
let client_secret = env::var("GOOGLE_CLIENT_SECRET")
.expect("Error getting GOOGLE_CLIENT_SECRET")
.to_string();
let oauth2_client = build_oauth_client(client_id, client_secret);
let dev_env = env::var("DEV_ENV")
.expect("Error getting DEV_ENV")
.to_string();
let mut is_dev_env = false;
if dev_env == "dev" {
is_dev_env = true;
}
// Initialise Snowflake Generator
let snowflake_generator = SnowflakeIdGenerator::new(1, 1);
// Initialise S3 bucket
let storage_bucket = Storage::init_bucket();
// Initialise email credentials
let email_credentials = ChaosEmail::setup_credentials();
// Add all data to AppState
let state = AppState {
db: pool,
ctx,
oauth2_client,
encoding_key,
decoding_key,
jwt_header,
jwt_validator,
snowflake_generator,
storage_bucket,
is_dev_env,
email_credentials,
};
state
}
pub async fn app() -> Result<Router, ChaosError> {
let state = init_app_state().await;
let cors = CorsLayer::new()
.allow_methods([Method::GET, Method::POST, Method::DELETE, Method::PUT, Method::PATCH])
.allow_headers([header::ACCEPT, header::COOKIE, header::SET_COOKIE, header::CONTENT_TYPE])
.allow_credentials(true)
.allow_origin([
"http://localhost".parse().unwrap(),
"http://localhost:3000".parse().unwrap(),
"https://chaos.devsoc.app".parse().unwrap(),
"http://chaos.devsoc.app".parse().unwrap(),
"https://chaosstaging.devsoc.app".parse().unwrap(),
"http://chaosstaging.devsoc.app".parse().unwrap(),
]);
Ok(Router::new()
.route("/", get(|| async { "Join DevSoc! https://devsoc.app/" }))
.route("/auth/google", get(google_auth_init))
.route("/api/auth/callback/google", get(google_callback))
.route("/api/v1/dev/super_admin_login", get(DevLoginHandler::dev_super_admin_login))
.route("/api/v1/dev/org_admin_login", get(DevLoginHandler::dev_org_admin_login))
.route("/api/v1/dev/user_login", get(DevLoginHandler::dev_user_login))
.route("/api/v1/user", get(UserHandler::get))
.route("/api/v1/user/name", patch(UserHandler::update_name))
.route("/api/v1/user/pronouns", patch(UserHandler::update_pronouns))
.route("/api/v1/user/gender", patch(UserHandler::update_gender))
.route("/api/v1/user/zid", patch(UserHandler::update_zid))
.route("/api/v1/user/degree", patch(UserHandler::update_degree))
.route(
"/api/v1/user/applications",
get(ApplicationHandler::get_from_curr_user),
)
.route("/api/v1/user/organisations", get(OrganisationHandler::get_all_for_user))
.route("/api/v1/organisation", post(OrganisationHandler::create))
.route(
"/api/v1/organisation/slug_check",
post(OrganisationHandler::check_organisation_slug_availability),
)
.route(
"/api/v1/organisation/:organisation_id",
get(OrganisationHandler::get).delete(OrganisationHandler::delete),
)
.route("/api/v1/organisation/:organisation_id/role", get(OrganisationHandler::get_user_role))
.route(
"/api/v1/organisation/slug/:slug",
get(OrganisationHandler::get_by_slug),
)
.route(
"/api/v1/organisation/:organisation_id/campaign",
post(OrganisationHandler::create_campaign),
)
.route(
"/api/v1/organisation/:organisation_id/campaign/slug_check",
post(OrganisationHandler::check_campaign_slug_availability),
)
.route(
"/api/v1/organisation/:organisation_id/campaigns",
get(OrganisationHandler::get_campaigns),
)
.route(
"/api/v1/organisation/:organisation_id/email_template",
post(OrganisationHandler::create_email_template),
)
.route(
"/api/v1/organisation/:organisation_id/email_templates",
get(OrganisationHandler::get_all_email_templates),
)
.route(
"/api/v1/organisation/:organisation_id/logo",
patch(OrganisationHandler::update_logo),
)
.route(
"/api/v1/organisation/:organisation_id/members",
get(OrganisationHandler::get_members)
.put(OrganisationHandler::update_members)
)
.route(
"/api/v1/organisation/:organisation_id/user",
post(OrganisationHandler::invite_user).delete(OrganisationHandler::remove_user),
)
.route(
"/api/v1/organisation/:organisation_id/admins",
get(OrganisationHandler::get_admins)
.put(OrganisationHandler::update_admins)
)
.route(
"/api/v1/organisation/:organisation_id/admin",
delete(OrganisationHandler::remove_admin),
)
.route(
"/api/v1/organisation/:organisation_id/users",
get(OrganisationHandler::get_users)
)
.route(
"/api/v1/rating/:rating_id",
get(RatingHandler::get)
.delete(RatingHandler::delete)
.put(RatingHandler::update),
)
.route(
"/api/v1/application/:application_id/rating",
get(ApplicationHandler::get_rating_by_current_user)
.post(ApplicationHandler::create_rating)
.put(ApplicationHandler::update_rating),
)
.route(
"/api/v1/application/:application_id/ratings",
get(ApplicationHandler::get_ratings),
)
.route(
"/api/v1/campaign/:campaign_id/role",
post(CampaignHandler::create_role),
)
.route(
"/api/v1/campaign/:campaign_id/role/:role_id/questions",
get(QuestionHandler::get_all_by_campaign_and_role),
)
.route(
"/api/v1/campaign/:campaign_id/roles",
get(CampaignHandler::get_roles),
)
.route(
"/api/v1/campaign/:campaign_id/applications",
get(CampaignHandler::get_applications),
)
.route(
"/api/v1/role/:role_id",
get(RoleHandler::get)
.patch(RoleHandler::update)
.delete(RoleHandler::delete),
)
.route(
"/api/v1/role/:role_id/applications",
get(RoleHandler::get_applications),
)
.route(
"/api/v1/campaign/:campaign_id",
get(CampaignHandler::get)
.put(CampaignHandler::update)
.delete(CampaignHandler::delete),
)
.route(
"/api/v1/campaign/:campaign_id/publish",
patch(CampaignHandler::publish),
)
.route(
"/api/v1/organisation/slug/:organisation_slug/campaign/slug/:campaign_slug",
get(CampaignHandler::get_by_slugs),
)
.route("/api/v1/campaigns", get(CampaignHandler::get_all))
.route(
"/api/v1/campaign/:campaign_id/apply",
post(ApplicationHandler::create_or_get),
)
.route(
"/api/v1/campaign/:campaign_id/application/exists",
get(ApplicationHandler::check_application_exists),
)
.route(
"/api/v1/campaign/:campaign_id/question",
post(QuestionHandler::create),
)
.route(
"/api/v1/campaign/:campaign_id/question/:id",
patch(QuestionHandler::update).delete(QuestionHandler::delete),
)
.route(
"/api/v1/campaign/:campaign_id/questions/common",
get(QuestionHandler::get_all_common_by_campaign),
)
.route(
"/api/v1/campaign/:campaign_id/banner",
patch(CampaignHandler::update_banner),
)
.route(
"/api/v1/campaign/:campaign_id/application",
post(CampaignHandler::create_application),
)
.route(
"/api/v1/campaign/:campaign_id/offer",
post(CampaignHandler::create_offer),
)
.route(
"/api/v1/campaign/:campaign_id/offers",
get(CampaignHandler::get_offers),
)
.route(
"/api/v1/application/:application_id",
get(ApplicationHandler::get),
)
.route(
"/api/v1/application/:application_id/status",
patch(ApplicationHandler::set_status),
)
.route(
"/api/v1/application/:application_id/private",
patch(ApplicationHandler::set_private_status),
)
.route(
"/api/v1/application/:application_id/answers/common",
get(AnswerHandler::get_all_common_by_application),
)
.route(
"/api/v1/application/:application_id/answer",
post(AnswerHandler::create),
)
.route(
"/api/v1/application/:application_id/role/:role_id/answers",
get(AnswerHandler::get_all_by_application_and_role),
)
.route(
"/api/v1/application/:application_id/roles",
get(ApplicationHandler::get_roles).patch(ApplicationHandler::update_roles)
)
.route(
"/api/v1/application/:application_id/submit",
post(ApplicationHandler::submit)
)
.route(
"/api/v1/answer/:answer_id",
patch(AnswerHandler::update).delete(AnswerHandler::delete),
)
.route(
"/api/v1/email_template/:template_id",
get(EmailTemplateHandler::get)
.patch(EmailTemplateHandler::update)
.delete(EmailTemplateHandler::delete),
)
.route(
"/api/v1/email_template/:template_id/duplicate",
post(EmailTemplateHandler::duplicate)
)
.route(
"/api/v1/offer/:offer_id",
get(OfferHandler::get)
.delete(OfferHandler::delete)
.post(OfferHandler::reply),
)
.route(
"/api/v1/offer/:offer_id/preview",
get(OfferHandler::preview_email),
)
.route(
"/api/v1/offer/:offer_id/send",
post(OfferHandler::send_offer),
)
// Invite routes
// - GET /api/v1/invite/:code -> invite details
// - POST /api/v1/invite/:code -> accept invite
.route(
"/api/v1/invite/:code", get(InviteHandler::get).post(InviteHandler::use_invite)
)
.layer(cors)
.with_state(state))
}