Go to file
2022-10-24 19:31:49 +02:00
actix_admin_macros export lib crates in prelude to use in macro 2022-10-24 18:42:51 +02:00
docs remove old theme 2022-10-24 19:31:49 +02:00
example export lib crates in prelude to use in macro 2022-10-24 18:42:51 +02:00
src export lib crates in prelude to use in macro 2022-10-24 18:42:51 +02:00
static add initial crate documentation 2022-09-02 17:06:13 +02:00
templates add custom handlers and add them optionally to the menu 2022-10-11 18:54:45 +02:00
tests add custom handlers and add them optionally to the menu 2022-10-11 18:54:45 +02:00
.gitignore add jekyll docs 2022-10-24 19:15:52 +02:00
Cargo.toml bump to v0.2.0 2022-10-11 19:05:24 +02:00
README.md add custom handlers and add them optionally to the menu 2022-10-11 18:54:45 +02:00

Actix Admin

The actix-admin crate aims at creating a web admin interface similar to other admin interfaces (such as flask-admin in python).

Getting Started

Features

  1. Async: Builds on sea-orm for the database backend
  2. Macros: Generate the required implementations for models automatically
  3. Authentication: optionally pass authentication handler to implement authentication for views
  4. Supports custom validation rules
  5. Searchable attributes can be specified
  6. Supports custom views which are added to the Navbar

Screenshot

Quick overview

Required dependencies

itertools = "0.10.3" sea-orm = { version = "^0.9.1", features = [ "sqlx-sqlite", "runtime-actix-native-tls", "macros" ], default-features = true } actix_admin = { version = "^0.1.0" }

See inlined steps

use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
use actix_admin::prelude::*;
use actix_web::web;
use actix_web::App;
use actix_web::HttpServer;
use sea_orm::entity::prelude::*;
use sea_orm::entity::prelude::*;
use actix_admin::prelude::*;
// 1. Import ActixAdmin
use actix_admin::prelude::*;

// 2. Use DeriveActixAmin* Macros to implement the traits for the model
#[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
}
impl ActixAdminModelValidationTrait<ActiveModel> for Entity {}
impl ActiveModelBehavior for ActiveModel {}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation { }

// 3. Add actix-admin to the AppState
#[derive(Clone)]
pub struct AppState {
 pub db: DatabaseConnection,
 pub actix_admin: ActixAdmin,
}

// 4. 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
    }
}

// 5. Setup the actix admin configuration
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
}

// 6. Add to the actix app
let actix_admin = create_actix_admin_builder().get_actix_admin();
//let opt = ConnectOptions::new("sqlite::memory:".to_owned());
//let conn = sea_orm::Database::connect(opt).unwrap();
//let app_state = AppState {
//    db: conn,
//    actix_admin: actix_admin,
//};

HttpServer::new(move || {
    App::new()
        //.app_data(web::Data::new(app_state.clone()))
        .service(
            create_actix_admin_builder().get_scope::<AppState>()
        )
});

Access

The admin interface will be available under /admin/.