Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislearn committed Jan 18, 2025
1 parent 027defe commit 0685b28
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 341 deletions.
27 changes: 13 additions & 14 deletions templates/classic/_base/Cargo.toml.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ readme = "./README.md"
anyhow = "1.0.79"
figment = { version = "0.10.8", features = ["env", "toml"] }
jsonwebtoken = "9.2.0"
once_cell = "1.19.0"
rust-embed = "8.0.0"
salvo = {version = "0.76.0", features = ["full"]}
serde = "1.0.196"
Expand All @@ -26,50 +25,50 @@ tracing-appender ="0.2.3"
tracing-subscriber = {version = "0.3.19", features = ["std", "fmt", "env-filter", "tracing-log", "time", "local-time", "json"]}
{%- if db_lib == "sqlx" and db_type == "postgres" %}
sqlx = { version = "0.8", features = ["runtime-tokio", "macros", "postgres"]}
{% endif %}
{%- endif %}
{%- if db_lib == "sqlx" and db_type == "mysql" %}
sqlx = { version = "0.8", features = ["runtime-tokio", "macros", "mysql"]}
{%- endif %}
{%- if db_lib == "sqlx" and db_type == "sqlite" %}
sqlx = { version = "0.8", features = ["runtime-tokio", "macros", "sqlite"]}
{% endif %}
{%- endif %}
{%- if db_lib == "seaorm" and db_type == "postgres" %}
sea-orm = { version = "1", features = ["runtime-tokio-native-tls", "sqlx-postgres"]}
{% endif %}
{%- endif %}
{%- if db_lib == "seaorm" and db_type == "mysql" %}
sea-orm = { version = "1", features = ["runtime-tokio-native-tls", "sqlx-mysql"]}
{% endif %}
{%- endif %}
{%- if db_lib == "seaorm" and db_type == "sqlite" %}
sea-orm = { version = "1", "features": ["runtime-tokio-native-tls", "sqlx-sqlite"]}
{% endif %}
{%- endif %}
{%- if db_lib == "diesel" and db_type == "postgres" %}
diesel = { version = "2", features = ["postgres", "r2d2", "chrono", "serde_json"]}
diesel_migrations = "2"
scheduled-thread-pool = "0.2.0"
url = "2"
{% endif %}
{%- endif %}
{%- if db_lib == "diesel" and db_type == "mysql" %}
diesel = { version = "2", features = ["mysql"]}
{% endif %}
{%- endif %}
{%- if db_lib == "diesel" and db_type == "sqlite" %}
diesel = { version = "2", features = ["sqlite", "returning_clauses_for_sqlite_3_35"]}
{% endif %}
{%- endif %}
{%- if db_lib == "rbatis" and db_type == "postgres" %}
rbatis = { version = "4", features = ["debug_mode"]}
{% if db_type == "postgres" %}
{%- if db_type == "postgres" %}
rbdc-pg = { "version": "4"}
{% endif %}
{%- endif %}
{%- if db_type == "mysql" %}
rbdc-mysql = { version = "4"}
{% endif %}
{%- if db_type == "sqlite" %}
rbdc-sqlite = { version = "4"}
{% endif %}
{%- endif %}
rbs = { version = "4"}
{% endif %}
{%- endif %}
{%- if db_type == "mongodb" %}
mongodb = { version = "2"}
futures-util = { version = "0.3"}
{% endif %}
{%- endif %}
askama = "0.12.0"
rand = "0.8.3"
2 changes: 0 additions & 2 deletions templates/classic/_base/src/config/db.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::{fs::File, io::Read, path::Path};

use serde::{Deserialize, Serialize};

use super::default_false;
Expand Down
38 changes: 17 additions & 21 deletions templates/classic/_base/src/config/log.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
// https://github.com/clia/tracing-config/blob/main/src/lib.rs

use std::{fs::File, io::Read, path::Path};

use once_cell::sync::Lazy;
use serde::Deserialize;
use time::macros::format_description;
pub use tracing_appender::non_blocking::WorkerGuard;
use tracing_appender::non_blocking::WorkerGuard;
use tracing_subscriber::fmt;
use tracing_subscriber::fmt::time::OffsetTime;

use tracing_appender::rolling;

