Rocket/examples
Sergio Benitez 64e46b7107 Introduce sentinels: auto-discovered launch abort.
Sentinels resolve a long-standing usability and functional correctness
issue in Rocket: starting an application with guards and/or responders
that depend on state that isn't available. The canonical example is the
'State' guard. Prior to this commit, an application with routes that
queried unmanaged state via 'State' would fail at runtime. With this
commit, the application refuses to launch with a detailed error message.

The 'Sentinel' docs explains it as:

    A sentinel, automatically run on ignition, can trigger a launch
    abort should an instance fail to meet arbitrary conditions. Every
    type that appears in a mounted route's type signature is eligible to
    be a sentinel. Of these, those that implement 'Sentinel' have their
    'abort()' method invoked automatically, immediately after ignition,
    once for each unique type. Sentinels inspect the finalized instance
    of 'Rocket' and can trigger a launch abort by returning 'true'.

The following types are now sentinels:

  * 'contrib::databases::Connection' (any '#[database]' type)
  * 'contrib::templates::Metadata'
  * 'contrib::templates::Template'
  * 'core::State'

The following are "specialized" sentinels, which allow sentinel
discovery even through type aliases:

  * 'Option<T>', 'Debug<T>' if 'T: Sentinel'
  * 'Result<T, E>', 'Either<T, E>' if 'T: Sentinel', 'E: Sentinel'

Closes #464.
2021-04-16 01:44:53 -07:00
..
config Introduce statically-enforced 'Rocket' phasing. 2021-04-13 19:26:45 -07:00
cookies Rename 'rocket::ignite()' to 'rocket::build()'. 2021-04-08 01:07:52 -07:00
databases Introduce statically-enforced 'Rocket' phasing. 2021-04-13 19:26:45 -07:00
error-handling Introduce sentinels: auto-discovered launch abort. 2021-04-16 01:44:53 -07:00
fairings Introduce statically-enforced 'Rocket' phasing. 2021-04-13 19:26:45 -07:00
forms Rename 'rocket::ignite()' to 'rocket::build()'. 2021-04-08 01:07:52 -07:00
hello Rename 'rocket::ignite()' to 'rocket::build()'. 2021-04-08 01:07:52 -07:00
manual-routing Remove unnecessary second 'Handler' lifetimes. 2021-04-14 22:17:21 -07:00
pastebin Rename 'rocket::ignite()' to 'rocket::build()'. 2021-04-08 01:07:52 -07:00
responders Rename 'rocket::ignite()' to 'rocket::build()'. 2021-04-08 01:07:52 -07:00
serialization Introduce statically-enforced 'Rocket' phasing. 2021-04-13 19:26:45 -07:00
state Introduce statically-enforced 'Rocket' phasing. 2021-04-13 19:26:45 -07:00
static-files Rename 'rocket::ignite()' to 'rocket::build()'. 2021-04-08 01:07:52 -07:00
templating Add 'Rocket.toml' to templating example. 2021-04-13 18:20:41 -07:00
testing Introduce statically-enforced 'Rocket' phasing. 2021-04-13 19:26:45 -07:00
tls Rename 'rocket::ignite()' to 'rocket::build()'. 2021-04-08 01:07:52 -07:00
todo Introduce statically-enforced 'Rocket' phasing. 2021-04-13 19:26:45 -07:00
uuid Rename 'rocket::ignite()' to 'rocket::build()'. 2021-04-08 01:07:52 -07:00
Cargo.toml Completely revamp, redo examples. 2021-04-07 23:09:05 -07:00
README.md Completely revamp, redo examples. 2021-04-07 23:09:05 -07:00

README.md

Rocket Examples

This directory contains projects showcasing Rocket's features.

Applications

  • pastebin

    A simple, API-only pastebin application, similar to https://paste.rs. Stores pastes locally on the file system. Implements a custom parameter guard, PasteId, to parse and validate paste identifiers.

  • todo

    A todo app with a web UI to add, delete, and mark/unmark items. Uses a SQLite database driven by diesel. Runs migrations automatically at start-up. Uses tera to render templates.

Feature Examples

  • config - Illustrates how to extract values from a Rocket Figment, how to store and retrieve an application specific configuration in managed state using AdHoc::config(), and how to set configuration values in Rocket.toml.

  • cookies - Uses cookies to create a client-side message box. Uses private cookies for a session-based authentication.

  • databases - Implements a CRUD-like "blog" JSON API backed by a SQLite database driven by each of sqlx, diesel, and rusqlite. Runs migrations automatically for the former two drivers. Uses contrib database support for the latter two drivers.

  • error-handling - Exhibits the use of scoped catchers; contains commented out lines that will cause a launch-time error with code to custom-display the error.

  • fairings - Exemplifies creating a custom Counter fairing and using AdHoc fairings.

  • forms - Showcases all of Rocket's form support features including multipart file uploads, ad-hoc validations, field renaming, and use of form context for staged forms.

  • hello - Basic example of Rocket's core features: route declaration with path and query parameters, both simple and compound, mounting, launching, testing, and returning simple responses. Also showcases using UTF-8 in route declarations and responses.

  • manual-routing - An example eschewing Rocket's codegen in favor of manual routing. This should be seen as last-ditch effort, much like unsafe in Rust, as manual routing also eschews many of Rocket's automatic web security guarantees.

  • responders - Illustrates the use of many of Rocket's built-in responders: Stream, Redirect, File, NamedFile, content for manually setting Content-Types, and Either. In the process, showcases using TempFile for raw uploads. Also illustrates the creation of a custom, derived Responder.

  • serialization - Showcases JSON and MessagePack (de)serialization support in contrib by implementing a CRUD-like message API in JSON and a simply read/echo API in MessagePack.

  • state - Illustrates the use of request-local state and managed state. Uses request-local state to cache "expensive" per-request operations. Uses managed state to implement a simple index hit counter. Also uses managed state to store, retrieve, and push/pop from a concurrent queue.

  • static-files - Uses contrib StaticFiles serve static files. Also creates a second manual yet safe version.

  • templating - Illustrates using contrib templates support with identical examples for handlebars and tera.

  • testing - Uses Rocket's local libraries to test an application. Showcases necessary use of the async Client. Note that all examples contains tests, themselves serving as examples for how to test Rocket applications.

  • tls - Illustrates configuring TLS with a variety of key pair kinds.

  • uuid - Uses UUID support in contrib, converting between contrib::Uuid type and the uuid crate Uuid.