feat: update API endpoints and enhance feed retrieval logic, add CORS support
This commit is contained in:
@@ -3,4 +3,4 @@ PORT=8000
|
|||||||
#DATABASE_URL="sqlite://dev.db"
|
#DATABASE_URL="sqlite://dev.db"
|
||||||
DATABASE_URL="postgresql://postgres:postgres@localhost/thoughts"
|
DATABASE_URL="postgresql://postgres:postgres@localhost/thoughts"
|
||||||
#DATABASE_URL=postgres://thoughts_user:postgres@database:5432/thoughts_db
|
#DATABASE_URL=postgres://thoughts_user:postgres@database:5432/thoughts_db
|
||||||
PREFORK=1
|
PREFORK=0
|
||||||
|
@@ -14,8 +14,8 @@ readme = "README.md"
|
|||||||
members = ["api", "app", "doc", "models", "migration", "utils"]
|
members = ["api", "app", "doc", "models", "migration", "utils"]
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
axum = { version = "0.8.4", default-features = false }
|
|
||||||
tower = { version = "0.5.2", default-features = false }
|
tower = { version = "0.5.2", default-features = false }
|
||||||
|
axum = { version = "0.8.4", default-features = false }
|
||||||
sea-orm = { version = "1.1.12" }
|
sea-orm = { version = "1.1.12" }
|
||||||
sea-query = { version = "0.32.6" } # Added sea-query dependency
|
sea-query = { version = "0.32.6" } # Added sea-query dependency
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
serde = { version = "1.0.219", features = ["derive"] }
|
||||||
|
@@ -15,7 +15,7 @@ tower = { workspace = true }
|
|||||||
tracing = { workspace = true }
|
tracing = { workspace = true }
|
||||||
validator = { workspace = true, features = ["derive"] }
|
validator = { workspace = true, features = ["derive"] }
|
||||||
|
|
||||||
tower-http = { version = "0.6.6", features = ["fs"] }
|
tower-http = { version = "0.6.6", features = ["fs", "cors"] }
|
||||||
tower-cookies = "0.11.0"
|
tower-cookies = "0.11.0"
|
||||||
anyhow = "1.0.98"
|
anyhow = "1.0.98"
|
||||||
dotenvy = "0.15.7"
|
dotenvy = "0.15.7"
|
||||||
|
@@ -10,7 +10,7 @@ use crate::{error::ApiError, extractor::AuthUser};
|
|||||||
|
|
||||||
#[utoipa::path(
|
#[utoipa::path(
|
||||||
get,
|
get,
|
||||||
path = "/feed",
|
path = "/",
|
||||||
responses(
|
responses(
|
||||||
(status = 200, description = "Authenticated user's feed", body = ThoughtListSchema)
|
(status = 200, description = "Authenticated user's feed", body = ThoughtListSchema)
|
||||||
),
|
),
|
||||||
@@ -24,7 +24,10 @@ async fn feed_get(
|
|||||||
auth_user: AuthUser,
|
auth_user: AuthUser,
|
||||||
) -> Result<impl IntoResponse, ApiError> {
|
) -> Result<impl IntoResponse, ApiError> {
|
||||||
let followed_ids = get_followed_ids(&state.conn, auth_user.id).await?;
|
let followed_ids = get_followed_ids(&state.conn, auth_user.id).await?;
|
||||||
let thoughts_with_authors = get_feed_for_user(&state.conn, followed_ids).await?;
|
let mut thoughts_with_authors = get_feed_for_user(&state.conn, followed_ids).await?;
|
||||||
|
|
||||||
|
let own_thoughts = get_feed_for_user(&state.conn, vec![auth_user.id]).await?;
|
||||||
|
thoughts_with_authors.extend(own_thoughts);
|
||||||
|
|
||||||
let thoughts_schema: Vec<ThoughtSchema> = thoughts_with_authors
|
let thoughts_schema: Vec<ThoughtSchema> = thoughts_with_authors
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
@@ -7,15 +7,19 @@ pub mod user;
|
|||||||
|
|
||||||
use app::state::AppState;
|
use app::state::AppState;
|
||||||
use root::create_root_router;
|
use root::create_root_router;
|
||||||
|
use tower_http::cors::CorsLayer;
|
||||||
use user::create_user_router;
|
use user::create_user_router;
|
||||||
|
|
||||||
use crate::routers::{feed::create_feed_router, thought::create_thought_router};
|
use crate::routers::{feed::create_feed_router, thought::create_thought_router};
|
||||||
|
|
||||||
pub fn create_router(state: AppState) -> Router {
|
pub fn create_router(state: AppState) -> Router {
|
||||||
|
let cors = CorsLayer::permissive();
|
||||||
|
|
||||||
Router::new()
|
Router::new()
|
||||||
.merge(create_root_router())
|
.merge(create_root_router())
|
||||||
.nest("/users", create_user_router())
|
.nest("/users", create_user_router())
|
||||||
.nest("/thoughts", create_thought_router())
|
.nest("/thoughts", create_thought_router())
|
||||||
.nest("/feed", create_feed_router())
|
.nest("/feed", create_feed_router())
|
||||||
.with_state(state)
|
.with_state(state)
|
||||||
|
.layer(cors)
|
||||||
}
|
}
|
||||||
|
@@ -23,7 +23,7 @@ export default function Home() {
|
|||||||
const fetchFeed = async () => {
|
const fetchFeed = async () => {
|
||||||
try {
|
try {
|
||||||
setError(null);
|
setError(null);
|
||||||
const response = await fetch("http://localhost:8000/api/feed");
|
const response = await fetch("http://localhost:8000/feed");
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`HTTP error! status: ${response.status}`);
|
throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
}
|
}
|
||||||
@@ -51,7 +51,7 @@ export default function Home() {
|
|||||||
if (!newThoughtContent.trim()) return; // Prevent empty posts
|
if (!newThoughtContent.trim()) return; // Prevent empty posts
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch("http://localhost:8000/api/thoughts", {
|
const response = await fetch("http://localhost:8000/thoughts", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
|
Reference in New Issue
Block a user