-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.rs
146 lines (138 loc) · 4.9 KB
/
routes.rs
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 axum::{
middleware::{self},
routing::{get, post},
Extension, Json, Router,
};
use serde_json::json;
use sqlx::{Pool, Postgres};
use tower_http::services::ServeDir;
use utoipa::{
openapi::security::{ApiKey, ApiKeyValue, SecurityScheme},
Modify, OpenApi,
};
use utoipa_rapidoc::RapiDoc;
// use utoipa_redoc::{Redoc, Servable}; // Uncomment to enable Redoc
use utoipa_swagger_ui::SwaggerUi;
use rust_todo_api::{
auth::{self, models::JWT_SECRET},
common,
common::middlewares::{auth_middleware, AuthState},
todo, user, web,
};
pub fn build_routes(pool: Pool<Postgres>) -> Router {
#[derive(OpenApi)]
#[openapi(
paths(
auth::handlers::get_tokens,
user::handlers::register_user,
user::handlers::find_user_by_email,
todo::handlers::create_todo,
todo::handlers::find_todos,
todo::handlers::find_todo_by_id,
todo::handlers::edit_todo_by_id,
todo::handlers::delete_todo_by_id,
),
components(
schemas(
common::errors::ApiError,
common::pagination::PaginatedTodoView,
auth::views::LoginRequest,
auth::views::TokenView,
user::views::NewUserRequest,
user::views::UserView,
todo::views::TodoView,
todo::views::NewTodoRequest,
todo::views::EditTodoRequest,
)
),
info(
title = "Todo API",
description = "A simple todo API",
version = "0.1.0",
contact(
name = "SJ",
url = "https://github.com/litsynp"
),
license(
name = "MIT",
url = "https://opensource.org/licenses/MIT",
)
),
modifiers(&SecurityAddon),
tags(
(name = "auth", description = "Authentication API"),
(name = "user", description = "User API"),
(name = "todo", description = "Todo API")
)
)]
struct ApiDoc;
struct SecurityAddon;
impl Modify for SecurityAddon {
fn modify(&self, openapi: &mut utoipa::openapi::OpenApi) {
if let Some(components) = openapi.components.as_mut() {
components.add_security_scheme(
"api_key",
SecurityScheme::ApiKey(ApiKey::Header(ApiKeyValue::new("Authorization"))),
)
}
}
}
let auth_state = AuthState {
pool: pool.clone(),
jwt_secret: JWT_SECRET.to_string(),
};
let user_service = user::service::UserService::new(pool.clone());
let todo_service = todo::service::TodoService::new(pool.clone());
let api_routes = Router::new()
.nest(
"/auth",
Router::new().route("/tokens", post(auth::handlers::get_tokens)),
)
.nest(
"/todos",
Router::new()
.route(
"/",
get(todo::handlers::find_todos).post(todo::handlers::create_todo),
)
.route(
"/:id",
get(todo::handlers::find_todo_by_id)
.put(todo::handlers::edit_todo_by_id)
.delete(todo::handlers::delete_todo_by_id),
)
.route_layer(middleware::from_fn_with_state(
auth_state.clone(),
auth_middleware,
)),
)
.nest(
"/users",
Router::new()
.route(
"/",
get(user::handlers::find_user_by_email).route_layer(
middleware::from_fn_with_state(auth_state.clone(), auth_middleware),
),
)
.route("/", post(user::handlers::register_user)),
)
.layer(Extension(user_service))
.layer(Extension(todo_service));
let assets_path = std::env::current_dir().unwrap();
Router::new()
.route("/", get(web::handlers::index))
.route("/login", get(web::handlers::login))
.route("/register", get(web::handlers::register))
.nest_service(
"/assets",
ServeDir::new(format!("{}/assets", assets_path.to_str().unwrap())),
)
.merge(SwaggerUi::new("/swagger-ui").url("/api-docs/openapi.json", ApiDoc::openapi()))
// .merge(Redoc::with_url("/redoc", ApiDoc::openapi())) // Uncomment to enable Redoc
// There is no need to create `RapiDoc::with_openapi` because the OpenApi is served
// via SwaggerUi instead we only make rapidoc to point to the existing doc.
.merge(RapiDoc::new("/api-docs/openapi.json").path("/rapidoc"))
.route("/health", get(|| async { Json(json!({ "status": "ok" })) }))
.nest("/api", api_routes.with_state(auth_state))
}