derive fields for table columns
This commit is contained in:
parent
b5f55487fa
commit
790cf9ff59
@ -24,6 +24,6 @@ serde_json = "1.0.79"
|
||||
serde_derive = "1.0.136"
|
||||
quote = "1.0"
|
||||
sea-orm = { version = "0.6.0", features = [ "sqlx-sqlite", "runtime-actix-native-tls", "macros" ], default-features = false }
|
||||
|
||||
syn = "1.0.91"
|
||||
actix_admin = { path = "actix_admin" }
|
||||
azure_auth = { path = "azure_auth" }
|
@ -1,21 +1,47 @@
|
||||
use proc_macro;
|
||||
use quote::quote;
|
||||
use proc_macro2::{Span, Ident};
|
||||
use syn::{ DeriveInput };
|
||||
|
||||
mod struct_fields;
|
||||
use struct_fields::get_field_for_tokenstream;
|
||||
|
||||
#[proc_macro_derive(DeriveActixAdminModel)]
|
||||
pub fn derive_crud_fns(_input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let fields = get_field_for_tokenstream(input);
|
||||
|
||||
let names_const_fields_str = fields.iter().map(|(_vis, ident)| {
|
||||
let ident_name = ident.to_string();
|
||||
quote! {
|
||||
#ident_name
|
||||
}
|
||||
});
|
||||
|
||||
let expanded = quote! {
|
||||
use std::convert::From;
|
||||
use async_trait::async_trait;
|
||||
use actix_web::{web, HttpResponse, HttpRequest, Error};
|
||||
use actix_admin::{ ActixAdminModelTrait, ActixAdminViewModelTrait, ActixAdminViewModel, ActixAdminModel, AppDataTrait };
|
||||
use actix_admin::{ ActixAdminField, ActixAdminModelTrait, ActixAdminViewModelTrait, ActixAdminViewModel, ActixAdminModel, AppDataTrait };
|
||||
|
||||
impl From<Entity> for ActixAdminViewModel {
|
||||
fn from(entity: Entity) -> Self {
|
||||
ActixAdminViewModel {
|
||||
entity_name: entity.table_name().to_string()
|
||||
entity_name: entity.table_name().to_string(),
|
||||
fields: Entity::get_fields()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl ActixAdminViewModelTrait for Entity {
|
||||
async fn list<T: AppDataTrait>(req: HttpRequest, data: web::Data<T>) -> Result<HttpResponse, Error> {
|
||||
let db = &data.get_db();
|
||||
let entities = Entity::list_db(db, 1, 5);
|
||||
let entity_names = &data.get_actix_admin().entity_names;
|
||||
let model = ActixAdminViewModel::from(Entity);
|
||||
actix_admin::list_model(req, &data, model, entity_names)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl ActixAdminModelTrait for Entity {
|
||||
@ -33,16 +59,23 @@ pub fn derive_crud_fns(_input: proc_macro::TokenStream) -> proc_macro::TokenStre
|
||||
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl ActixAdminViewModelTrait for Entity {
|
||||
async fn list<T: AppDataTrait>(req: HttpRequest, data: web::Data<T>) -> Result<HttpResponse, Error> {
|
||||
let db = &data.get_db();
|
||||
let entities = Entity::list_db(db, 1, 5);
|
||||
let entity_names = &data.get_actix_admin().entity_names;
|
||||
let model = ActixAdminViewModel::from(Entity);
|
||||
actix_admin::list_model(req, &data, model, entity_names)
|
||||
|
||||
fn get_fields() -> Vec<(&'static str, ActixAdminField)> {
|
||||
let mut vec = Vec::new();
|
||||
let field_names = stringify!(
|
||||
#(#names_const_fields_str),*
|
||||
).split(",")
|
||||
.collect::<Vec<_>>()
|
||||
.into_iter()
|
||||
.for_each( |field_name|
|
||||
vec.push((
|
||||
field_name,
|
||||
// TODO: derive correct AxtixAdminField Value
|
||||
ActixAdminField::Text
|
||||
)
|
||||
)
|
||||
);
|
||||
vec
|
||||
}
|
||||
}
|
||||
};
|
||||
|
54
actix_admin/actix_admin_macros/src/struct_fields.rs
Normal file
54
actix_admin/actix_admin_macros/src/struct_fields.rs
Normal file
@ -0,0 +1,54 @@
|
||||
use proc_macro2::{Span, Ident};
|
||||
use syn::{
|
||||
Attribute, Fields, Meta, NestedMeta, Visibility, DeriveInput
|
||||
};
|
||||
|
||||
const ATTR_META_SKIP: &'static str = "skip";
|
||||
|
||||
pub fn get_field_for_tokenstream(input: proc_macro::TokenStream) -> std::vec::Vec<(syn::Visibility, proc_macro2::Ident)> {
|
||||
let ast: DeriveInput = syn::parse(input).unwrap();
|
||||
let (vis, ty, generics) = (&ast.vis, &ast.ident, &ast.generics);
|
||||
let names_struct_ident = Ident::new(&(ty.to_string() + "FieldStaticStr"), Span::call_site());
|
||||
|
||||
let fields = filter_fields(match ast.data {
|
||||
syn::Data::Struct(ref s) => &s.fields,
|
||||
_ => panic!("FieldNames can only be derived for structs"),
|
||||
});
|
||||
fields
|
||||
}
|
||||
|
||||
pub fn has_skip_attr(attr: &Attribute, path: &'static str) -> bool {
|
||||
if let Ok(Meta::List(meta_list)) = attr.parse_meta() {
|
||||
if meta_list.path.is_ident(path) {
|
||||
for nested_item in meta_list.nested.iter() {
|
||||
if let NestedMeta::Meta(Meta::Path(path)) = nested_item {
|
||||
if path.is_ident(ATTR_META_SKIP) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
pub fn filter_fields(fields: &Fields) -> Vec<(Visibility, Ident)> {
|
||||
fields
|
||||
.iter()
|
||||
.filter_map(|field| {
|
||||
if field
|
||||
.attrs
|
||||
.iter()
|
||||
.find(|attr| has_skip_attr(attr, "struct_field_names"))
|
||||
.is_none()
|
||||
&& field.ident.is_some()
|
||||
{
|
||||
let field_vis = field.vis.clone();
|
||||
let field_ident = field.ident.as_ref().unwrap().clone();
|
||||
Some((field_vis, field_ident))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
}
|
@ -33,7 +33,7 @@ pub struct Params {
|
||||
|
||||
// Fields
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub enum Field {
|
||||
pub enum ActixAdminField {
|
||||
Text,
|
||||
}
|
||||
|
||||
@ -47,22 +47,25 @@ pub trait AppDataTrait {
|
||||
#[async_trait]
|
||||
pub trait ActixAdminModelTrait: Clone {
|
||||
async fn list_db(db: &DatabaseConnection, page: usize, posts_per_page: usize) -> Vec<&str>;
|
||||
fn get_fields() -> Vec<(&'static str, ActixAdminField)>;
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub struct ActixAdminModel {
|
||||
pub fields: Vec<(&'static str, Field)>,
|
||||
|
||||
}
|
||||
|
||||
// ActixAdminViewModel
|
||||
#[async_trait(?Send)]
|
||||
pub trait ActixAdminViewModelTrait {
|
||||
async fn list<T: AppDataTrait + Sync + Send>(req: HttpRequest, data: web::Data<T>) -> Result<HttpResponse, Error>;
|
||||
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub struct ActixAdminViewModel {
|
||||
pub entity_name: String,
|
||||
pub fields: Vec<(&'static str, ActixAdminField)>,
|
||||
}
|
||||
|
||||
// ActixAdminController
|
||||
@ -120,6 +123,7 @@ pub fn list_model<T: AppDataTrait>(req: HttpRequest, data: &web::Data<T>, view_m
|
||||
ctx.insert("posts_per_page", &posts_per_page);
|
||||
ctx.insert("num_pages", "5" /*&num_pages*/);
|
||||
ctx.insert("columns", &columns);
|
||||
ctx.insert("model_fields", &view_model.fields);
|
||||
|
||||
let body = TERA
|
||||
.render("list.html", &ctx)
|
||||
|
@ -1,5 +1,14 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block content %}
|
||||
Hello
|
||||
<table>
|
||||
<tr>
|
||||
{% for model_field in model_fields -%}
|
||||
<th>{{ model_field[0] }}</th>
|
||||
{%- endfor %}
|
||||
</tr>
|
||||
<tr>
|
||||
<td></td>
|
||||
</tr>
|
||||
</table>
|
||||
{% endblock content %}
|
@ -9,9 +9,9 @@ pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
#[serde(skip_deserializing)]
|
||||
pub id: i32,
|
||||
pub title: String,
|
||||
pub comment: String,
|
||||
#[sea_orm(column_type = "Text")]
|
||||
pub text: String,
|
||||
pub user: String,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
|
@ -1,6 +1,8 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use actix_admin::{ DeriveActixAdminModel };
|
||||
use sea_orm::{entity::*, query::*, tests_cfg::cake};
|
||||
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize, DeriveActixAdminModel)]
|
||||
#[sea_orm(table_name = "post")]
|
||||
|
96
src/main.rs
96
src/main.rs
@ -1,25 +1,28 @@
|
||||
extern crate serde_derive;
|
||||
|
||||
use actix_session::{Session, CookieSession};
|
||||
use actix_admin::{
|
||||
ActixAdmin, ActixAdminViewModel, ActixAdminViewModelTrait,
|
||||
AppDataTrait as ActixAdminAppDataTrait,
|
||||
};
|
||||
use actix_session::{CookieSession, Session};
|
||||
use actix_web::{web, App, HttpResponse, HttpServer};
|
||||
use tera::{ Tera, Context};
|
||||
use azure_auth::{AppDataTrait as AzureAuthAppDataTrait, AzureAuth, UserInfo};
|
||||
use oauth2::basic::BasicClient;
|
||||
use oauth2::{ RedirectUrl };
|
||||
use std::time::{Duration};
|
||||
use oauth2::RedirectUrl;
|
||||
use sea_orm::{ConnectOptions, DatabaseConnection};
|
||||
use std::env;
|
||||
use sea_orm::{{ DatabaseConnection, ConnectOptions }};
|
||||
use actix_admin::{ AppDataTrait as ActixAdminAppDataTrait, ActixAdminViewModel, ActixAdmin, ActixAdminViewModelTrait };
|
||||
use azure_auth::{ AzureAuth, UserInfo, AppDataTrait as AzureAuthAppDataTrait };
|
||||
use std::time::Duration;
|
||||
use tera::{Context, Tera};
|
||||
|
||||
mod entity;
|
||||
use entity::{ Post, Comment };
|
||||
use entity::{Comment, Post};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AppState {
|
||||
pub oauth: BasicClient,
|
||||
pub tmpl: Tera,
|
||||
pub db: DatabaseConnection,
|
||||
pub actix_admin: ActixAdmin
|
||||
pub actix_admin: ActixAdmin,
|
||||
}
|
||||
|
||||
impl ActixAdminAppDataTrait for AppState {
|
||||
@ -39,7 +42,11 @@ impl AzureAuthAppDataTrait for AppState {
|
||||
|
||||
async fn index(session: Session, data: web::Data<AppState>) -> HttpResponse {
|
||||
let login = session.get::<UserInfo>("user_info").unwrap();
|
||||
let web_auth_link = if login.is_some() { "/auth/logout" } else { "/auth/login" };
|
||||
let web_auth_link = if login.is_some() {
|
||||
"/auth/logout"
|
||||
} else {
|
||||
"/auth/login"
|
||||
};
|
||||
|
||||
let mut ctx = Context::new();
|
||||
ctx.insert("web_auth_link", web_auth_link);
|
||||
@ -47,26 +54,46 @@ async fn index(session: Session, data: web::Data<AppState>) -> HttpResponse {
|
||||
HttpResponse::Ok().body(rendered)
|
||||
}
|
||||
|
||||
// TODO: Generate this with a Macro accepting Tuples of (Entity, viewmodel)
|
||||
fn setup_actix_admin(
|
||||
actix_admin: &ActixAdmin,
|
||||
post_view_model: &ActixAdminViewModel,
|
||||
comment_view_model: &ActixAdminViewModel,
|
||||
) -> actix_web::Scope {
|
||||
actix_admin
|
||||
.create_scope::<AppState>()
|
||||
.service(
|
||||
web::scope(&format!("/{}", post_view_model.entity_name))
|
||||
.route("/list", web::get().to(Post::list::<AppState>)),
|
||||
)
|
||||
.service(
|
||||
web::scope(&format!("/{}", comment_view_model.entity_name))
|
||||
.route("/list", web::get().to(Comment::list::<AppState>)),
|
||||
)
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
async fn main() {
|
||||
dotenv::dotenv().ok();
|
||||
let oauth2_client_id = env::var("OAUTH2_CLIENT_ID").expect("Missing the OAUTH2_CLIENT_ID environment variable.");
|
||||
let oauth2_client_secret = env::var("OAUTH2_CLIENT_SECRET").expect("Missing the OAUTH2_CLIENT_SECRET environment variable.");
|
||||
let oauth2_server = env::var("OAUTH2_SERVER").expect("Missing the OAUTH2_SERVER environment variable.");
|
||||
let oauth2_client_id =
|
||||
env::var("OAUTH2_CLIENT_ID").expect("Missing the OAUTH2_CLIENT_ID environment variable.");
|
||||
let oauth2_client_secret = env::var("OAUTH2_CLIENT_SECRET")
|
||||
.expect("Missing the OAUTH2_CLIENT_SECRET environment variable.");
|
||||
let oauth2_server =
|
||||
env::var("OAUTH2_SERVER").expect("Missing the OAUTH2_SERVER environment variable.");
|
||||
let azure_auth = AzureAuth::new(&oauth2_server, &oauth2_client_id, &oauth2_client_secret);
|
||||
|
||||
// Set up the config for the OAuth2 process.
|
||||
let client = azure_auth.clone().get_oauth_client()
|
||||
// This example will be running its own server at 127.0.0.1:5000.
|
||||
.set_redirect_uri(
|
||||
RedirectUrl::new("http://localhost:5000/auth".to_string())
|
||||
.expect("Invalid redirect URL"),
|
||||
);
|
||||
let client = azure_auth
|
||||
.clone()
|
||||
.get_oauth_client()
|
||||
// This example will be running its own server at 127.0.0.1:5000.
|
||||
.set_redirect_uri(
|
||||
RedirectUrl::new("http://localhost:5000/auth".to_string())
|
||||
.expect("Invalid redirect URL"),
|
||||
);
|
||||
|
||||
let tera =
|
||||
Tera::new(
|
||||
concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")
|
||||
).unwrap();
|
||||
let tera = Tera::new(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")).unwrap();
|
||||
|
||||
let db_url = env::var("DATABASE_URL").expect("DATABASE_URL is not set in .env file");
|
||||
let mut opt = ConnectOptions::new(db_url);
|
||||
@ -84,12 +111,11 @@ async fn main() {
|
||||
let actix_admin = ActixAdmin::new()
|
||||
.add_entity::<AppState>(&post_view_model)
|
||||
.add_entity::<AppState>(&comment_view_model);
|
||||
|
||||
let app_state = AppState {
|
||||
oauth: client,
|
||||
tmpl: tera,
|
||||
db: conn,
|
||||
actix_admin: actix_admin.clone()
|
||||
actix_admin: actix_admin.clone(),
|
||||
};
|
||||
|
||||
HttpServer::new(move || {
|
||||
@ -98,23 +124,15 @@ async fn main() {
|
||||
.wrap(CookieSession::signed(&[0; 32]).secure(false))
|
||||
.route("/", web::get().to(index))
|
||||
.service(azure_auth.clone().create_scope::<AppState>())
|
||||
.service(
|
||||
// TODO: Generate this with a Macro accepting Tuples of (Entity, viewmodel)
|
||||
actix_admin
|
||||
.create_scope::<AppState>()
|
||||
.service(
|
||||
web::scope(&format!("/{}", post_view_model.entity_name))
|
||||
.route("/list", web::get().to(Post::list::<AppState>)),
|
||||
)
|
||||
.service(
|
||||
web::scope(&format!("/{}", comment_view_model.entity_name))
|
||||
.route("/list", web::get().to(Comment::list::<AppState>)),
|
||||
)
|
||||
)
|
||||
.service(setup_actix_admin(
|
||||
&actix_admin,
|
||||
&post_view_model,
|
||||
&comment_view_model,
|
||||
))
|
||||
})
|
||||
.bind("127.0.0.1:5000")
|
||||
.expect("Can not bind to port 5000")
|
||||
.run()
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user