forked from TheSoftwareDevGuild/TheGuildGenesis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.rs
More file actions
109 lines (96 loc) · 3.74 KB
/
Copy pathapi.rs
File metadata and controls
109 lines (96 loc) · 3.74 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
use std::sync::Arc;
use crate::domain::repositories::ProfileRepository;
use crate::domain::services::auth_service::AuthService;
use crate::infrastructure::{
repositories::PostgresProfileRepository,
services::ethereum_address_verification_service::EthereumAddressVerificationService,
};
use axum::middleware::{from_fn, from_fn_with_state};
use axum::{
extract::DefaultBodyLimit,
http::Method,
routing::{delete, get, post, put},
Router,
};
use tower::ServiceBuilder;
use tower_http::{
cors::{Any, CorsLayer},
trace::TraceLayer,
};
use super::handlers::{
create_profile_handler, delete_profile_handler, get_all_profiles_handler, get_nonce_handler,
get_profile_handler, update_profile_handler,
};
use super::middlewares::{eth_auth_layer, test_auth_layer};
pub async fn create_app(pool: sqlx::PgPool) -> Router {
let profile_repository = Arc::from(PostgresProfileRepository::new(pool));
let auth_service = EthereumAddressVerificationService::new(profile_repository.clone());
let state: AppState = AppState {
profile_repository,
auth_service: Arc::from(auth_service),
};
let protected_routes = Router::new()
.route("/profiles", post(create_profile_handler))
.route("/profiles/", post(create_profile_handler))
.route("/profiles/:address", put(update_profile_handler))
.route("/profiles/:address", delete(delete_profile_handler))
.with_state(state.clone());
let protected_with_auth = if std::env::var("TEST_MODE").is_ok() {
protected_routes.layer(from_fn(test_auth_layer))
} else {
protected_routes.layer(from_fn_with_state(state.clone(), eth_auth_layer))
};
let public_routes = Router::new()
.route("/profiles/:address", get(get_profile_handler))
.route("/profiles", get(get_all_profiles_handler))
.route("/auth/nonce/:address", get(get_nonce_handler))
.with_state(state.clone());
Router::new()
.merge(protected_with_auth)
.merge(public_routes)
.with_state(state.clone())
.layer(
ServiceBuilder::new()
.layer(TraceLayer::new_for_http())
.layer(
CorsLayer::new()
.allow_origin(Any)
.allow_methods([Method::GET, Method::POST, Method::PUT, Method::DELETE])
.allow_headers(Any),
)
.layer(DefaultBodyLimit::max(1024 * 1024)),
)
}
#[derive(Clone)]
pub struct AppState {
pub profile_repository: Arc<dyn ProfileRepository>,
pub auth_service: Arc<dyn AuthService>,
}
pub fn test_api(state: AppState) -> Router {
let protected_routes = Router::new()
.route("/profiles", post(create_profile_handler))
.route("/profiles/:address", put(update_profile_handler))
.route("/profiles/:address", delete(delete_profile_handler))
.with_state(state.clone())
.layer(from_fn(test_auth_layer));
let public_routes = Router::new()
.route("/profiles/:address", get(get_profile_handler))
.route("/profiles", get(get_all_profiles_handler))
.route("/auth/nonce/:address", get(get_nonce_handler))
.with_state(state.clone());
Router::new()
.merge(protected_routes)
.merge(public_routes)
.with_state(state.clone())
.layer(
ServiceBuilder::new()
.layer(TraceLayer::new_for_http())
.layer(
CorsLayer::new()
.allow_origin(Any)
.allow_methods([Method::GET, Method::POST, Method::PUT, Method::DELETE])
.allow_headers(Any),
)
.layer(DefaultBodyLimit::max(1024 * 1024)),
)
}