Rust web framework Rocket releases v0.5: supports asynchronous, SSE, WebSockets, etc.

After more than four years, four RC versions, a thousand commits, more than a thousand issues, discussions and PRs, Rocket v0.5 was officially released .

Rocket is an asynchronous backend Rust web framework focused on usability, security, scalability, and performance. It makes writing secure web applications simple without sacrificing productivity or performance.

Some of the important new features of Rocket v0.5 include:

  1. Support for stable Rust compiler : Rocket v0.5 compiles and builds based on the stable version of Rust. Developers can now rustccompile and build Rocket applications in the stable release channel and no longer need to use #![feature(..)]the crate  attribute.
     

    #[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])
    }

     

  2. Support for asynchronous I/O : Rocket v0.5 re-architects the core request processing part to take advantage of the latest asynchronous network features in Rust. Rocket can now automatically multiplex request processing across all available cores on the machine, allowing route handlers to be declared as asynchronous and use await syntax.
     

    use rocket::tokio;
    use rocket::data::{Data, ToByteUnit};
    
    #[post("/debug", data = "<data>")]
    async fn debug(data: Data<'_>) -> std::io::Result<()> {
        // Stream at most 512KiB all of the body data to stdout.
        data.open(512.kibibytes())
            .stream_to(tokio::io::stdout())
            .await?;
    
        Ok(())
    }

     

  3. Support for Sentinels : Rocket v0.5 introduces Sentinels, a feature unique to the Rocket framework. Sentinels provide an automated last line of defense that aborts application launch when invalid conditions occur. Sentinels can be implemented outside of Rocket, and they should be used whenever possible.
     

    use rocket::{Rocket, Ignite, Sentinel};
    
    impl Sentinel for MyResponder {
        fn abort(r: &Rocket<Ignite>) -> bool {
            r.state::<T>().is_none() || !r.catchers().any(|c| c.code == Some(400))
        }
    }

     

  4. Support for streaming and SSE : Rocket v0.5 introduces real-time, typed asynchronous streaming. Rocket's new asynchronous streams section contains more details, and we encourage anyone interested to check out the new live, multi-room chat examples.

  5. Support for WebSockets : Rocket v0.5 introduces support for HTTP connection upgrades through the new upgrade API. This API allows the responder to control raw I/O with the client within an existing HTTP connection, allowing upgrading the HTTP connection to any protocol, including WebSockets.

  6. Comprehensive form function support : Rocket v0.5 has completely improved form support, supporting multi-part uploads, arbitrarily nested collections, instant validation, and more. Rocket's new form protocol allows applications to express any structure, arbitrary nesting and collections without any custom code.

In addition to the above features, Rocket v0.5 also introduces more than 40 other new features and major improvements, including security and privacy headers, graceful shutdown, flexible configuration system, type system-enforced data restrictions, and more.

The release of Rocket v0.5 is an important milestone, providing Rust developers with a powerful and easy-to-use web framework. We encourage all users to upgrade to Rocket v0.5 and check out the full CHANGELOG to learn about all new features and improvements.

Please note that Rocket v0.4 will still receive support and security updates until Rocket v0.6 is released.

Guess you like

Origin www.oschina.net/news/267838/rocketrs-0-5-released