2.5 KiB
2.5 KiB
layout |
---|
default |
Getting Started
- See the basic example and run with
cargo run
.
Quick overview
Required dependencies in Cargo.toml
sea-orm = { version = "^0.9.1", features = [ "sqlx-sqlite", "runtime-actix-native-tls", "macros" ], default-features = true }
actix_admin = { version = "^0.2.0" }
Steps
- Import ActixAdmin in the main.rs and your database models:
use actix_admin::prelude::*;
- Use the DeriveActixAdminMacros on the Database models to implement required traits:
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize,
DeriveActixAdmin, DeriveActixAdminModel, DeriveActixAdminViewModel
)]
#[sea_orm(table_name = "comment")]
pub struct Model {
#[sea_orm(primary_key)]
#[serde(skip_deserializing)]
#[actix_admin(primary_key)]
pub id: i32,
pub comment: String
}
- Add ActixAdmin to the actix admin app state
#[derive(Clone)]
pub struct AppState {
pub db: DatabaseConnection,
pub actix_admin: ActixAdmin,
}
- Implement the ActixAdminAppDataTrait for the AppState
impl ActixAdminAppDataTrait for AppState {
fn get_db(&self) -> &DatabaseConnection {
&self.db
}
fn get_actix_admin(&self) -> &ActixAdmin {
&self.actix_admin
}
}
- Setup the actix admin configuration and add database models to it in main.rs
pub fn create_actix_admin_builder() -> ActixAdminBuilder {
let comment_view_model = ActixAdminViewModel::from(Entity);
let configuration = ActixAdminConfiguration {
enable_auth: false,
user_is_logged_in: None,
login_link: None,
logout_link: None,
};
let mut admin_builder = ActixAdminBuilder::new(configuration);
admin_builder.add_entity::<AppState, Entity>(&comment_view_model);
admin_builder
}
- Add to the actix app in main.rs
let opt = ConnectOptions::new("sqlite::memory:".to_owned());
let conn = sea_orm::Database::connect(opt).unwrap();
HttpServer::new(move || {
let actix_admin_builder = create_actix_admin_builder();
let app_state = AppState {
db: conn.clone(),
actix_admin: actix_admin_builder.get_actix_admin(),
};
App::new()
.app_data(web::Data::new(app_state.clone()))
.service(
actix_admin_builder.get_scope::<AppState>()
)
});
Access
The admin interface will be available under /admin/.