-5

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.
  • 1
    Did you read this https://rocket.rs/v0.4/guide/configuration/ ? – Peterrabbit Jun 27 '23 at 16:12
  • I just wish to choose the execution port, like 4444, etc, because the rocket default port is 8000 and i have another app running in that. – WiseHipoppotamus Jun 27 '23 at 17:02
  • 1
    And you don't want to read the documentation to find out how to do that? – aled Jun 27 '23 at 18:20
  • I'm going to read thank you, but, like, i thought there were people here that are used to do this and could help me quickly, i thought too that it was simple like adding some func like .port() - But thanks anyway – WiseHipoppotamus Jun 27 '23 at 19:05
  • @WiseHipoppotamus the documentation link I gave is straightforward and very clear, I coundn't answer better than this, you can easily bind the port you like with the configuration file. Just read, the answer is just there. – Peterrabbit Jun 27 '23 at 19:45
  • Hey @Peterrabbit, thank you very much. I've read the doc and I fixed it using a Rocket.toml. I'm sorry for this kind of question; I just thought that it was a matter of a function like port() or setPort(). Thanks for the help. I'll read everything about this doc. – WiseHipoppotamus Jun 27 '23 at 19:49

1 Answers1

0

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.