Using rocket api a library for another application #2357
-
I want to create an application written in GTK rust and an API that is written in rocket. Can i somehow bundle the API with the GTK application, so that the server can be starter before the application and then the UI uses it (network calls on localhost) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
There are a variety of ways to accomplish this, but one way is to just pass an instance of Rocket off to Tokio via 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#example This comes with another caveat that you'd need to bundle (or embed) your Rocket.toml file with your binary (or manually define your Config). See: https://api.rocket.rs/v0.5-rc/rocket/config/struct.Config.html#method.figment |
Beta Was this translation helpful? Give feedback.
-
There's also the new rocket::execute function (released in rc.2), which makes it possible to start a Rocket instance inside another application, without constructing a custom Tokio runtime. For usage examples, see: |
Beta Was this translation helpful? Give feedback.
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:
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#…