feat: allow to use custom port with a .env file

This commit is contained in:
Mariano Riefolo 2024-08-10 20:37:51 +02:00
parent 7a33ada21e
commit c884eae54c
4 changed files with 16 additions and 1 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/target
database.db
.env

7
Cargo.lock generated
View File

@ -212,6 +212,12 @@ dependencies = [
"subtle",
]
[[package]]
name = "dotenvy"
version = "0.15.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b"
[[package]]
name = "fallible-iterator"
version = "0.3.0"
@ -683,6 +689,7 @@ version = "0.1.0"
dependencies = [
"argon2",
"axum",
"dotenvy",
"rand",
"rusqlite",
"tokio",

View File

@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
argon2 = { version = "0.5.3", features = ["password-hash", "rand"] }
axum = "0.7.5"
dotenvy = "0.15.7"
rand = "0.8.5"
rusqlite = "0.32.1"
tokio = { version = "1.39.2", features = ["full"] }

View File

@ -10,9 +10,15 @@ async fn main() {
let connection = Connection::open("database.db").expect("Failed to open database");
db::init(&connection).expect("Failed to create database");
dotenvy::dotenv().ok();
let app = Router::new();
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
let port = std::env::var("PORT")
.unwrap_or_else(|_| "3000".to_string())
.parse()
.expect("PORT must be an unsigned number");
let addr = SocketAddr::from(([0, 0, 0, 0], port));
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
axum::serve(listener, app).await.unwrap();