Skip to content

Commit

Permalink
Merge pull request #4 from VadimYarovoy/feat/db_setup
Browse files Browse the repository at this point in the history
merge: Database setup
  • Loading branch information
VadimYarovoy authored Sep 1, 2024
2 parents fade6b9 + af73a7d commit 6f263f9
Show file tree
Hide file tree
Showing 9 changed files with 674 additions and 7 deletions.
531 changes: 529 additions & 2 deletions backend/Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ edition = "2021"
[dependencies]
axum = "0.7.5"
color-eyre = "0.6.3"
deadpool-postgres = "0.14.0"
figment = { version = "0.10.19", features = ["env", "toml"] }
serde = { version = "1.0.209", features = ["derive"] }
tokio = { version = "1.40.0", features = ["full"] }
tokio-postgres = "0.7.11"
tower-http = { version = "0.5.2", features = ["trace"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
9 changes: 8 additions & 1 deletion backend/config.toml
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"
3 changes: 2 additions & 1 deletion backend/src/bin/api_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ use backend::AppConfig;
#[tokio::main]
async fn main() -> color_eyre::Result<()> {
tracing_subscriber::fmt::init();

let config: AppConfig = AppConfig::figment().extract()?;
let addr = config.server.bind_addr();
let app = backend::app().await;
let app = backend::app(config).await;
info!("SERVER: Starting");

let listener = tokio::net::TcpListener::bind(&addr).await?;
Expand Down
10 changes: 10 additions & 0 deletions backend/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct ServerConfig {
#[derive(Deserialize, Debug)]
pub struct AppConfig {
pub server: ServerConfig,
pub db: DbConfig,
}

impl ServerConfig {
Expand All @@ -21,6 +22,15 @@ impl ServerConfig {
}
}

#[derive(Deserialize, Debug)]
pub struct DbConfig {
pub host: String,
pub port: u16,
pub database: String,
pub user: String,
pub password: String,
}

impl AppConfig {
pub fn figment() -> Figment {
Figment::new()
Expand Down
44 changes: 42 additions & 2 deletions backend/src/server.rs
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)),
)
}
21 changes: 20 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,26 @@ services:
context: .
environment:
- APP_SERVER_PORT=3000
- APP_DB_HOST="psql"
- APP_DB_PORT=5432
- APP_DB_DATABASE="wborders"
- APP_DB_USER="postgres"
- APP_DB_PASSWORD="postgres"
expose:
- "3000"
ports:
- "3000:3000"
- "3000:3000"
psql:
image: postgres:latest
container_name: psql
expose:
- "5432"
ports:
- "5432:5432"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: wborders
volumes:
- ./migrations:/docker-entrypoint-initdb.d
restart: always
4 changes: 4 additions & 0 deletions migrations/init.sql
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
);
56 changes: 56 additions & 0 deletions sql/queries.sql
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;

0 comments on commit 6f263f9

Please sign in to comment.