mirror of
https://github.com/Lumither/nocturne.git
synced 2026-10-17 10:26:21 +00:00
feat(backend) backed init
This commit is contained in:
11 files changed
+132
-2
No files matched your search
@@ -1,2 +1,4 @@
|
|||||||
BLOG_ENDPOINT=https://raw.githubusercontent.com/lumither/blog-posts/master
|
BLOG_ENDPOINT=https://raw.githubusercontent.com/lumither/blog-posts/master
|
||||||
; use mirror
|
BACKEND_PORT=3001
|
||||||
|
|
||||||
|
DB_CONNECTION=postgresql://localhost:5432/blog
|
||||||
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import Markdown from 'react-markdown';
|
import Markdown from 'react-markdown';
|
||||||
import { test_markdown } from '@/app/blog/post/[id]/test_markdown';
|
|
||||||
import { Card, CardBody } from '@nextui-org/card';
|
import { Card, CardBody } from '@nextui-org/card';
|
||||||
import remarkGfm from 'remark-gfm';
|
import remarkGfm from 'remark-gfm';
|
||||||
import rehypeHighlight from 'rehype-highlight';
|
import rehypeHighlight from 'rehype-highlight';
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
### Rust template
|
||||||
|
# Generated by Cargo
|
||||||
|
# will have compiled files and executables
|
||||||
|
debug/
|
||||||
|
target/
|
||||||
|
|
||||||
|
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||||
|
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||||
|
Cargo.lock
|
||||||
|
|
||||||
|
# These are backup files generated by rustfmt
|
||||||
|
**/*.rs.bk
|
||||||
|
|
||||||
|
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||||
|
*.pdb
|
||||||
|
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
[package]
|
||||||
|
name = "backend"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
axum = "0.7.5"
|
||||||
|
dotenv = "0.15.0"
|
||||||
|
serde = { version = "1.0.199", features = ["derive"] }
|
||||||
|
serde_json = "1.0.116"
|
||||||
|
tokio = { version = "1.37.0", features = ["full"] }
|
||||||
|
uuid = { version = "1.8.0", features = ["v4", "fast-rng", "serde"] }
|
||||||
|
sqlx = { version = "0.7.4", features = ["runtime-tokio", "tls-native-tls", "postgres", "chrono", "uuid"] }
|
||||||
|
chrono = { version = "0.4.38", features = ["serde"] }
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
CREATE TABLE Post
|
||||||
|
(
|
||||||
|
post_id UUID PRIMARY KEY,
|
||||||
|
title VARCHAR(255) NOT NULL,
|
||||||
|
content TEXT NOT NULL,
|
||||||
|
last_update TIMESTAMP
|
||||||
|
);
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
use axum::extract::{Path, State};
|
||||||
|
use axum::Json;
|
||||||
|
use serde_json::Value;
|
||||||
|
use sqlx::PgPool;
|
||||||
|
|
||||||
|
pub async fn get_post(
|
||||||
|
State(_db_connection): State<PgPool>,
|
||||||
|
Path(post_id): Path<String>,
|
||||||
|
) -> Result<Json<Value>, Json<Value>> {
|
||||||
|
dbg!(&post_id);
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
use axum::extract::State;
|
||||||
|
use axum::Json;
|
||||||
|
use serde_json::Value;
|
||||||
|
use sqlx::PgPool;
|
||||||
|
|
||||||
|
use crate::model::Post;
|
||||||
|
|
||||||
|
pub async fn get_post_list(
|
||||||
|
State(db_connection): State<PgPool>,
|
||||||
|
Json(_request): Json<Value>,
|
||||||
|
) -> Result<Json<Value>, Json<Value>> {
|
||||||
|
let post_list: Vec<Post> = sqlx::query_as("select * from post")
|
||||||
|
.fetch_all(&db_connection)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
dbg!(&post_list);
|
||||||
|
Ok(Json::from(serde_json::to_value(post_list).unwrap()))
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
pub mod get_post;
|
||||||
|
pub mod get_post_list;
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
use std::env;
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
|
use axum::Router;
|
||||||
|
use axum::routing::get;
|
||||||
|
use dotenv::dotenv;
|
||||||
|
use sqlx::postgres::PgPoolOptions;
|
||||||
|
|
||||||
|
use crate::api::get_post::get_post;
|
||||||
|
use crate::api::get_post_list::get_post_list;
|
||||||
|
|
||||||
|
mod api;
|
||||||
|
mod model;
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
dotenv()?;
|
||||||
|
|
||||||
|
let port: u32 = match env::var("BACKEND_PORT") {
|
||||||
|
Ok(value) => value.parse().unwrap(),
|
||||||
|
Err(_) => 3001,
|
||||||
|
};
|
||||||
|
|
||||||
|
let db_connection: String = env::var("DB_CONNECTION")?;
|
||||||
|
|
||||||
|
let db_pool = PgPoolOptions::new()
|
||||||
|
.connect(&db_connection)
|
||||||
|
.await
|
||||||
|
.expect("failed to load database");
|
||||||
|
|
||||||
|
let app = Router::new()
|
||||||
|
.route("/api/get/post_list", get(get_post_list))
|
||||||
|
.route("/api/get/post/:post_id", get(get_post))
|
||||||
|
.with_state(db_pool);
|
||||||
|
|
||||||
|
let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", port)).await?;
|
||||||
|
|
||||||
|
axum::serve(listener, app).await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
use chrono::NaiveDateTime;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use sqlx::FromRow;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, FromRow)]
|
||||||
|
pub struct Post {
|
||||||
|
post_id: Uuid,
|
||||||
|
title: String,
|
||||||
|
content: String,
|
||||||
|
last_update: NaiveDateTime,
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
CREATE TABLE Tag
|
||||||
|
(
|
||||||
|
post_id UUID NOT NULL,
|
||||||
|
tag VARCHAR(255) NOT NULL
|
||||||
|
);
|
||||||
Reference in new issue
Block a user