Skip to content

Commit 03c21d2

Browse files
committed
feat/configurable-port
1 parent e35705b commit 03c21d2

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
PORT=8080
2+
13
MONGODB_URI=mongodb+srv://<<username>>:<<password>>@clusterX.*****.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0 # mongodb://192.168.8.136:27017 on local
24
DB_NAME_TEST=selfsend_test
35
DB_NAME_PRODUCTION=selfsend_production
46
DB_DISPOSABLE_EMAILS_COLLECTION=disposable_email_domains
57

68
# Redis
79
REDIS_URL=redis://127.0.0.1:6379
10+
TEST_REDIS_URL=redis://***.redns.redis-cloud.com:11594
811
REDIS_CACHE_TTL=86400 # 1 day in seconds

src/main.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use actix_web::{App, HttpServer, web::Data};
22
use email_sanitizer::graphql::schema::create_schema;
33
use email_sanitizer::openapi::ApiDoc;
44
use email_sanitizer::routes::email::RedisCache;
5+
use std::env::VarError;
56
use utoipa::OpenApi;
67
use utoipa_swagger_ui::SwaggerUi;
78

@@ -21,7 +22,7 @@ use utoipa_swagger_ui::SwaggerUi;
2122
/// - OpenAPI spec: `/api-docs/openapi.json`
2223
///
2324
/// # Configuration
24-
/// - Server binds to `127.0.0.1:8080` by default
25+
/// - Server binds to `127.0.0.1:8080` by default. Port can be specified as an env variable named "PORT".
2526
/// - Environment variables loaded from `.env` file (if present)
2627
/// - Redis URL from REDIS_URL environment variable (defaults to localhost:6379)
2728
/// - Redis cache TTL from REDIS_CACHE_TTL environment variable (defaults to 86400 seconds/24 hours)
@@ -43,6 +44,18 @@ async fn main() -> std::io::Result<()> {
4344
// Create GraphQL schema
4445
let schema = create_schema();
4546

47+
let port: Result<String, VarError> = std::env::var("PORT");
48+
let port = match port {
49+
Ok(v) => v,
50+
Err(e) => {
51+
eprintln!(
52+
"Error reading PORT environment variable: {}, binding to 8080",
53+
e
54+
);
55+
"8080".to_string()
56+
}
57+
};
58+
4659
HttpServer::new(move || {
4760
let openapi = ApiDoc::openapi();
4861

@@ -53,7 +66,10 @@ async fn main() -> std::io::Result<()> {
5366
.configure(email_sanitizer::routes::configure)
5467
.service(SwaggerUi::new("/swagger-ui/{_:.*}").url("/api-docs/openapi.json", openapi))
5568
})
56-
.bind(("127.0.0.1", 8080))?
69+
.bind((
70+
"127.0.0.1",
71+
port.parse::<u16>().expect("Failed to parse port"),
72+
))?
5773
.run()
5874
.await
5975
}

0 commit comments

Comments
 (0)