Skip to content

Commit

Permalink
feat(server): migrate DB on start
Browse files Browse the repository at this point in the history
  • Loading branch information
alextes committed Jul 6, 2023
1 parent 8e631f7 commit fa9ccda
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
9 changes: 2 additions & 7 deletions src/bin/server/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@ use crate::attestations::{
get_aggregate_price_interval_attestations, get_price_aggregate,
get_price_interval_attestations, get_price_value_attestations, post_oracle_message,
};
use crate::db::{get_db_pool, DbPool};
use crate::db::DbPool;
use crate::state::AppState;
use axum::{
routing::{get, post},
Router,
};
use std::sync::Arc;

pub async fn get_router() -> Router {
let db_pool = get_db_pool().await;
get_router_with_db_pool(db_pool)
}

fn get_router_with_db_pool(db_pool: DbPool) -> Router {
pub fn get_router_with_db_pool(db_pool: DbPool) -> Router {
let shared_state = Arc::new(AppState { db_pool });
Router::new()
.route(
Expand Down
5 changes: 4 additions & 1 deletion src/bin/server/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::net::SocketAddr;

mod app;
mod attestations;
mod db;
Expand All @@ -8,7 +9,9 @@ mod state;
#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
let app = app::get_router().await;
let db_pool = db::get_db_pool().await;
sqlx::migrate!().run(&db_pool).await.unwrap();
let app = app::get_router_with_db_pool(db_pool);
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
tracing::info!("Listening on {}", addr);
axum::Server::bind(&addr)
Expand Down

0 comments on commit fa9ccda

Please sign in to comment.