Skip to content

Commit 6ffb417

Browse files
committed
feat: Implement initial backend API for tutorials and comments
1 parent 6f532fa commit 6ffb417

4 files changed

Lines changed: 17 additions & 26 deletions

File tree

backend/src/handlers/comments.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use axum::{
2828
};
2929
use std::net::SocketAddr;
3030
use serde::{Deserialize, Serialize};
31-
use std::{env, sync::OnceLock};
31+
3232

3333
#[derive(Deserialize)]
3434
pub struct CreateCommentRequest {

backend/src/handlers/tutorials.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ pub async fn update_tutorial(
554554
&color,
555555
&topics_json,
556556
&topics_vec,
557-
tutorial.version.try_into().unwrap_or(1),
557+
new_version.try_into().unwrap_or(1),
558558
)
559559
.await
560560
.map_err(|e| {

backend/src/main.rs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@ use crate::middleware::{cors, security};
1313
// HTTP-related imports for building the web server
1414
use 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
};
2120
use 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;
3432
use tokio::signal;
3533
use tower_governor::key_extractor::SmartIpKeyExtractor;
3634
use tower_governor::{governor::GovernorConfigBuilder, GovernorLayer};
37-
use tower_http::cors::{AllowHeaders, AllowMethods, AllowOrigin, CorsLayer};
35+
use tower_http::cors::{CorsLayer};
3836
use tower_http::limit::RequestBodyLimitLayer;
3937
use tower_http::services::ServeDir;
4038
use 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))

backend/src/repositories/tutorials.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::db::DbPool;
2-
use crate::models::{CreateTutorialRequest, Tutorial, UpdateTutorialRequest};
2+
use crate::models::Tutorial;
33
use sqlx;
4-
use std::convert::TryInto;
4+
55

66
pub async fn list_tutorials(
77
pool: &DbPool,

0 commit comments

Comments
 (0)