Skip to content

Using rocket api a library for another application #2357

Discussion options

You must be logged in to vote

There are a variety of ways to accomplish this, but one way is to just pass an instance of Rocket off to Tokio via tokio::spawn. You would need to be using a custom runtime (or a default Tokio runtime), as opposed to using the Rocket-provided macros (#[rocket::main] or #[launch]).

An example could be:

#[tokio::main]
async fn main() {
    tokio::spawn(rocket::build().mount("/", routes![index]).launch());

    loop {
        // ... your main app loop
    }
}

There are some considerations you must make of course: you lose out on some things provided by Rocket by using a custom runtime (such as graceful shutdown handling). See: https://api.rocket.rs/v0.5-rc/rocket/config/struct.Shutdown.html#…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by sahilarora3117
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants