SquidSpirit c39a800b6b
All checks were successful
Frontend CI / build (push) Successful in 2m18s
BLOG-43 Post related api endpoints (#55)
### Description

- `GET` `/post_info`

  Get all the info of the posts.

  - `200` Without any post

    ```json
    []
    ```

  - `200` With posts

    ```json
    [
        {
            "description": "This is the first post.",
            "id": 1,
            "labels": [
                {
                    "color": "#FF666666",
                    "id": 2,
                    "name": "Rust"
                }
            ],
            "preview_image_url": "https://squidspirit.com/icon/logo-light.svg",
            "published_time": null,
            "title": "The First Post"
        }
    ]
    ```

- `GET` `/post/{id}`

  Get the full post content with the given `id`

  - `200` With result

    ```json
    {
        "content": "Hello! I'm Squid!!",
        "id": 1,
        "info": {
            "description": "This is the first post.",
            "id": 1,
            "labels": [
                {
                    "color": "#FF666666",
                    "id": 2,
                    "name": "Rust"
                }
            ],
            "preview_image_url": "https://squidspirit.com/icon/logo-light.svg",
            "published_time": null,
            "title": "The First Post"
        }
    }
    ```

  - `404` There is no post with the `id`

### Package Changes

```toml
[workspace.package]
version = "0.1.1"
edition = "2024"

[workspace.dependencies]
actix-web = "4.10.2"
async-trait = "0.1.88"
chrono = "0.4.41"
dotenv = "0.15.0"
env_logger = "0.11.8"
futures = "0.3.31"
log = "0.4.27"
serde = { version = "1.0.219", features = ["derive"] }
sqlx = { version = "0.8.5", features = [
    "chrono",
    "macros",
    "postgres",
    "runtime-tokio-rustls",
] }
tokio = { version = "1.45.0", features = ["full"] }
```

### Screenshots

_No response_

### Reference

Resolves #43

### Checklist

- [x] A milestone is set
- [x] The related issuse has been linked to this branch

Reviewed-on: #55
Reviewed-by: zoe <zoe@noreply.localhost>
Co-authored-by: SquidSpirit <squid@squidspirit.com>
Co-committed-by: SquidSpirit <squid@squidspirit.com>
2025-06-07 21:26:10 +08:00

61 lines
1.5 KiB
Rust

use actix_web::{
App, Error, HttpServer,
body::MessageBody,
dev::{ServiceFactory, ServiceRequest, ServiceResponse},
web,
};
use post::framework::web::post_web_routes::configure_post_routes;
use server::container::Container;
use sqlx::{Pool, Postgres, postgres::PgPoolOptions};
use std::{env, sync::Arc};
#[actix_web::main]
async fn main() -> std::io::Result<()> {
dotenv::dotenv().ok();
env_logger::init();
let db_pool = init_database().await;
HttpServer::new(move || create_app(db_pool.clone()))
.bind(("127.0.0.1", 8080))?
.run()
.await
}
async fn init_database() -> Arc<Pool<Postgres>> {
let database_url = env::var("DATABASE_URL")
.unwrap_or_else(|_| "postgres://postgres@localhost:5432/postgres".to_string());
let db_pool = PgPoolOptions::new()
.max_connections(5)
.connect(&database_url)
.await
.expect("Failed to create database connection pool");
sqlx::migrate!("../migrations")
.run(&db_pool)
.await
.expect("Failed to run database migrations");
Arc::new(db_pool)
}
fn create_app(
db_pool: Arc<Pool<Postgres>>,
) -> App<
impl ServiceFactory<
ServiceRequest,
Response = ServiceResponse<impl MessageBody>,
Config = (),
InitError = (),
Error = Error,
>,
> {
let container = Container::new(db_pool.clone());
App::new()
.app_data(web::Data::new(db_pool))
.app_data(web::Data::new(container.post_controller))
.configure(configure_post_routes)
}