Skip to content

Commit 58163f3

Browse files
committed
chore: adding some way to read the env variable and run the app with the setted port
1 parent 7bf488b commit 58163f3

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
model::NoteModel,
3-
schema::{CreateNoteSchema, FilterOptions, UpdateNoteSchema},
3+
schema::{ CreateNoteSchema, FilterOptions, UpdateNoteSchema },
44
AppState,
55
};
66
use actix_web::{delete, get, patch, post, web, HttpResponse, Responder};

src/main.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use actix_web::middleware::Logger;
77
use actix_web::{ http::header, web, App, HttpServer };
88
use dotenv::dotenv;
99
use sqlx::{ postgres::PgPoolOptions, Pool, Postgres };
10+
use std::env;
1011

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

24+
let port: u16 = env::var("PORT")
25+
.unwrap_or_else(|_| "8080".to_string())
26+
.parse()
27+
.expect("PORT must be a number");
28+
2329
let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
2430
let pool = match PgPoolOptions::new().max_connections(10).connect(&database_url).await {
2531
Ok(pool) => {
@@ -46,6 +52,6 @@ async fn main() -> std::io::Result<()> {
4652
.wrap(cors)
4753
.wrap(Logger::default())
4854
})
49-
.bind(("127.0.0.1", 8000))?
55+
.bind(("127.0.0.1", port))?
5056
.run().await
5157
}

0 commit comments

Comments
 (0)