Skip to content

Commit 1fdaab0

Browse files
committed
update
1 parent af0a254 commit 1fdaab0

File tree

17 files changed

+183
-15
lines changed

17 files changed

+183
-15
lines changed

.env.local

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ DISCORD_TOKEN="your_discord_token"
3030
# cms db:
3131
DATABASE_URL=postgres://username:password@localhost/diesel_demo
3232

33+
HOST=127.0.0.1
34+
PORT=5000
35+
3336
################################################################################
3437

3538
# BINANCE API:
3639
BINANCE_API_KEY="your_binance_api_key"
37-
BINANCE_SECRET_KEY="your_binance_secret_key"
40+
BINANCE_SECRET_KEY="your_binance_secret_key"

Taskfile.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ tasks:
153153
cmds:
154154
- task: commit
155155
- task: commit
156-
- task: push
156+
- git status
157157
ignore_error: true
158158

159159
commit:

crates/rs-cms-migration/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[package]
2-
name = "migration"
2+
name = "rs-cms-migration"
33
version = "0.1.0"
44
edition = "2021"
55
publish = false
66

77
[lib]
8-
name = "migration"
8+
name = "rs_cms_migration"
99
path = "src/lib.rs"
1010

1111
[dependencies]

crates/rs-cms/Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ serde_json = "1.0"
2626
diesel = { version = "1.4", features = ["mysql"] } # orm
2727
dotenvy = "0.15" # env vars
2828

29-
log = "0.4" # log
30-
pretty_env_logger = "0.4" # colorful log
29+
#log = "0.4" # log
30+
#pretty_env_logger = "0.4" # colorful log
3131

3232
# sqlx-postgres
3333
sea-orm = { version = "^0.8", features = ["sqlx-mysql", "runtime-tokio-native-tls", "macros", "debug-print", "mock"] }
@@ -36,3 +36,4 @@ anyhow = "1.0"
3636

3737
# local crates:
3838
rs-cms-entity = { path = "../rs-cms-entity" }
39+
#rs-cms-migration = { path = "../rs-cms-migration" }

crates/rs-cms/Taskfile.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ tasks:
1616

1717
# auto compile and run:
1818
watch:
19-
- cargo watch -x run
19+
cmds:
20+
- cargo watch -x run
21+
22+
clean:
23+
cmds:
24+
- cargo clean
2025

2126
open:
2227
cmds:

crates/rs-cms/src/config/config.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
struct AppConfig {
2+
name: String,
3+
version: String,
4+
5+
http: ServerConfig,
6+
db: DbConfig,
7+
cache: CacheConfig,
8+
mq: MQConfig,
9+
}
10+
11+
struct ServerConfig {
12+
host: String,
13+
port: String,
14+
}
15+
16+
struct DbConfig {
17+
url: String,
18+
}
19+
20+
struct CacheConfig {
21+
url: String,
22+
}
23+
24+
struct MQConfig {
25+
url: String,
26+
}

crates/rs-cms/src/config/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod config;

crates/rs-cms/src/dao/cache/cache.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub struct CacheDao {}

crates/rs-cms/src/dao/cache/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod cache;

crates/rs-cms/src/dao/db/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod user;

crates/rs-cms/src/dao/db/user.rs

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
use rs_cms_entity::user;
2+
use sea_orm::{DatabaseConnection, DbErr};
3+
4+
struct UserDao {
5+
conn: DatabaseConnection,
6+
}
7+
8+
impl UserDao {
9+
fn init(&mut self, mut conn: DatabaseConnection) {
10+
self.conn = conn
11+
}
12+
13+
fn get_user(&self) -> Result<user::Model, DbErr> {
14+
todo!()
15+
}
16+
17+
fn add_user(&mut self) {}
18+
}
19+
20+
#[cfg(test)]
21+
mod tests {
22+
use sea_orm::{
23+
entity::{prelude::*, *},
24+
tests_cfg::*,
25+
DatabaseBackend, MockDatabase, Transaction,
26+
};
27+
28+
#[async_std::test]
29+
async fn test_find_cake() -> Result<(), DbErr> {
30+
// Create MockDatabase with mock query results
31+
let db = MockDatabase::new(DatabaseBackend::Postgres)
32+
.append_query_results(vec![
33+
// First query result
34+
vec![cake::Model { id: 1, name: "New York Cheese".to_owned() }],
35+
// Second query result
36+
vec![
37+
cake::Model { id: 1, name: "New York Cheese".to_owned() },
38+
cake::Model { id: 2, name: "Chocolate Forest".to_owned() },
39+
],
40+
])
41+
.append_query_results(vec![
42+
// Third query result
43+
vec![(
44+
cake::Model { id: 1, name: "Apple Cake".to_owned() },
45+
fruit::Model { id: 2, name: "Apple".to_owned(), cake_id: Some(1) },
46+
)],
47+
])
48+
.into_connection();
49+
50+
// Find a cake from MockDatabase
51+
// Return the first query result
52+
assert_eq!(
53+
cake::Entity::find().one(&db).await?,
54+
Some(cake::Model { id: 1, name: "New York Cheese".to_owned() })
55+
);
56+
57+
// Find all cakes from MockDatabase
58+
// Return the second query result
59+
assert_eq!(
60+
cake::Entity::find().all(&db).await?,
61+
vec![
62+
cake::Model { id: 1, name: "New York Cheese".to_owned() },
63+
cake::Model { id: 2, name: "Chocolate Forest".to_owned() },
64+
]
65+
);
66+
67+
// Find all cakes with its related fruits
68+
assert_eq!(
69+
cake::Entity::find().find_also_related(fruit::Entity).all(&db).await?,
70+
vec![(
71+
cake::Model { id: 1, name: "Apple Cake".to_owned() },
72+
Some(fruit::Model { id: 2, name: "Apple".to_owned(), cake_id: Some(1) })
73+
)]
74+
);
75+
76+
// Checking transaction log
77+
assert_eq!(
78+
db.into_transaction_log(),
79+
vec![
80+
Transaction::from_sql_and_values(
81+
DatabaseBackend::Postgres,
82+
r#"SELECT "cake"."id", "cake"."name" FROM "cake" LIMIT $1"#,
83+
vec![1u64.into()]
84+
),
85+
Transaction::from_sql_and_values(
86+
DatabaseBackend::Postgres,
87+
r#"SELECT "cake"."id", "cake"."name" FROM "cake""#,
88+
vec![]
89+
),
90+
Transaction::from_sql_and_values(
91+
DatabaseBackend::Postgres,
92+
r#"SELECT "cake"."id" AS "A_id", "cake"."name" AS "A_name", "fruit"."id" AS "B_id", "fruit"."name" AS "B_name", "fruit"."cake_id" AS "B_cake_id" FROM "cake" LEFT JOIN "fruit" ON "cake"."id" = "fruit"."cake_id""#,
93+
vec![]
94+
),
95+
]
96+
);
97+
98+
Ok(())
99+
}
100+
}

