Skip to content

Commit

Permalink
chore: adding some way to read the env variable and run the app with …
Browse files Browse the repository at this point in the history
…the setted port
  • Loading branch information
igorvieira committed Apr 6, 2024
1 parent 7bf488b commit 58163f3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
model::NoteModel,
schema::{CreateNoteSchema, FilterOptions, UpdateNoteSchema},
schema::{ CreateNoteSchema, FilterOptions, UpdateNoteSchema },
AppState,
};
use actix_web::{delete, get, patch, post, web, HttpResponse, Responder};
Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use actix_web::middleware::Logger;
use actix_web::{ http::header, web, App, HttpServer };
use dotenv::dotenv;
use sqlx::{ postgres::PgPoolOptions, Pool, Postgres };
use std::env;

pub struct AppState {
db: Pool<Postgres>,
Expand All @@ -20,6 +21,11 @@ async fn main() -> std::io::Result<()> {
dotenv().ok();
env_logger::init();

let port: u16 = env::var("PORT")
.unwrap_or_else(|_| "8080".to_string())
.parse()
.expect("PORT must be a number");

let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let pool = match PgPoolOptions::new().max_connections(10).connect(&database_url).await {
Ok(pool) => {
Expand All @@ -46,6 +52,6 @@ async fn main() -> std::io::Result<()> {
.wrap(cors)
.wrap(Logger::default())
})
.bind(("127.0.0.1", 8000))?
.bind(("127.0.0.1", port))?
.run().await
}

0 comments on commit 58163f3

Please sign in to comment.