6 Commits
27 changed files with 194 additions and 262 deletions

No files matched your search

+8 -8
View File
@@ -1,19 +1,19 @@
# postgres settings
DB_HOST=localhost
DB_PORT=5432
POSTGRES_PASSWORD=
POSTGRES_USER=
#POSTGRES_PASSWORD=
POSTGRES_USER=nocturne
POSTGRES_DB=nocturne
POSTGRES_HOST=postgres
# backend settings
#GIT_WORK_DIR=../blog_posts_git
#LOG_ROOT_DIR=/var/log/nocturne
BLOG_ENDPOINT=https://github.com/Lumither/blog-posts.git
WORK_DIR=~/.nocturne
BACKEND_PORT=3001
#BLOG_ENDPOINT=https://github.com/Lumither/blog-posts.git
BLOG_GIT_URL=https://github.com/Lumither/blog-posts.git
WORK_DIR=/nocturne
#BACKEND_PORT=3001
# frontend settings
API_LOCAL_URL=http://localhost:3001
API_LOCAL_URL=http://backend:3001
API_REMOTE_URL=https://lumither.com/api
+13 -3
View File
@@ -1,6 +1,16 @@
ddev:
ARCH := $(shell uname -m)
ifeq ($(ARCH), arm64)
ARCH := aarch64
endif
all: release
release:
docker compose -f prod.compose.yaml build --build-arg ARCH=$(ARCH)
dev:
docker-compose -f dev.compose.yaml up --build
dclean:
docker-compose -f dev.compose.yaml rm -fsv
clean:
docker-compose -f prod.compose.yaml -f dev.compose.yaml rm -fsv
+2 -4
View File
@@ -1,14 +1,12 @@
# `Nocturne: personal website & CMS`
> This is the last working version
This is the code repo of my personal website, and [here](https://github.com/Lumither/blog-posts/) is the link of the
content repo.
Powered by Next.js and Rust, built with passion and love
<p align="center">
<img src="https://avatars.githubusercontent.com/u/46409277?v=4" alt="GitHub Avatar"/>
</p>
*will add more here in the future
## Nav:
+2
View File
@@ -1 +1,3 @@
/target
/Cargo.lock
+1 -1
View File
@@ -15,7 +15,7 @@ dotenv = "0.15.0"
dirs = "5.0.1"
tokio = { version = "1.40.0", features = ["full"] }
sqlx = { version = "0.8.2", features = ["runtime-tokio", "tls-native-tls", "postgres", "chrono", "uuid"] }
axum = "0.7.5"
axum = { version = "0.7.5", features = ["macros"] }
serde = { version = "1.0.199", features = ["derive"] }
serde_json = "1.0.128"
uuid = { version = "1.10.0", features = ["v4", "fast-rng", "serde"] }
@@ -1,3 +1,5 @@
use std::error::Error;
use constants::blog::PAGE_SIZE;
use axum::{extract::Query, extract::State, http::StatusCode, Json};
@@ -14,6 +16,7 @@ pub struct Params {
page: Option<u32>,
}
#[axum::debug_handler]
pub async fn get_post_list(
State(db_connection): State<PgPool>,
Query(params): Query<Params>,
@@ -47,33 +50,72 @@ pub async fn get_post_list(
}
};
let mut res = Map::new();
let mut res = Vec::new();
let posts: Vec<_> = post_list
.into_iter()
.map(|post| analyze_post(&db_connection, post))
.map(|post| async { analyze_post(&db_connection, post).await })
.collect();
let posts = join_all(posts).await;
for post in posts {
match post {
Ok(p) => res.push(p),
Err(e) => {
return Err((
StatusCode::INTERNAL_SERVER_ERROR,
Json::from(json!(("error", e.to_string()))),
))
}
}
}
res.insert("posts".to_string(), Value::from(posts));
// res.insert("posts".to_string(), Value::from(posts));
Ok(Json::from(Value::from(res)))
}
async fn analyze_post(db_connection: &PgPool, post: PgRow) -> Value {
async fn analyze_post(db_connection: &PgPool, post: PgRow) -> Result<Value, Box<dyn Error + Send>> {
let post_id: Uuid = post.get("post_id");
let tags = fetch_post_tags(db_connection, post_id).await;
let meta = fetch_post_meta(db_connection, post_id).await.unwrap();
let header_img = meta.get("header_img");
let last_update = meta.get("last_update");
json!({
Ok(json!({
"post_id": post_id.to_string(),
"title": post.get::<String, _>("title"),
"category": post.get::<String, _>("category"),
"header_img": post.get::<String, _>("header_img"),
"header_img": header_img,
"sub_title": post.get::<String, _>("sub_title"),
"summary": post.get::<String, _>("summary"),
"last_update": post.get::<DateTime<Utc>, _>("last_update"),
"last_update": last_update,
"first_update": post.get::<DateTime<Utc>, _>("first_update"),
"tags": tags,
})
}))
}
async fn fetch_post_meta(db_connection: &PgPool, post_id: Uuid) -> Option<Value> {
match query(
r#"
SELECT jsonb_strip_nulls(to_jsonb(t.*)) - 'post_id' AS meta
FROM meta t
WHERE post_id = $1;
"#,
)
.bind(post_id)
.fetch_one(db_connection)
.await
{
Ok(value) => Some(value.get::<Value, _>("meta")),
Err(e) => {
error!(
"Database error on Reading `{}` from table `{}`: <{}>: {}",
"post meta data",
"meta",
post_id.to_string(),
e.to_string()
);
None
}
}
}
async fn fetch_post_tags(db_connection: &PgPool, post_id: Uuid) -> Vec<String> {
-1
View File
@@ -1,4 +1,3 @@
pub mod get_page_count;
pub mod get_post;
pub mod get_post_list;
pub mod refresh;
-190
View File
@@ -1,190 +0,0 @@
use std::path::Path;
use std::str::FromStr;
use axum::extract::{Query, State};
use axum::http::StatusCode;
use axum::Json;
use serde::Deserialize;
use serde_json::{Map, Value};
use sqlx::PgPool;
// use crate::api::post::errors::refresh::PostIdxError;
// use crate::api::post::utils::{build_post_idx, post};
// use crate::model::blog::{POST_BASE_MODEL, POST_HASH_MODEL, POST_META_MODEL, TAG_MODEL};
#[derive(Deserialize)]
pub struct Params {
/// refresh level
level: Option<String>,
}
pub async fn refresh(
State(db_connection): State<PgPool>,
Query(params): Query<Params>,
) -> Result<StatusCode, (StatusCode, Json<Value>)> {
// let is_force_refresh = params.level.unwrap_or("".to_string()) == "force";
//
// // search post
// let data_repo_dir = match env::var("GIT_WORK_DIR") {
// Ok(value) => value,
// Err(_) => {
// return Err((
// StatusCode::INTERNAL_SERVER_ERROR,
// Json::from(json!((
// "error",
// "error at parsing environment variable: GIT_WORK_DIR"
// ))),
// ));
// }
// };
// let post_dir = Path::new(&data_repo_dir).join("posts");
//
// if is_force_refresh {
// if let Err(e) = query("DROP TABLE IF EXISTS hash, meta, post, tag;")
// .execute(&db_connection)
// .await
// {
// error!(
// "DB query error when deleting table: `hash`, `meta`, `post`, `tag`: {}",
// e.to_string()
// );
// return Err((
// StatusCode::INTERNAL_SERVER_ERROR,
// Json::from(json!(("error", e.to_string()))),
// ));
// }
// }
//
// // generate post idx
// let post_path = search_md(&post_dir);
//
// let post_list_with_hash: Vec<(Map<_, _>, Vec<_>)> = post_path
// .into_iter()
// .filter_map(|post_path| match post::from_path(&post_path) {
// Ok(post) => match fs::read(&post_path) {
// Ok(bytes) => {
// let mut hasher = Sha256::new();
// hasher.update(bytes);
// let hash = hasher.finalize().to_vec();
// Some((post, hash))
// }
// Err(e) => {
// error!(
// "Failed to read file while hashing the post <{}>, skipping: {}",
// post_path,
// e.to_string()
// );
// None
// }
// },
// Err(e) => {
// error!(
// "Failed to parse post <{}>, skipping: {}",
// post_path,
// e.to_string()
// );
// None
// }
// })
// .collect();
//
// // db create
// query(POST_BASE_MODEL)
// .execute(&db_connection)
// .await
// .unwrap();
// query(POST_META_MODEL)
// .execute(&db_connection)
// .await
// .unwrap();
// query(TAG_MODEL).execute(&db_connection).await.unwrap();
// query(POST_HASH_MODEL)
// .execute(&db_connection)
// .await
// .unwrap();
//
// let res: Vec<_> = stream::iter(post_list_with_hash)
// .map(|(post, hash)| {
// let db_clone = db_connection.clone();
// async move { build_post_idx(&db_clone, post, hash).await }
// })
// .buffer_unordered(usize::MAX)
// .collect()
// .await;
//
// let errs: Vec<_> = res
// .into_iter()
// .filter_map(|res| res.err())
// .map(|e| e.to_string())
// .collect();
//
// if !errs.is_empty() {
// Err((
// StatusCode::INTERNAL_SERVER_ERROR,
// Json::from(json!({"error": errs})),
// ))
// } else {
// Ok(StatusCode::OK)
// }
todo!()
}
/// Search a dir, return a list of markdown files
///
/// # Arguments
/// * `entry`: dir path
///
/// # Returns
/// Vec<String, Global>
/// * `String`: path of a markdown file
///
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![]
}
}
async fn build_post_idx(
db_connection: &PgPool,
post: Map<String, Value>,
hash: Vec<u8>,
// ) -> Result<(), PostIdxError> {
) -> Result<(), ()> {
todo!()
// let meta = &post["meta"];
//
// // extract post id
// let post_id = match Uuid::from_str(post["post_id"].as_str().unwrap()) {
// Ok(id) => id,
// Err(e) => {
// return Err(PostIdxError::InvalidUUID {
// id: post["post_id"].as_str().unwrap().to_string(),
// err_msg: e.to_string(),
// })
// }
// };
//
// if build_post_idx::hash(db_connection, post_id, hash).await? {
// build_post_idx::base(db_connection, &post, post_id, meta).await?;
// build_post_idx::tag(db_connection, post_id, meta).await?;
// build_post_idx::meta(db_connection, post_id, meta).await?;
// }
//
// Ok(())
}
+1 -1
View File
@@ -7,7 +7,7 @@ pub mod check_update;
pub fn get_tasks(db_connection: Pool<Postgres>) -> Vec<Box<dyn Task>> {
let db_conn_clone = db_connection.clone();
vec![
BasicTask::new(move || check_update::task(&db_conn_clone), "0 * * * * *")
BasicTask::new(move || check_update::task(&db_conn_clone), "0/15 * * * * *")
.unwrap()
.to_task(),
]
+1 -3
View File
@@ -5,14 +5,12 @@ use crate::blog::api::{
get_page_count::get_page_count, get_post::get_post, get_post_list::get_post_list,
};
use crate::blog::api::refresh::refresh;
use axum::routing::{get, post};
use axum::routing::get;
use axum::Router;
use sqlx::{Pool, Postgres};
pub fn get_router() -> Router<Pool<Postgres>> {
Router::new()
.route("/refresh_posts", post(refresh))
.route("/get_post_list", get(get_post_list))
.route("/get_page_count", get(get_page_count))
.route("/get_post/:post_id", get(get_post))
+4 -1
View File
@@ -11,7 +11,10 @@ pub async fn init() -> Result<Pool<Postgres>, Box<dyn Error>> {
let db_connect_option = PgConnectOptions::from_str(&parse_db_uri())
.unwrap()
.disable_statement_logging();
Ok(PgPoolOptions::new().connect_with(db_connect_option).await?)
Ok(PgPoolOptions::new()
.max_connections(1) // todo: temporal solution, to be fixed
.connect_with(db_connect_option)
.await?)
}
fn parse_db_uri() -> String {
+10
View File
@@ -0,0 +1,10 @@
FROM rust:latest AS builder
WORKDIR /app
COPY . .
RUN ["cargo", "build", "-r"]
FROM gcr.io/distroless/cc AS production
ARG ARCH
COPY --from=builder /app/target/release/api_server ./api_server
COPY --from=builder /usr/lib/${ARCH}-linux-gnu/libz.so.1 /usr/lib/${ARCH}-linux-gnu/
ENTRYPOINT ["./api_server"]
+15
View File
@@ -0,0 +1,15 @@
FROM node:22 AS builder
WORKDIR /app
ENV NEXT_TELEMETRY_DISABLED=1
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM gcr.io/distroless/nodejs22 AS production
ENV NEXT_TELEMETRY_DISABLED=1
COPY --from=builder /app/next.config.mjs ./
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
ENV HOSTNAME="0.0.0.0"
CMD ["./server.js"]
+2 -2
View File
@@ -5,7 +5,7 @@ services:
dockerfile: ../container/dev/frontend.containerfile
restart: always
ports:
- 3000:3000
- "3000:3000"
volumes:
- ./frontend:/app
- /app/node_modules
@@ -22,7 +22,7 @@ services:
dockerfile: ../container/dev/backend.containerfile
restart: always
ports:
- 3001:3001
- "3001:3001"
volumes:
- ./blog_posts_git:/app/blog_posts_git:ro
env_file:
+2 -2
View File
@@ -1,7 +1,7 @@
/node_modules
/.next/
/out/
/.next
/out
/build
.DS_Store
+3 -3
View File
@@ -18,7 +18,7 @@ function BlogPostCard({ post }: Props) {
// let last_update = new Date((post as any)['last_update']);
let first_update = new Date((post as any)['first_update']);
let post_id = (post as any)['post_id'];
let header_img = (post as any)['header_img'];
let header_img = ((post as any)['header_img'] as string | null);
return (
@@ -29,7 +29,7 @@ function BlogPostCard({ post }: Props) {
isHoverable
>
{ header_img !== '' && <div className={ `w-full overflow h-80 relative` }>
{ header_img && <div className={ `w-full overflow h-80 relative` }>
<Image
alt={ 'cover picture' }
src={ header_img }
@@ -48,7 +48,7 @@ function BlogPostCard({ post }: Props) {
<CardBody className={ `overflow-visible` }>
<div className={ `flex flex-row justify-between` }>
<div className={ `mx-2 ${ header_img !== '' ? '-mt-6' : 'mt-2' }` }>
<div className={ `mx-2 ${ header_img ? '-mt-6' : 'mt-2' }` }>
<Chip className={ `mb-2 z-30 text-lg` } radius={ `sm` } size={ 'lg' } color={ `secondary` }
variant={ `solid` }>{ category }</Chip>
<p className={ `text-4xl font-bold` }> { title }</p>
+2 -2
View File
@@ -8,8 +8,8 @@ import { Card, CardBody } from '@nextui-org/card';
const PostList = async ({ page }: { page: number }) => {
try {
const res = await fetchNocturneApi(`/get_post_list?page=${ page }`);
let posts = (await res.json() as any)['posts'];
const res = await fetchNocturneApi(`/blog/get_post_list?page=${ page }`);
const posts = await res.json();
return (
// blog list
<div className={ 'w-full' }>
+6 -6
View File
@@ -2,15 +2,15 @@ import PostList from '@/app/blog/PostList';
import Pagination from '@/app/blog/Pagination';
import { fetchNocturneApi } from '@/app/blog/api';
async function Blog({ searchParams }: {
searchParams?: {
page?: number
};
async function Blog({
searchParams
}: {
searchParams: Promise<{ [key: string]: string | string[] | undefined }>
}) {
const parsePage = Number(searchParams?.page);
const parsePage = Number((await searchParams)?.page);
const currPage = parsePage > 0 ? parsePage : 1;
const ret = await fetchNocturneApi(`/get_page_count`);
const ret = await fetchNocturneApi(`/blog/get_page_count`);
const pageCount = (await ret.json() as any)['res'] as number;
return (
+3 -1
View File
@@ -17,7 +17,7 @@ const Footer = (props: Props) => {
const author = post['meta']['author'] ?? 'Tao';
const author_link = post['meta']['author_link'] ?? '/about';
const post_date = new Date(post['first_update']);
const update_date = new Date(post['last_update']);
const update_date: Date | undefined = post['last_update'] ? new Date(post['last_update']) : undefined;
const cc = post['meta']['cc'] ?? 'by-nc-sa';
@@ -55,6 +55,7 @@ const Footer = (props: Props) => {
year: `numeric`
}) }</p>
</div>
{ update_date &&
<div>
<p className={ 'text-lg font-bold' }>Updated on</p>
<p>{ update_date.toLocaleString('default', {
@@ -63,6 +64,7 @@ const Footer = (props: Props) => {
year: `numeric`
}) }</p>
</div>
}
<div>
<p className={ 'text-lg font-bold' }>Published under</p>
<Link href={ `https://creativecommons.org/licenses/${ cc }/4.0/` }>
+4 -4
View File
@@ -18,11 +18,11 @@ const Headers = (props: Props) => {
// let last_update = new Date((post as any)['last_update']);
let first_update = new Date(post['first_update']);
// let post_id = (post as any)['post_id'];
let header_img = post['header_img'];
let header_img = post['meta']['header_img'] as string | null;
return (
<>
{ header_img !== '' && <div className={ `w-full overflow h-96 relative` }>
{ header_img && <div className={ `w-full overflow h-96 relative` }>
<Image
alt={ 'cover picture' }
src={ header_img }
@@ -38,11 +38,11 @@ const Headers = (props: Props) => {
</div> }
<div
className={ header_img !== '' ?
className={ header_img ?
'mx-8 -mt-16 backdrop-blur transition bg-zinc-300/80 dark:bg-gray-600/25 rounded-2xl z-30'
: 'mx-6 mt-6'
}>
<div className={ header_img !== '' ? `p-4` : 'p-4 pt-2' }>
<div className={ header_img ? `p-4` : 'p-4 pt-2' }>
<Chip className={ `mb-2` } radius={ `sm` } color={ `secondary` }
variant={ `solid` }>{ category }</Chip>
<p className={ `text-4xl font-bold` }> { title }</p>
+1 -1
View File
@@ -13,7 +13,7 @@ async function PostCard(props: Props) {
try {
const res = await fetchNocturneApi(`/get_post/${ props.id }`);
const res = await fetchNocturneApi(`/blog/get_post/${ props.id }`);
const ret: any = await res.json();
return (
<div>
+6 -2
View File
@@ -3,7 +3,11 @@ import PostCard from '@/app/blog/post/[id]/PostCard';
import { MotionDiv } from '@/app/public/MotionDiv';
import ButtonGoBack from '@/app/blog/post/[id]/ButtonGoBack';
function BlogReader({ params }: { params: { id: string } }) {
async function BlogReader({
params
}: {
params: Promise<{ id: string }>
}) {
return (
<MotionDiv
@@ -13,7 +17,7 @@ function BlogReader({ params }: { params: { id: string } }) {
transition={ { ease: 'easeInOut', duration: 0.5 } }
>
<div className={ `max-w-full` }>
<PostCard id={ params.id } />
<PostCard id={ (await params).id } />
</div>
<div className={ 'mt-3' }>
<ButtonGoBack />
+14 -13
View File
@@ -3,7 +3,16 @@
import React, { useEffect, useState } from 'react';
import { Button } from '@nextui-org/react';
import Image from 'next/image';
import { IoHome, IoInformationCircle, IoLink, IoLogoGithub, IoMail, IoPencil, IoSearch } from 'react-icons/io5';
import {
IoFlask,
IoHome,
IoInformationCircle,
IoLink,
IoLogoGithub,
IoMail,
IoPencil,
IoSearch
} from 'react-icons/io5';
import Link from 'next/link';
import ThemeSwitcher from '@/components/ThemeSwitcher';
import { usePathname } from 'next/navigation';
@@ -34,6 +43,10 @@ const entries: { display_name: string; href: string; icon: React.ReactNode }[] =
icon: <IoSearch size={ `20px` }></IoSearch>,
display_name: 'Search',
href: '/search'
}, {
icon: <IoFlask size={ '20px' } />,
display_name: 'Lab',
href: 'https://lab.lmt.moe'
}
];
@@ -51,18 +64,6 @@ const connections: { label: string, href: string, icon: React.ReactNode }[] = [
];
function Navbar() {
// const [ scrolled, setScrolled ] = useState(false);
// useEffect(() => {
// const handleScroll = () => {
// const isScrolled = window.scrollY > 0;
// setScrolled(isScrolled);
// };
// window.addEventListener('scroll', handleScroll);
// return () => {
// window.removeEventListener('scroll', handleScroll);
// };
// }, []);
const path = usePathname();
const [ shownPath, setShownPath ] = useState(path);
+2 -2
View File
@@ -49,9 +49,9 @@ function ThemeSwitcher() {
fullWidth
isIconOnly={ isMobile }
>
{ switcherElements.get(currTheme.values().next().value)?.icon }
{ switcherElements.get(currTheme.values().next().value as string)?.icon }
<p className={ 'hidden lg:block' }>
{ switcherElements.get(currTheme.values().next().value)?.display_name }
{ switcherElements.get(currTheme.values().next().value as string)?.display_name }
</p>
</Button>
</DropdownTrigger>
+2 -2
View File
@@ -11,8 +11,8 @@ const nextConfig = {
hostname: 'pic.re'
}
]
}
// output: 'standalone'
},
output: 'standalone'
};
export default nextConfig;
+1 -1
View File
@@ -13,7 +13,7 @@
"@types/react-syntax-highlighter": "^15.5.13",
"framer-motion": "^11.3.24",
"lxgw-wenkai-webfont": "^1.7.0",
"next": "14.2.3",
"next": "^15.0.3",
"next-themes": "^0.3.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
+38
View File
@@ -0,0 +1,38 @@
services:
frontend:
depends_on:
- backend
build:
context: ./frontend
dockerfile: ../container/release/frontend.containerfile
restart: always
ports:
- "3000:3000"
env_file:
- ./.env
backend:
depends_on:
- postgres
build:
context: ./backend
dockerfile: ../container/release/backend.containerfile
args:
ARCH: ${ARCH}
restart: always
ports:
- "3001:3001"
env_file:
- ./.env
- ./.env.local
dns:
- 1.1.1.1
postgres:
image: postgres:16.3
ports:
- "5432:5432"
env_file:
- ./.env
- ./.env.local