-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from VadimYarovoy/feat/db_setup
merge: Database setup
- Loading branch information
Showing
9 changed files
with
674 additions
and
7 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,9 @@ | ||
[server] | ||
port = 3000 | ||
port = 3000 | ||
|
||
[db] | ||
host = "localhost" | ||
port = 5432 | ||
database = "wborders" | ||
user = "postgres" | ||
password = "postgres" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,47 @@ | ||
use axum::Router; | ||
use deadpool_postgres::{Config, ManagerConfig, Pool, RecyclingMethod, Runtime}; | ||
use std::sync::Arc; | ||
use tokio_postgres::NoTls; | ||
use tower_http::trace::{self, TraceLayer}; | ||
use tracing::Level; | ||
|
||
use crate::api; | ||
use crate::config::AppConfig; | ||
|
||
pub async fn app() -> Router { | ||
Router::new().nest("/api/", api::routes()) | ||
pub struct AppState { | ||
#[allow(dead_code)] // not in use will be removed soon | ||
pub pool: Pool, | ||
} | ||
|
||
impl AppState { | ||
async fn create(config: AppConfig) -> Arc<AppState> { | ||
let mut cfg = Config::new(); | ||
|
||
cfg.dbname = Some(config.db.database); | ||
cfg.host = Some(config.db.host); | ||
cfg.password = Some(config.db.password); | ||
cfg.user = Some(config.db.user); | ||
cfg.manager = Some(ManagerConfig { | ||
recycling_method: RecyclingMethod::Fast, | ||
}); | ||
|
||
let pool = cfg | ||
.create_pool(Some(Runtime::Tokio1), NoTls) | ||
.expect("Fildel to create db pool"); | ||
|
||
Arc::new(AppState { pool }) | ||
} | ||
} | ||
|
||
pub async fn app(config: AppConfig) -> Router { | ||
let app_state = AppState::create(config).await; | ||
|
||
Router::new() | ||
.nest("/api/", api::routes()) | ||
.with_state(Arc::clone(&app_state)) | ||
.layer( | ||
TraceLayer::new_for_http() | ||
.make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO)) | ||
.on_response(trace::DefaultOnResponse::new().level(Level::INFO)), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
CREATE TABLE IF NOT EXISTS orders ( | ||
id SERIAL PRIMARY KEY, | ||
order_data JSONB NOT NULL | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
-- insert test value | ||
INSERT INTO orders (order_data) | ||
VALUES ('{ | ||
"order_uid": "b563feb7b2b84b6test", | ||
"track_number": "WBILMTESTTRACK", | ||
"entry": "WBIL", | ||
"delivery": { | ||
"name": "Test Testov", | ||
"phone": "+9720000000", | ||
"zip": "2639809", | ||
"city": "Kiryat Mozkin", | ||
"address": "Ploshad Mira 15", | ||
"region": "Kraiot", | ||
"email": "[email protected]" | ||
}, | ||
"payment": { | ||
"transaction": "b563feb7b2b84b6test", | ||
"request_id": "", | ||
"currency": "USD", | ||
"provider": "wbpay", | ||
"amount": 1817, | ||
"payment_dt": 1637907727, | ||
"bank": "alpha", | ||
"delivery_cost": 1500, | ||
"goods_total": 317, | ||
"custom_fee": 0 | ||
}, | ||
"items": [ | ||
{ | ||
"chrt_id": 9934930, | ||
"track_number": "WBILMTESTTRACK", | ||
"price": 453, | ||
"rid": "ab4219087a764ae0btest", | ||
"name": "Mascaras", | ||
"sale": 30, | ||
"size": "0", | ||
"total_price": 317, | ||
"nm_id": 2389212, | ||
"brand": "Vivienne Sabo", | ||
"status": 202 | ||
} | ||
], | ||
"locale": "en", | ||
"internal_signature": "", | ||
"customer_id": "test", | ||
"delivery_service": "meest", | ||
"shardkey": "9", | ||
"sm_id": 99, | ||
"date_created": "2021-11-26T06:22:19Z", | ||
"oof_shard": "1" | ||
}'); | ||
|
||
-- get test value | ||
SELECT * FROM orders; | ||
|
||
SELECT * FROM orders WHERE id = 1; |