Skip to main content

actix_web_admin/
lib.rs

1//! # actix-web-admin
2//!
3//! A powerful admin panel library for Actix-web 4 with a pluggable authentication system.
4//!
5//! ## Quick Start
6//!
7//! ```rust,no_run
8//! use actix_web::{web, App, HttpServer};
9//! use actix_session::storage::CookieSessionStore;
10//! use actix_session::SessionMiddleware;
11//! use actix_web_admin::auth::{UserStore, JsonUserStore};
12//! use actix_web_admin::{AdminRegistry, AdminSite, init_templates};
13//! use std::sync::Arc;
14//!
15//! #[actix_web::main]
16//! async fn main() -> std::io::Result<()> {
17//!     let templates = init_templates();
18//!
19//!     let store = Arc::new(JsonUserStore::new("users.json"));
20//!     if store.find_by_username("admin").await.unwrap().is_none() {
21//!         store.create_user("admin", "admin@example.com", "Admin", "admin", true).await.unwrap();
22//!     }
23//!
24//!     HttpServer::new(move || {
25//!         let mut registry = AdminRegistry::new();
26//!         // Register your resources...
27//!
28//!         App::new()
29//!             .app_data(web::Data::new(templates.clone()))
30//!             .app_data(web::Data::new(store.clone() as Arc<dyn UserStore>))
31//!             .wrap(SessionMiddleware::new(CookieSessionStore::default(), actix_web::cookie::Key::generate()))
32//!             .configure(|cfg| {
33//!                 AdminSite::new("/admin")
34//!                     .with_user_store(store.clone())
35//!                     .mount(cfg, registry)
36//!             })
37//!     })
38//!     .bind(("127.0.0.1", 8080))?
39//!     .run()
40//!     .await
41//! }
42//! ```
43
44pub mod auth;
45pub mod cli;
46pub mod resource;
47pub mod registry;
48pub mod site;
49pub mod types;
50pub mod handlers;
51
52pub use resource::AdminResource;
53pub use registry::AdminRegistry;
54pub use site::AdminSite;
55
56use tera::Tera;
57use std::sync::Arc;
58
59/// Helper to create a Tera instance with embedded templates.
60pub fn init_templates() -> Tera {
61    let mut tera = Tera::default();
62
63    tera.add_raw_template("base.html", include_str!("templates/base.html")).unwrap();
64    tera.add_raw_template("login.html", include_str!("templates/login.html")).unwrap();
65    tera.add_raw_template("dashboard.html", include_str!("templates/dashboard.html")).unwrap();
66    tera.add_raw_template("list.html", include_str!("templates/list.html")).unwrap();
67    tera.add_raw_template("form.html", include_str!("templates/form.html")).unwrap();
68
69    tera
70}
71
72pub type AdminTemplates = Arc<Tera>;