Expand All @@ -25,7 +19,7 @@ pub struct LogConfig {
#[serde(default = "default_true")]
pub with_ansi: bool,
#[serde(default = "default_true")]
pub to_stdout: bool,
pub stdout: bool,
#[serde(default = "default_directory")]
pub directory: String,
#[serde(default = "default_file_name")]
Expand Down Expand Up @@ -66,7 +60,7 @@ impl Default for LogConfig {
Self {
filter_level: default_filter_level(),
with_ansi: true,
to_stdout: false,
stdout: false,
directory: default_directory(),
file_name: default_file_name(),
rolling: default_rolling(),
Expand All @@ -80,6 +74,7 @@ impl Default for LogConfig {
}
}

#[allow(dead_code)]
impl LogConfig {
/// Will try_from_default_env while not setted.
///
Expand All @@ -99,20 +94,20 @@ impl LogConfig {
}

/// Will append log to stdout.
pub fn to_stdout(mut self, to_stdout: bool) -> Self {
self.to_stdout = to_stdout;
pub fn stdout(mut self, stdout: bool) -> Self {
self.stdout = stdout;
self
}

/// Set log file directory.
pub fn directory(mut self, directory: &str) -> Self {
self.directory = directory.to_owned();
pub fn directory(mut self, directory: impl Into<String>) -> Self {
self.directory = directory.into();
self
}

/// Set log file name.
pub fn file_name(mut self, file_name: &str) -> Self {
self.file_name = file_name.to_owned();
pub fn file_name(mut self, file_name: impl Into<String>) -> Self {
self.file_name = file_name.into();
self
}

Expand All @@ -131,15 +126,16 @@ impl LogConfig {
/// Valid values: pretty | compact | json | full
///
/// Will panic on other values.
pub fn format(mut self, format: &str) -> Self {
pub fn format(mut self, format: impl Into<String>) -> Self {
let format = format.into();
if format != FORMAT_PRETTY
&& format != FORMAT_COMPACT
&& format != FORMAT_JSON
&& format != FORMAT_FULL
{
panic!("Unknown format")
}
self.format = format.to_owned();
self.format = format;
self
}

Expand Down Expand Up @@ -205,7 +201,7 @@ impl LogConfig {
.with_thread_names(self.with_thread_names)
.with_source_location(self.with_source_location),
);
if self.to_stdout {
if self.stdout {
subscriber.with_writer(std::io::stdout).init();
} else {
subscriber.with_writer(file_writer).init();
Expand All @@ -220,7 +216,7 @@ impl LogConfig {
.with_thread_names(self.with_thread_names)
.with_source_location(self.with_source_location),
);
if self.to_stdout {
if self.stdout {
subscriber.with_writer(std::io::stdout).init();
} else {
subscriber.with_writer(file_writer).init();
Expand All @@ -235,7 +231,7 @@ impl LogConfig {
.with_thread_names(self.with_thread_names)
.with_source_location(self.with_source_location),
);
if self.to_stdout {
if self.stdout {
subscriber.json().with_writer(std::io::stdout).init();
} else {
subscriber.json().with_writer(file_writer).init();
Expand All @@ -250,7 +246,7 @@ impl LogConfig {
.with_thread_names(self.with_thread_names)
.with_source_location(self.with_source_location),
);
if self.to_stdout {
if self.stdout {
subscriber.with_writer(std::io::stdout).init();
} else {
subscriber.with_writer(file_writer).init();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use std::sync::OnceLock;
use std::{fs::File, io::Read, path::Path};

use serde::Deserialize;
use salvo::http::HeaderValue;

use crate::env_vars::required_var;

mod log;
pub use log::LogConfig;
Expand All @@ -16,7 +12,6 @@ pub fn get() -> &'static ServerConfig {
CONFIG.get().expect("config should be set")
}


#[derive(Deserialize, Clone, Debug)]
pub struct ServerConfig {
#[serde(default = "default_listen_addr")]
Expand All @@ -42,6 +37,9 @@ pub struct TlsConfig {
pub fn default_false() -> bool {
false
}
pub fn default_true() -> bool {
true
}

fn default_listen_addr() -> String {
"127.0.0.1:8008".into()
Expand Down
Loading

0 comments on commit 0685b28

Please sign in to comment.