actix-admin/docs/content/getting_started.md

101 lines
2.5 KiB
Markdown
Raw Normal View History

2022-10-24 19:15:52 +02:00
---
2022-10-24 20:09:59 +02:00
layout: default
2022-10-24 19:15:52 +02:00
---
## Getting Started
2022-11-08 17:10:27 +01:00
* See the [basic example](https://github.com/mgugger/actix-admin/tree/main/examples/basic) and run with ```cargo run```.
2022-10-24 19:15:52 +02:00
## 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" }
```
2022-11-08 17:10:27 +01:00
### Steps
1. Import ActixAdmin in the main.rs and your database models:
2022-10-24 19:15:52 +02:00
```rust
use actix_admin::prelude::*;
2022-11-08 17:10:27 +01:00
```
2022-10-24 19:15:52 +02:00
2022-11-08 17:10:27 +01:00
2. Use the DeriveActixAdminMacros on the Database models to implement required traits:
```rust
2022-10-24 19:15:52 +02:00
#[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
}
2022-11-08 17:10:27 +01:00
```
2022-10-24 19:15:52 +02:00
2022-11-08 17:10:27 +01:00
3. Add ActixAdmin to the actix admin app state
```rust
2022-10-24 19:15:52 +02:00
#[derive(Clone)]
pub struct AppState {
pub db: DatabaseConnection,
pub actix_admin: ActixAdmin,
}
2022-11-08 17:10:27 +01:00
```
2022-10-24 19:15:52 +02:00
2022-11-08 17:10:27 +01:00
4. Implement the ActixAdminAppDataTrait for the AppState
```rust
2022-10-24 19:15:52 +02:00
impl ActixAdminAppDataTrait for AppState {
fn get_db(&self) -> &DatabaseConnection {
&self.db
}
fn get_actix_admin(&self) -> &ActixAdmin {
&self.actix_admin
}
}
2022-11-08 17:10:27 +01:00
```
2022-10-24 19:15:52 +02:00
2022-11-08 17:10:27 +01:00
5. Setup the actix admin configuration and add database models to it in main.rs
```rust
2022-10-24 19:15:52 +02:00
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
}
2022-11-08 17:10:27 +01:00
```
2022-10-24 19:15:52 +02:00
2022-11-08 17:10:27 +01:00
6. Add to the actix app in main.rs
```rust
2022-10-24 19:15:52 +02:00
let opt = ConnectOptions::new("sqlite::memory:".to_owned());
let conn = sea_orm::Database::connect(opt).unwrap();
HttpServer::new(move || {
2022-11-08 17:10:27 +01:00
let actix_admin_builder = create_actix_admin_builder();
let app_state = AppState {
db: conn.clone(),
actix_admin: actix_admin_builder.get_actix_admin(),
};
2022-10-24 19:15:52 +02:00
App::new()
2022-11-08 17:10:27 +01:00
.app_data(web::Data::new(app_state.clone()))
2022-10-24 19:15:52 +02:00
.service(
2022-11-08 17:10:27 +01:00
actix_admin_builder.get_scope::<AppState>()
2022-10-24 19:15:52 +02:00
)
});
```
## Access
The admin interface will be available under /admin/.