-
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.
use config struct to load the env variable into struct
- Loading branch information
Showing
3 changed files
with
43 additions
and
9 deletions.
There are no files selected for viewing
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,3 @@ | ||
DATABASE_URL=postgres://shuttle_test:password@localhost:5432/shuttle | ||
GEOCODING_API_KEY= | ||
GEOCODING_API_KEY= | ||
PORT=8000 |
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,11 @@ | ||
#[derive(clap::Parser, Clone, Debug)] | ||
pub struct Config { | ||
#[clap(long, env)] | ||
pub database_url: String, | ||
|
||
#[clap(long, env)] | ||
pub geocoding_api_key: String, | ||
|
||
#[clap(long, env, default_value = "8000")] | ||
pub port: u16, | ||
} |
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,17 +1,39 @@ | ||
use std::env; | ||
use std::{error::Error, net::SocketAddr}; | ||
|
||
use axum::Router; | ||
use clap::Parser; | ||
use sqlx::{PgPool, Pool, Postgres}; | ||
|
||
mod config; | ||
mod handlers; | ||
fn app(pool: Pool<Postgres>) -> Router { | ||
handlers::router(pool) | ||
|
||
fn app(pool: Pool<Postgres>, geocoding_api_key: String) -> Router { | ||
handlers::router(pool, geocoding_api_key) | ||
} | ||
|
||
#[shuttle_runtime::main] | ||
async fn shuttle_service() -> shuttle_axum::ShuttleAxum { | ||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn Error>> { | ||
// load environment variable from .env file | ||
dotenv::dotenv().ok(); | ||
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set the env variable"); | ||
let pool = PgPool::connect(&database_url).await.unwrap(); | ||
Ok(app(pool).into()) | ||
|
||
// initialize the tracing | ||
tracing_subscriber::fmt().init(); | ||
|
||
// load the env variable into config struct | ||
let config = config::Config::parse(); | ||
tracing::info!("config .env {:?}", config); | ||
|
||
// initialize the database pool | ||
let pool = PgPool::connect(&config.database_url).await.unwrap(); | ||
|
||
// bind an address from the env port | ||
let addr = SocketAddr::from(([0, 0, 0, 0], config.port)); | ||
tracing::info!("listening on {}", addr); | ||
|
||
// start the server | ||
axum::Server::bind(&addr) | ||
.serve(app(pool, config.geocoding_api_key).into_make_service()) | ||
.await | ||
.unwrap(); | ||
Ok(()) | ||
} |