Rocket: How to create multiple rockets with different configurations?

Created on 7 Jul 2017  路  2Comments  路  Source: SergioBenitez/Rocket

I need two web-servers with different mount points, routes and settings at all (different port, Connection: keep-alive for one and close for another, and so on). How can I do that?

I think of something like:

  1. Create the first rocket instance.
  2. Create the second rocket instance.
  3. Multiplex them somehow (epoll, or something, or just put them into a tokio)
question

Most helpful comment

Rocket is not asynchronous, so you wouldn't use epoll or tokio with Rocket. Rocket also doesn't allow you to change low-level settings such as whether it sends Connection: close or Connection: keep-alive, though it probably should.

To create to Rocket instances with different configurations, you simply need to spawn one of them in a different thread; multiplexing happens automatically. Here's an example:

use std::thread;
use rocket::config::{Config, Environment};

let config_one = Config::build(Environment::Staging)
    .port(9234)
    .finalize()?;

let config_two = Config::build(Environment::Production)
    .port(8716)
    .finalize()?;

// first instance
thread::spawn(move || {
    rocket::custom(config_one, false)
        .mount("/one", routes![a, b, c])
        .launch();
});

// second instance
rocket::custom(config_two, false)
    .mount("/two", routes![e, f, g])
    .launch();

All 2 comments

Rocket is not asynchronous, so you wouldn't use epoll or tokio with Rocket. Rocket also doesn't allow you to change low-level settings such as whether it sends Connection: close or Connection: keep-alive, though it probably should.

To create to Rocket instances with different configurations, you simply need to spawn one of them in a different thread; multiplexing happens automatically. Here's an example:

use std::thread;
use rocket::config::{Config, Environment};

let config_one = Config::build(Environment::Staging)
    .port(9234)
    .finalize()?;

let config_two = Config::build(Environment::Production)
    .port(8716)
    .finalize()?;

// first instance
thread::spawn(move || {
    rocket::custom(config_one, false)
        .mount("/one", routes![a, b, c])
        .launch();
});

// second instance
rocket::custom(config_two, false)
    .mount("/two", routes![e, f, g])
    .launch();

Hopefully this has been answered. Please comment if it has not and we'll reopen.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lambda-fairy picture lambda-fairy  路  4Comments

marcusball picture marcusball  路  3Comments

lucklove picture lucklove  路  4Comments

denysvitali picture denysvitali  路  3Comments

GoRustafari picture GoRustafari  路  3Comments