crates/rs-cms/src/dao/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod cache;
2+
pub mod db;

crates/rs-cms/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod dao;
2+
pub mod service;
3+
pub mod utils;

crates/rs-cms/src/main.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ use axum::{
2525
use dotenvy::dotenv;
2626
// use log::{debug, info, warn};
2727
use crate::service::hello;
28-
use pretty_env_logger;
28+
// use pretty_env_logger;
2929
use serde::{Deserialize, Serialize};
3030
use std::{
3131
collections::HashMap,
3232
env,
3333
net::SocketAddr,
34+
str::FromStr,
3435
sync::{Arc, RwLock},
3536
time::Duration,
3637
};
@@ -40,6 +41,8 @@ use uuid::Uuid;
4041
mod service;
4142
mod utils;
4243
use crate::utils::{route, shutdown};
44+
// use rs_cms_migration::{Migrator, MigratorTrait};
45+
use sea_orm::{prelude::*, Database, QueryOrder, Set};
4346
use tracing::{debug, error, info, warn, Level};
4447
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
4548

@@ -54,9 +57,21 @@ async fn main() {
5457
.init();
5558

5659
dotenv().ok();
57-
for (key, value) in env::vars() {
58-
println!("{key}: {value}");
59-
}
60+
// for (key, value) in env::vars() {
61+
// println!("{key}: {value}");
62+
// }
63+
64+
let db_url = env::var("DATABASE_URL").expect("DATABASE_URL is not set in .env file");
65+
let host = env::var("HOST").unwrap_or("127.0.0.1".into());
66+
let port = env::var("PORT").unwrap_or("4000".into());
67+
let server_url = format!("{}:{}", host, port);
68+
debug!("db url: {}", db_url);
69+
70+
// todo x: db conn:
71+
let conn = Database::connect(db_url).await.expect("Database connection failed");
72+
73+
// todo x: migration
74+
// Migrator::up(&conn, None).await.unwrap();
6075

6176
debug!("debug print");
6277
info!("info print");
@@ -98,10 +113,9 @@ async fn main() {
98113

99114
// app.fallback(handler_404());
100115

101-
let addr = SocketAddr::from(([127, 0, 0, 1], 4000));
102-
println!("Listening on http://{}", addr);
116+
let addr = SocketAddr::from_str(&server_url).unwrap();
103117

104-
tracing::debug!("listening on {}", addr);
118+
debug!("listening on http://{}", addr);
105119
axum::Server::bind(&addr)
106120
.serve(app.into_make_service())
107121
.with_graceful_shutdown(shutdown::shutdown_signal()) // graceful shutdown

crates/rs-cms/src/service/hello.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ pub async fn todos_delete(Path(id): Path<Uuid>, Extension(db): Extension<Db>) ->
123123
}
124124
}
125125

126-
pub(crate) type Db = Arc<RwLock<HashMap<Uuid, Todo>>>;
126+
pub type Db = Arc<RwLock<HashMap<Uuid, Todo>>>;
127127

128128
#[derive(Debug, Serialize, Clone)]
129129
pub struct Todo {

crates/rs-cms/src/service/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pub mod hello;
2+
pub mod user;

crates/rs-cms/src/service/user.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pub struct UserService {}
2+
3+
impl UserService {
4+
fn new() {}
5+
6+
pub async fn get_user(&self) {}
7+
8+
pub async fn add_user(&self) {}
9+
}

0 commit comments

Comments
 (0)