ref(backend) start of refactor

This commit is contained in:
lumither committed 2024-09-04 23:18:41 -07:00
1 parent 3c1fc34c2a
commit c1ba41ea16
36 files changed
+182 -41

No files matched your search

+3 -1
View File
@@ -7,8 +7,10 @@ POSTGRES_DB=nocturne
# backend settings
GIT_WORK_DIR=../blog_posts_git
BLOG_ENDPOINT=https://github.com/Lumither/blog-posts.git
LOG_ROOT_DIR=/var/log/nocturne
BLOG_ENDPOINT=https://github.com/Lumither/blog-posts.git
WORD_DIR=~/.nocturne
BACKEND_PORT=3001
# frontend settings
+7 -25
View File
@@ -1,25 +1,7 @@
[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", "alloc"] }
regex = "1.10.4"
futures = { version = "0.3.30", features = ["executor"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
tracing-appender = "0.2.3"
log = "0.4.21"
sha2 = "0.10.8"
thiserror = "1.0.63"
base64 = "0.22.1"
[workspace]
resolver = "2"
members = [
"api_server",
"constants",
"markdown"
]
+12
View File
@@ -0,0 +1,12 @@
[package]
name = "api_server"
version = "0.1.0"
edition = "2021"
[dependencies]
constants = { path = "../constants" }
chrono = "0.4.38"
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
tracing-appender = "0.2.3"
dotenv = "0.15.0"
+22
View File
@@ -0,0 +1,22 @@
use crate::logger;
use std::env;
use tracing_appender::non_blocking::WorkerGuard;
pub fn logger_init() -> (WorkerGuard, WorkerGuard) {
logger::init()
}
pub fn load_env() {
if let Some(env_file) = env::args().nth(1) {
match dotenv::from_filename(&env_file) {
Ok(_) => {
println!("[info] env loaded from {}, starting up", env_file);
}
Err(e) => {
panic!("[fatal] failed to read {}: {}", env_file, e);
}
}
} else {
println!("[info] no .env file referred, starting up")
}
}
+68
View File
@@ -0,0 +1,68 @@
use std::{env, io};
use chrono::Local;
use tracing::info;
use tracing_appender::non_blocking::WorkerGuard;
use tracing_appender::{non_blocking, rolling};
use tracing_subscriber::fmt::format::Writer;
use tracing_subscriber::fmt::time::FormatTime;
use tracing_subscriber::fmt::MakeWriter;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::{fmt, Registry};
struct LocalTimer;
impl FormatTime for LocalTimer {
fn format_time(&self, w: &mut Writer<'_>) -> std::fmt::Result {
write!(
w,
"{}",
Local::now().format(constants::time::DEFAULT_TIME_FORMAT)
)
}
}
type _LogWriter = dyn for<'writer> MakeWriter<'writer, Writer = dyn io::Write + 'static> + 'static;
pub fn init() -> (WorkerGuard, WorkerGuard) {
let log_path = env::var("LOG_ROOT_DIR").unwrap_or_else(|e| {
panic!("[fatal] failed to load LOG_ROOT_DIR: {}", e);
});
let log_path = format!("{}/backend", log_path);
let (info_log_writer, info_guard) = non_blocking(rolling::never(&log_path, "info.log"));
let (err_log_writer, error_guard) = non_blocking(rolling::never(&log_path, "error.log"));
let log_format = tracing_subscriber::fmt::format()
.with_level(true)
.with_target(true)
.with_source_location(true)
.with_timer(LocalTimer);
let subscribers = Registry::default()
.with(
fmt::Layer::default()
.with_writer(info_log_writer)
.with_ansi(false),
)
.with(
fmt::Layer::default()
.with_writer(err_log_writer)
.with_ansi(false),
)
.with(
fmt::Layer::default()
.with_writer(io::stdout)
.with_ansi(true)
.event_format(log_format),
);
tracing::subscriber::set_global_default(subscribers)
.expect("[fatal] Failed to setup logging system");
info!("Module Started: Logger");
(info_guard, error_guard)
}
+7
View File
@@ -0,0 +1,7 @@
mod init;
mod logger;
fn main() {
init::load_env();
let _guards = init::logger_init();
}
+6
View File
@@ -0,0 +1,6 @@
[package]
name = "constants"
version = "0.1.0"
edition = "2021"
[dependencies]
+1
View File
@@ -0,0 +1 @@
pub const PAGE_SIZE: u32 = 6;
+2
View File
@@ -0,0 +1,2 @@
pub mod blog;
pub mod time;
+1
View File
@@ -0,0 +1 @@
pub const DEFAULT_TIME_FORMAT: &str = "%Y-%m-%dT%H:%M:%S%z";
+25
View File
@@ -0,0 +1,25 @@
[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", "alloc"] }
regex = "1.10.4"
futures = { version = "0.3.30", features = ["executor"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
tracing-appender = "0.2.3"
log = "0.4.21"
sha2 = "0.10.8"
thiserror = "1.0.63"
base64 = "0.22.1"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -4,13 +4,12 @@ use std::num::ParseIntError;
use std::process::exit;
use std::str::FromStr;
use crate::api::get::get_page_count::get_page_count;
use crate::api::get::get_post::get_post;
use crate::api::get::get_post_list::get_post_list;
use crate::api::post::refresh::refresh;
use crate::api::{
get::{get_page_count::get_page_count, get_post::get_post, get_post_list::get_post_list},
post::refresh::refresh,
};
use axum::routing::{get, post};
use axum::Router;
use dotenv::dotenv;
use sqlx::postgres::{PgConnectOptions, PgPoolOptions};
use sqlx::ConnectOptions;
use tracing::{error, info, warn};
@@ -26,21 +25,15 @@ async fn main() -> Result<(), Box<dyn Error>> {
if let Some(env_file) = env::args().nth(1) {
match dotenv::from_filename(&env_file) {
Ok(_) => {
println!("[Info] env loaded from {}", env_file);
println!("[Info] env loaded from {}, starting up", env_file);
}
Err(e) => {
panic!("[Fatal] failed to read {}: {}", env_file, e);
}
}
};
match dotenv() {
Ok(_) => {
println!("[Info] env loaded from .env file, starting up...")
} else {
println!("[info] no .env file referred, starting up")
}
Err(_) => {
println!("[Info] .env not detected, starting up...")
}
};
let _guards = logger::init();
@@ -52,7 +45,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
Err(_) => 3001,
};
let db_connect_option = PgConnectOptions::from_str(dbg!(&parse_db_uri()))
let db_connect_option = PgConnectOptions::from_str(&parse_db_uri())
.unwrap()
.disable_statement_logging();
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+6
View File
@@ -0,0 +1,6 @@
[package]
name = "markdown"
version = "0.1.0"
edition = "2021"
[dependencies]
+14
View File
@@ -0,0 +1,14 @@
pub fn add(left: u64, right: u64) -> u64 {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}