From e57e758834245aa568d77f994939e837876f7d72 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Tue, 4 Feb 2025 01:29:25 -0800 Subject: [PATCH] Run on any port --- README.md | 17 +++++++++++++---- justfile | 4 ++-- src/main.rs | 5 +++++ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0e4bf75..3d200ca 100644 --- a/README.md +++ b/README.md @@ -65,14 +65,23 @@ To set up a development environment: 4. Build and run the application from source: ```bash - # Run with a persistent database + # Run with a persistent database on default port (8000) cargo run -- -d rss-reader.db - # Or try it out in demo mode (uses in-memory database) - cargo run -- --demo + # Run on a specific port + cargo run -- -d rss-reader.db -p 3000 + + # Run in demo mode with custom port + cargo run -- --demo -p 8080 ``` -The application will be available at `http://localhost:8000`. +The application will be available at `http://localhost:` (default port is 8000). + +### Command Line Options + +- `-d, --database ` - Path to the SQLite database file (required unless in demo mode) +- `--demo` - Run in demo mode with an in-memory database +- `-p, --port ` - Port to listen on (default: 8000) ### Running in Production diff --git a/justfile b/justfile index 8d4bf6e..59837ff 100644 --- a/justfile +++ b/justfile @@ -4,8 +4,8 @@ _default: export SECRET_KEY := "MHSePvm1msyOkYuJ7u+MtyJYCzgdHCS7QNvrk9ts+rI=" [doc("Run the reader locally in demo mode.")] # don't re-use this secret key -run-local-demo: - cargo run -- --demo +run-local-demo *args: + cargo run -- --demo {{args}} sqlx-prepare: DATABASE_URL="sqlite:data.sqlite" cargo sqlx prepare diff --git a/src/main.rs b/src/main.rs index 265b453..6ee6df3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,6 +28,10 @@ struct Args { /// Run in demo mode with an in-memory database #[arg(long)] demo: bool, + + /// Port to listen on + #[arg(short, long, default_value_t = 8000)] + port: u16, } #[derive(Database)] @@ -101,6 +105,7 @@ fn rocket() -> _ { let figment = rocket::Config::figment() .merge(("databases.rss_data.url", db_url)) + .merge(("port", args.port)) .merge(( "secret_key", std::env::var("SECRET_KEY").expect("SECRET_KEY environment variable must be set"),