mirror of
https://github.com/Lumither/nocturne.git
synced 2026-10-17 10:26:21 +00:00
feat(api) add refresh.rs
This commit is contained in:
10 files changed
+127
-9
No files matched your search
@@ -5,3 +5,10 @@ CREATE TABLE IF NOT EXISTS Post
|
|||||||
content TEXT NOT NULL,
|
content TEXT NOT NULL,
|
||||||
last_update TIMESTAMP
|
last_update TIMESTAMP
|
||||||
);
|
);
|
||||||
|
|
||||||
|
INSERT INTO Post (post_id, title, content, last_update)
|
||||||
|
VALUES (?1, ?2, ?3, ?4)
|
||||||
|
ON CONFLICT (post_id)
|
||||||
|
DO UPDATE SET title = excluded.title,
|
||||||
|
content = excluded.content,
|
||||||
|
last_update = excluded.last_update
|
||||||
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,2 @@
|
|||||||
|
pub mod get_post;
|
||||||
|
pub mod get_post_list;
|
||||||
@@ -1,2 +1,2 @@
|
|||||||
pub mod get_post;
|
pub mod get;
|
||||||
pub mod get_post_list;
|
pub mod post;
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
pub mod refresh;
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
use std::env;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
use axum::extract::State;
|
||||||
|
use axum::http::StatusCode;
|
||||||
|
use axum::Json;
|
||||||
|
use serde_json::{json, Value};
|
||||||
|
use sqlx::{PgPool, query};
|
||||||
|
|
||||||
|
use crate::model::Post;
|
||||||
|
|
||||||
|
pub async fn refresh(
|
||||||
|
State(db_connection): State<PgPool>,
|
||||||
|
) -> Result<StatusCode, (StatusCode, Json<Value>)> {
|
||||||
|
let data_repo_dir = match env::var("GIT_WORK_DIR") {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(_) => {
|
||||||
|
return Err((
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
Json::from(json!(format!(
|
||||||
|
"{{error: {}}}",
|
||||||
|
"error at parsing environment variable: GIT_WORK_DIR"
|
||||||
|
))),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let md_list = search_md(Path::new(&data_repo_dir));
|
||||||
|
|
||||||
|
let post_list: Vec<Post> = md_list
|
||||||
|
.into_iter()
|
||||||
|
.map(Post::from_path)
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
for post in post_list {
|
||||||
|
match query(
|
||||||
|
r##"
|
||||||
|
INSERT INTO Post (post_id, title, content, last_update)
|
||||||
|
VALUES ($1, $2, $3, $4)
|
||||||
|
ON CONFLICT (post_id)
|
||||||
|
DO UPDATE SET title = excluded.title,
|
||||||
|
content = excluded.content,
|
||||||
|
last_update = excluded.last_update
|
||||||
|
"##,
|
||||||
|
)
|
||||||
|
.bind(post.post_id)
|
||||||
|
.bind(post.title)
|
||||||
|
.bind(post.content)
|
||||||
|
.bind(post.last_update)
|
||||||
|
.execute(&db_connection)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(_) => {
|
||||||
|
dbg!("inserted");
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
return Err((
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
Json::from(json!(format!("{{error: {}}}", e.to_string()))),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(StatusCode::OK)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn search_md(entry: &Path) -> Vec<String> {
|
||||||
|
if entry.ends_with(".git") {
|
||||||
|
return vec![];
|
||||||
|
}
|
||||||
|
if entry.is_dir() {
|
||||||
|
if let Ok(files) = entry.read_dir() {
|
||||||
|
files
|
||||||
|
.flat_map(|file| {
|
||||||
|
let file_path = file.unwrap().path();
|
||||||
|
search_md(file_path.as_path())
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
} else {
|
||||||
|
vec![]
|
||||||
|
}
|
||||||
|
} else if entry.extension().is_some_and(|ext| ext == "md") {
|
||||||
|
vec![entry.to_str().unwrap().to_string()]
|
||||||
|
} else {
|
||||||
|
vec![]
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
-3
@@ -2,12 +2,13 @@ use std::env;
|
|||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
use axum::Router;
|
use axum::Router;
|
||||||
use axum::routing::get;
|
use axum::routing::{get, post};
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
use sqlx::postgres::PgPoolOptions;
|
use sqlx::postgres::PgPoolOptions;
|
||||||
|
|
||||||
use crate::api::get_post::get_post;
|
use crate::api::get::get_post::get_post;
|
||||||
use crate::api::get_post_list::get_post_list;
|
use crate::api::get::get_post_list::get_post_list;
|
||||||
|
use crate::api::post::refresh::refresh;
|
||||||
|
|
||||||
mod api;
|
mod api;
|
||||||
mod model;
|
mod model;
|
||||||
@@ -29,6 +30,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
.expect("failed to load database");
|
.expect("failed to load database");
|
||||||
|
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
|
.route("/api/post/refresh", post(refresh))
|
||||||
.route("/api/get/post_list", get(get_post_list))
|
.route("/api/get/post_list", get(get_post_list))
|
||||||
.route("/api/get/post/:post_id", get(get_post))
|
.route("/api/get/post/:post_id", get(get_post))
|
||||||
.with_state(db_pool);
|
.with_state(db_pool);
|
||||||
|
|||||||
@@ -3,10 +3,12 @@ use serde::{Deserialize, Serialize};
|
|||||||
use sqlx::FromRow;
|
use sqlx::FromRow;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub mod post;
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, FromRow)]
|
#[derive(Debug, Serialize, Deserialize, FromRow)]
|
||||||
pub struct Post {
|
pub struct Post {
|
||||||
post_id: Uuid,
|
pub post_id: Uuid,
|
||||||
title: String,
|
pub title: String,
|
||||||
content: String,
|
pub content: String,
|
||||||
last_update: NaiveDateTime,
|
pub last_update: NaiveDateTime,
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
use chrono::Utc;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::model::Post;
|
||||||
|
|
||||||
|
impl super::Post {
|
||||||
|
pub fn from_path(path: String) -> Self {
|
||||||
|
// todo: remove test
|
||||||
|
Post {
|
||||||
|
post_id: Uuid::new_v4(),
|
||||||
|
title: "test title".to_string(),
|
||||||
|
content: "test content".to_string(),
|
||||||
|
last_update: Utc::now().naive_utc(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user