{"id":16866010,"url":"https://github.com/lukemathwalker/actix-web-flash-messages","last_synced_at":"2025-04-13T04:14:58.608Z","repository":{"id":38276285,"uuid":"415611659","full_name":"LukeMathWalker/actix-web-flash-messages","owner":"LukeMathWalker","description":"A (flash) message framework for actix-web. A port to Rust of Django's message framework.","archived":false,"fork":false,"pushed_at":"2024-08-26T13:44:24.000Z","size":64,"stargazers_count":48,"open_issues_count":0,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-13T04:14:44.713Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LukeMathWalker.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-10-10T14:37:55.000Z","updated_at":"2025-01-26T18:49:02.000Z","dependencies_parsed_at":"2024-10-25T18:41:42.527Z","dependency_job_id":"20eab52c-725a-4ad4-af9d-fee16aab58f7","html_url":"https://github.com/LukeMathWalker/actix-web-flash-messages","commit_stats":{"total_commits":44,"total_committers":8,"mean_commits":5.5,"dds":"0.34090909090909094","last_synced_commit":"00a17471eb18f74e047d6159409a473a194da86c"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LukeMathWalker%2Factix-web-flash-messages","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LukeMathWalker%2Factix-web-flash-messages/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LukeMathWalker%2Factix-web-flash-messages/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LukeMathWalker%2Factix-web-flash-messages/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LukeMathWalker","download_url":"https://codeload.github.com/LukeMathWalker/actix-web-flash-messages/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248661718,"owners_count":21141451,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-13T14:49:10.411Z","updated_at":"2025-04-13T04:14:58.587Z","avatar_url":"https://github.com/LukeMathWalker.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003eactix-web-flash-messages\u003c/h1\u003e\n\u003cdiv align=\"center\"\u003e\n \u003cstrong\u003e\n   Flash messages for \u003ci\u003eactix-web\u003c/i\u003e\n \u003c/strong\u003e\n\u003c/div\u003e\n\n\u003cbr /\u003e\n\n\u003cdiv align=\"center\"\u003e\n  \u003c!-- Crates version --\u003e\n  \u003ca href=\"https://crates.io/crates/actix-web-flash-messages\"\u003e\n    \u003cimg src=\"https://img.shields.io/crates/v/actix-web-flash-messages.svg?style=flat-square\"\n    alt=\"Crates.io version\" /\u003e\n  \u003c/a\u003e\n  \u003c!-- Downloads --\u003e\n  \u003ca href=\"https://crates.io/crates/actix-web-flash-messages\"\u003e\n    \u003cimg src=\"https://img.shields.io/crates/d/actix-web-flash-messages.svg?style=flat-square\"\n      alt=\"Download\" /\u003e\n  \u003c/a\u003e\n  \u003c!-- docs.rs docs --\u003e\n  \u003ca href=\"https://docs.rs/actix-web-flash-messages\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/docs-latest-blue.svg?style=flat-square\"\n      alt=\"docs.rs docs\" /\u003e\n  \u003c/a\u003e\n\u003c/div\u003e\n\u003cbr/\u003e\n\nWeb applications sometimes need to show a **one-time notification** to the user - e.g. an error message after having failed to login.  \nThese notifications are commonly called **flash messages**.\n\n`actix-web-flash-messages` provides a framework to work with flash messages in `actix-web`, closely modeled after [Django's message framework](https://docs.djangoproject.com/en/3.2/ref/contrib/messages/#module-django.contrib.messages).\n\n```rust\nuse actix_web::{Responder, HttpResponse, get,http};\nuse actix_web_flash_messages::{\n    FlashMessage, IncomingFlashMessages,\n};\nuse std::fmt::Write;\n\n/// Attach two flash messages to the outgoing response,\n/// a redirect.\n#[get(\"/set\")]\nasync fn set() -\u003e impl Responder {\n    FlashMessage::info(\"Hey there!\").send();\n    FlashMessage::debug(\"How is it going?\").send();\n    // Redirect to /show\n    HttpResponse::TemporaryRedirect()\n        .insert_header((http::header::LOCATION, \"/show\"))\n        .finish()\n}\n\n/// Pick up the flash messages attached to the request, showing\n/// them to the user via the request body.\n#[get(\"/show\")]\nasync fn show(messages: IncomingFlashMessages) -\u003e impl Responder {\n    let mut body = String::new();\n    for message in messages.iter() {\n        writeln!(body, \"{} - {}\", message.content(), message.level()).unwrap();\n    }\n    HttpResponse::Ok().body(body)\n}\n```\n\n## How to install\n\nAdd `actix-web-flash-messages` to your dependencies:\n\n```toml\n[dependencies]\n# ...\nactix-web = \"4\"\nactix-web-flash-messages = \"0.4\"\n```\n\nBy default, `actix-web-flash-messages` does not provide any storage backend to receive and send flash messages.  \nYou can enable:\n\n- a cookie-based one, [`storage::CookieMessageStore`], using the `cookies` feature flag. The cookie store uses a signed cookie to store and retrieve messages;\n\n```toml\n[dependencies]\n# ...\nactix-web-flash-messages = { version = \"0.4\", features = [\"cookies\"] }\n```\n\n- a session-based one, [`storage::SessionMessageStore`], using the `sessions` feature flag. The session store attaches flash messages to the current session.\n\n```toml\n[dependencies]\n# ...\nactix-web-flash-messages = { version = \"0.4\", features = [\"sessions\"] }\n```\n\nYou can provide a different message store by implementing the [`storage::FlashMessageStore`] trait.\n\n## Examples\n\nYou can find examples of application using `actix-web-flash-messages` on GitHub:  \n\n- [cookies](https://github.com/LukeMathWalker/actix-web-flash-messages/tree/main/examples/cookies);\n- [cookie-based sessions](https://github.com/LukeMathWalker/actix-web-flash-messages/tree/main/examples/session-cookie);\n- [Redis-based sessions](https://github.com/LukeMathWalker/actix-web-flash-messages/tree/main/examples/session-redis).\n\n## The Structure of a Flash Message\n\n[`FlashMessage`]s are made of a [`Level`] and a string of content.\n\nThe message level can be used for filtering and rendering - for example:\n\n- Only show flash messages at `info` level or above in a production environment, while retaining `debug` level messages for local development; \n- Use different colours, in the UI, to display messages (e.g. red for errors, orange for warnings, etc.);\n\nYou can build a [`FlashMessage`] via [`FlashMessage::new`] by specifying its content and [`Level`].  \nYou can also use the shorter level-based constructors - e.g. [`FlashMessage::info`].\n\n## Enabling Flash Messages\n\nTo start sending and receiving flash messages you need to register [`FlashMessagesFramework`] as a middleware on your `actix_web`'s `App`:  \n\n```rust\nuse actix_web_flash_messages::{FlashMessagesFramework, storage::CookieMessageStore};\nuse actix_web::{HttpServer, App, web};\nuse actix_web::cookie::Key;\n\n#[actix_web::main]\nasync fn main() {\n    let signing_key = Key::generate(); // This will usually come from configuration!\n    let message_store = CookieMessageStore::builder(signing_key).build();\n    let message_framework = FlashMessagesFramework::builder(message_store).build();\n    \n    HttpServer::new(move || {\n        App::new()\n            .wrap(message_framework.clone())\n            // [...] your endpoints\n    })\n}\n```\n\nYou will then be able to:\n\n- extract [`FlashMessage`]s from incoming requests using the [`IncomingFlashMessages`] extractor;\n- send [`FlashMessage`]s alongside the outgoing response using [`FlashMessage::send`].\n\n```rust\nuse actix_web::{Responder, HttpResponse, get};\nuse actix_web_flash_messages::{\n    FlashMessage, IncomingFlashMessages,\n};\n\n/// Send a flash messages alongside the outgoing response, a redirect.\n#[get(\"/set\")]\nasync fn set() -\u003e impl Responder {\n    FlashMessage::info(\"Hey there!\").send();\n    // [...]\n}\n\n/// Extract the flash message from the incoming request.\n#[get(\"/show\")]\nasync fn show(_messages: IncomingFlashMessages) -\u003e impl Responder {\n    // [...]\n}\n```\n\n## Framework Configuration\n\nThere are a few knobs that you can tweak when it comes to [`FlashMessagesFramework`].  \nUse [`FlashMessagesFramework::builder`] to get access to its fluent configuration API, built around [`FlashMessagesFrameworkBuilder`].\n\n### Minimum Level\n\nBy default, [`FlashMessagesFramework`] will only dispatch messages at `info`-level or above, discarding `debug`-level messages.  \nYou can change this setting using [`FlashMessagesFrameworkBuilder::minimum_level`].\n\n```rust\nuse actix_web_flash_messages::{FlashMessagesFramework, Level, storage::CookieMessageStore};\nuse actix_web::{HttpServer, App, web};\n\nfn get_message_store() -\u003e CookieMessageStore {\n    // [...]\n    # CookieMessageStore::builder(actix_web::cookie::Key::generate()).build()\n}\n\n#[actix_web::main]\nasync fn main() {\n    // Show debug-level messages when developing locally\n    let minimum_level = match std::env::var(\"APP_ENV\") {\n        Ok(s) if \u0026s == \"local\" =\u003e Level::Debug,\n        _ =\u003e Level::Info,\n    };\n    let message_framework = FlashMessagesFramework::builder(get_message_store())\n        .minimum_level(minimum_level)\n        .build();\n\n    HttpServer::new(move || {\n        App::new()\n            .wrap(message_framework.clone())\n            // [...] Your endpoints\n    })\n}\n```\n\n### Message Storage\n\n`actix-web-flash-messages` provides a cookie-based implementation of flash messages, [`storage::CookieMessageStore`], using a signed cookie to store and retrieve messages.  \nYou can provide a different message store by implementing the [`storage::FlashMessageStore`] trait.\n\n## License \n\nLicensed under either of Apache License, Version 2.0 or MIT license at your option. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.\n\n[`FlashMessage`]: https://docs.rs/actix-web-flash-messages/latest/actix_web_flash_messages/struct.FlashMessage.html\n[`Level`]: https://docs.rs/actix-web-flash-messages/latest/actix_web_flash_messages/enum.Level.html\n[`FlashMessage::new`]: https://docs.rs/actix-web-flash-messages/latest/actix_web_flash_messages/struct.FlashMessage.html#method.new\n[`FlashMessage::info`]: https://docs.rs/actix-web-flash-messages/latest/actix_web_flash_messages/struct.FlashMessage.html#method.info\n[`FlashMessage::send`]: https://docs.rs/actix-web-flash-messages/latest/actix_web_flash_messages/struct.FlashMessage.html#method.send\n[`FlashMessagesFramework`]: https://docs.rs/actix-web-flash-messages/latest/actix_web_flash_messages/struct.FlashMessagesFramework.html\n[`FlashMessagesFrameworkBuilder::minimum_level`]: https://docs.rs/actix-web-flash-messages/latest/actix_web_flash_messages/struct.FlashMessagesFramework.html#method.minimum_level\n[`FlashMessagesFramework::builder`]: https://docs.rs/actix-web-flash-messages/latest/actix_web_flash_messages/struct.FlashMessagesFramework.html#method.builder\n[`FlashMessagesFrameworkBuilder`]: https://docs.rs/actix-web-flash-messages/latest/actix_web_flash_messages/struct.FlashMessagesFrameworkBuilder.html\n[`IncomingFlashMessages`]: https://docs.rs/actix-web-flash-messages/latest/actix_web_flash_messages/struct.IncomingFlashMessages.html\n[`storage::CookieMessageStore`]: https://docs.rs/actix-web-flash-messages/latest/actix_web_flash_messages/storage/struct.CookieMessageStore.html\n[`storage::FlashMessageStore`]: https://docs.rs/actix-web-flash-messages/latest/actix_web_flash_messages/storage/struct.FlashMessageStore.html\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flukemathwalker%2Factix-web-flash-messages","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flukemathwalker%2Factix-web-flash-messages","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flukemathwalker%2Factix-web-flash-messages/lists"}