| 91 | } |
| 92 | |
| 93 | pub async fn setup_store(config: &config::Config) -> Result<store::Store, handle_errors::Error> { |
| 94 | let store = store::Store::new(&format!( |
| 95 | "postgres://{}:{}@{}:{}/{}", |
| 96 | config.db_user, config.db_password, config.db_host, config.db_port, config.db_name |
| 97 | )) |
| 98 | .await |
| 99 | .map_err(handle_errors::Error::DatabaseQueryError)?; |
| 100 | |
| 101 | sqlx::migrate!() |
| 102 | .run(&store.clone().connection) |
| 103 | .await |
| 104 | .map_err(handle_errors::Error::MigrationError)?; |
| 105 | |
| 106 | let log_filter = format!( |
| 107 | "handle_errors={},rust_web_dev={},warp={}", |
| 108 | config.log_level, config.log_level, config.log_level |
| 109 | ); |
| 110 | |
| 111 | tracing_subscriber::fmt() |
| 112 | // Use the filter we built above to determine which traces to record. |
| 113 | .with_env_filter(log_filter) |
| 114 | // Record an event when each span closes. This can be used to time our |
| 115 | // routes' durations! |
| 116 | .with_span_events(FmtSpan::CLOSE) |
| 117 | .init(); |
| 118 | |
| 119 | Ok(store) |
| 120 | } |
| 121 | |
| 122 | pub async fn run(config: config::Config, store: store::Store) { |
| 123 | let routes = build_routes(store).await; |