@@ -13,15 +13,13 @@ use crate::middleware::{cors, security};
1313// HTTP-related imports for building the web server
1414use axum:: http:: {
1515 header:: {
16- AUTHORIZATION , CACHE_CONTROL , CONTENT_SECURITY_POLICY , CONTENT_TYPE , EXPIRES , PRAGMA ,
17- STRICT_TRANSPORT_SECURITY , X_CONTENT_TYPE_OPTIONS , X_FRAME_OPTIONS , ACCEPT ,
16+ AUTHORIZATION , CONTENT_TYPE , ACCEPT ,
1817 } ,
1918 HeaderName , HeaderValue , Method ,
2019} ;
2120use axum:: {
22- extract:: { DefaultBodyLimit , Request } ,
23- middleware:: { from_extractor, from_fn, Next } ,
24- response:: Response ,
21+ extract:: DefaultBodyLimit ,
22+ middleware:: Next ,
2523 routing:: { delete, get, post, put} ,
2624 Router ,
2725} ;
@@ -34,7 +32,7 @@ use std::net::SocketAddr;
3432use tokio:: signal;
3533use tower_governor:: key_extractor:: SmartIpKeyExtractor ;
3634use tower_governor:: { governor:: GovernorConfigBuilder , GovernorLayer } ;
37- use tower_http:: cors:: { AllowHeaders , AllowMethods , AllowOrigin , CorsLayer } ;
35+ use tower_http:: cors:: { CorsLayer } ;
3836use tower_http:: limit:: RequestBodyLimitLayer ;
3937use tower_http:: services:: ServeDir ;
4038use tracing_subscriber;
@@ -250,33 +248,25 @@ async fn main() {
250248 . route_layer ( axum:: middleware:: from_fn_with_state ( pool. clone ( ) , csrf:: enforce_csrf) )
251249 . route_layer ( axum:: middleware:: from_fn_with_state ( pool. clone ( ) , middleware:: auth:: auth_middleware) )
252250 . layer ( RequestBodyLimitLayer :: new ( ADMIN_BODY_LIMIT ) )
253- . layer ( GovernorLayer :: new ( admin_rate_limit_config. clone ( ) ) ) ;
254-
255- // Define the application router with all routes and middleware
256- let mut app = Router :: new ( )
257- . with_state ( pool)
258- // Merge all route modules
259- . merge ( login_router)
251+ . layer ( GovernorLayer :: new ( admin_rate_limit_config. clone ( ) ) )
252+ . with_state ( pool. clone ( ) ) ;
260253
254+ let api_routes = Router :: new ( )
261255 . route ( "/api/auth/me" , get ( handlers:: auth:: me) )
262-
263256 . route ( "/api/tutorials" , get ( handlers:: tutorials:: list_tutorials) )
264257 . route (
265258 "/api/tutorials/{id}" ,
266259 get ( handlers:: tutorials:: get_tutorial) ,
267260 )
268-
269261 . route (
270262 "/api/search/tutorials" ,
271263 get ( handlers:: search:: search_tutorials) ,
272264 )
273265 . route ( "/api/search/topics" , get ( handlers:: search:: get_all_topics) )
274-
275266 . route (
276267 "/api/tutorials/{id}/comments" ,
277268 get ( handlers:: comments:: list_comments) ,
278269 )
279-
280270 . route (
281271 "/api/content" ,
282272 get ( handlers:: site_content:: list_site_content) ,
@@ -285,9 +275,6 @@ async fn main() {
285275 "/api/content/{section}" ,
286276 get ( handlers:: site_content:: get_site_content) ,
287277 )
288-
289- . merge ( admin_routes)
290-
291278 . route (
292279 "/api/posts/{id}/comments" ,
293280 get ( handlers:: comments:: list_post_comments)
@@ -298,7 +285,6 @@ async fn main() {
298285 "/api/comments/{id}/vote" ,
299286 post ( handlers:: comments:: vote_comment) ,
300287 )
301-
302288 . route (
303289 "/api/public/pages/{slug}" ,
304290 get ( handlers:: site_pages:: get_published_page_by_slug) ,
@@ -316,9 +302,14 @@ async fn main() {
316302 get ( handlers:: site_pages:: list_published_page_slugs) ,
317303 )
318304 . nest_service ( "/uploads" , ServeDir :: new ( upload_dir) )
305+ . with_state ( pool. clone ( ) ) ;
319306
307+ // Define the application router with all routes and middleware
308+ let app = Router :: new ( )
309+ . merge ( login_router)
310+ . merge ( admin_routes)
311+ . merge ( api_routes)
320312 . route ( "/api/health" , get ( || async { "OK" } ) )
321-
322313 // Serve index.html with server-side injection for root and fallback
323314 . route ( "/" , get ( handlers:: frontend_proxy:: serve_index) )
324315 . route ( "/{*path}" , get ( handlers:: frontend_proxy:: serve_index) )
0 commit comments