How can i change the port running rocket? The default port is 8000 but it's already busy in my laptop, i wish to change. My Cargo.toml :
[package]
name = "hello_world"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rocket = "0.5.0-rc.3"
And my main.rs is:
#[macro_use]
extern crate rocket;
#[get("/<name>/<age>")]
fn hello(name: &str, age: u8) -> String {
format!("Hello, {} year old named {}!", age, name)
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/hello", routes![hello])
}
And i'm getting this error:
Error: Rocket failed to bind network socket to given address/port.
People here made me read the doc, and it was easy to solve it just by creating a Rocket.toml, specifying the port on code or set directly, like this:
The following code I made by myself is a little different from the code in the doc, but it works perfectly:
#[macro_use]
extern crate rocket;
#[get("/")]
fn hello() -> &'static str {
"Hello, World!"
}
#[launch]
fn rocket() -> _ {
rocket::build()
.configure(rocket::Config::figment().merge(("port", 9797)))
.mount("/", routes![hello])
}
You can set the ROCKET_PORT Env directly like this:
ROCKET_PORT=3721 ./your_application
Or you can create a Rocket.toml like this:
[development]
address = "localhost"
port = 9090
workers = 4
keep_alive = 5
read_timeout = 5
write_timeout = 5
log = "normal"
secret_key = "randomly generated at launch"
limits = { forms = 32768 }
[staging]
address = "localhost"
port = 9090
workers = 4
keep_alive = 5
read_timeout = 5
write_timeout = 5
log = "normal"
secret_key = "randomly generated at launch"
limits = { forms = 32768 }
[production]
address = "localhost"
port = 9090
workers = 4
keep_alive = 5
read_timeout = 5
write_timeout = 5
log = "critical"
secret_key = "randomly generated at launch"
limits = { forms = 32768 }
and then you can use the environment dep and then parse the Rocket.toml info in the rocket func.