move sample to folder example and rename project
This commit is contained in:
parent
c0245fd5c5
commit
5d9b7def23
4
.env
4
.env
@ -1,4 +0,0 @@
|
|||||||
DATABASE_URL=sqlite://database.db
|
|
||||||
OAUTH2_CLIENT_SECRET=
|
|
||||||
OAUTH2_CLIENT_ID=
|
|
||||||
OAUTH2_SERVER=
|
|
27
Cargo.toml
27
Cargo.toml
@ -1,29 +1,18 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-web-sample-app"
|
name = "actix_admin"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "4.0.1"
|
actix-web = "4.0.1"
|
||||||
actix-rt = "2.7.0"
|
actix-multipart = "0.4.0"
|
||||||
actix-session = "0.5.0"
|
futures-util = "0.3.21"
|
||||||
tera = "1.15.0"
|
chrono = "0.4.20"
|
||||||
itertools = "0.10.3"
|
tera = "1.16.0"
|
||||||
oauth2 = "4.1"
|
actix_admin_macros = { path = "actix_admin_macros" }
|
||||||
base64 = "0.13.0"
|
|
||||||
async-trait = "0.1.53"
|
async-trait = "0.1.53"
|
||||||
rand = "0.8.5"
|
lazy_static = "1.4.0"
|
||||||
url = "2.2.2"
|
|
||||||
http = "0.2.6"
|
|
||||||
dotenv = "0.15"
|
|
||||||
futures = "0.3.21"
|
|
||||||
serde = "1.0.136"
|
serde = "1.0.136"
|
||||||
serde_json = "1.0.79"
|
serde_json = "1.0.79"
|
||||||
serde_derive = "1.0.136"
|
serde_derive = "1.0.136"
|
||||||
quote = "1.0"
|
sea-orm = { version = "^0.9.1", features = [], default-features = false }
|
||||||
sea-orm = { version = "0.8.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,18 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "actix_admin"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
actix-web = "4.0.1"
|
|
||||||
actix-multipart = "0.4.0"
|
|
||||||
futures-util = "0.3.21"
|
|
||||||
chrono = "0.4.20"
|
|
||||||
tera = "1.16.0"
|
|
||||||
actix_admin_macros = { path = "actix_admin_macros" }
|
|
||||||
async-trait = "0.1.53"
|
|
||||||
lazy_static = "1.4.0"
|
|
||||||
serde = "1.0.136"
|
|
||||||
serde_json = "1.0.79"
|
|
||||||
serde_derive = "1.0.136"
|
|
||||||
sea-orm = { version = "^0", features = [], default-features = false }
|
|
@ -1,63 +0,0 @@
|
|||||||
{% if render_partial and render_partial == true %}
|
|
||||||
|
|
||||||
{% block content %}
|
|
||||||
{% endblock content %}
|
|
||||||
|
|
||||||
{% else %}
|
|
||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<title>Actix Admin</title>
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css">
|
|
||||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/css/all.min.css"
|
|
||||||
integrity="sha512-1sCRPdkRXhBV2PBLUdRb4tMg1w2YPf37qatUFeS7zlBy7jJI8Lf4VHwWfZZfpXtYSLy85pkm9GaYVYMfw5BC1A=="
|
|
||||||
crossorigin="anonymous" referrerpolicy="no-referrer" />
|
|
||||||
<script src="https://unpkg.com/htmx.org@1.7.0"></script>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
function checkAll(bx) {
|
|
||||||
var cbs = document.getElementsByTagName('input');
|
|
||||||
for (var i = 0; i < cbs.length; i++) {
|
|
||||||
if (cbs[i].type == 'checkbox') {
|
|
||||||
cbs[i].checked = bx.checked;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
|
||||||
// Get all "navbar-burger" elements
|
|
||||||
const $navbarBurgers = Array.prototype.slice.call(document.querySelectorAll('.navbar-burger'), 0);
|
|
||||||
|
|
||||||
// Add a click event on each of them
|
|
||||||
$navbarBurgers.forEach(el => {
|
|
||||||
el.addEventListener('click', () => {
|
|
||||||
|
|
||||||
// Get the target from the "data-target" attribute
|
|
||||||
const target = el.dataset.target;
|
|
||||||
const $target = document.getElementById(target);
|
|
||||||
|
|
||||||
// Toggle the "is-active" class on both the "navbar-burger" and the "navbar-menu"
|
|
||||||
el.classList.toggle('is-active');
|
|
||||||
$target.classList.toggle('is-active');
|
|
||||||
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body>
|
|
||||||
{% include "header.html" %}
|
|
||||||
<div class="container is-fluid">
|
|
||||||
{% block content %}
|
|
||||||
{% endblock content %}
|
|
||||||
</div>
|
|
||||||
</body>
|
|
||||||
|
|
||||||
</html>
|
|
||||||
{% endif %}
|
|
@ -1,4 +0,0 @@
|
|||||||
{% extends "base.html" %}
|
|
||||||
|
|
||||||
{% block content %}
|
|
||||||
{% endblock content %}
|
|
@ -1,145 +0,0 @@
|
|||||||
{% extends "base.html" %}
|
|
||||||
|
|
||||||
{% block content %}
|
|
||||||
|
|
||||||
{% if not render_partial or render_partial == false %}
|
|
||||||
<div class="columns">
|
|
||||||
<div class="column">
|
|
||||||
<div class="buttons">
|
|
||||||
<a class="button is-primary" href="create">Create</a>
|
|
||||||
|
|
||||||
<div hx-include="#checked-rows" hx-target="#{{ entity_name }}table" class="dropdown is-hoverable">
|
|
||||||
<div class="dropdown-trigger">
|
|
||||||
<button class="button" aria-haspopup="true" aria-controls="dropdown-menu4">
|
|
||||||
<span>With selected</span>
|
|
||||||
<span class="icon is-small">
|
|
||||||
<i class="fas fa-angle-down" aria-hidden="true"></i>
|
|
||||||
</span>
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
<div class="dropdown-menu" id="dropdown-menu4">
|
|
||||||
<div class="dropdown-content">
|
|
||||||
<div class="dropdown-item">
|
|
||||||
<a href="#" hx-confirm="Are you sure?" hx-delete="delete">Delete</a>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="column is-narrow">
|
|
||||||
{% if view_model.show_search %}
|
|
||||||
<div class="field">
|
|
||||||
<p class="control has-icons-left has-icons-right">
|
|
||||||
<input class="input is-rounded" type="search" id="search" value="{{ search }}" name="search"
|
|
||||||
placeholder="Search"
|
|
||||||
hx-get="/admin/{{ entity_name }}/list?render_partial=true&entities_per_page={{ entities_per_page }}&page={{ page }}"
|
|
||||||
hx-trigger="keyup changed delay:500ms, search" hx-target="#{{ entity_name }}table"
|
|
||||||
hx-indicator=".htmx-indicator">
|
|
||||||
<span class="icon is-small is-left">
|
|
||||||
<i class="fas fa-search"></i>
|
|
||||||
</span>
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
{% endif %}
|
|
||||||
</div>
|
|
||||||
<div class="column is-narrow">
|
|
||||||
<div>
|
|
||||||
<form>
|
|
||||||
<input type="hidden" value="{{ search }}" name="search">
|
|
||||||
<div class="select">
|
|
||||||
<div class="control has-icons-left has-icons-right">
|
|
||||||
<select class="select" name="entities_per_page" onchange="this.form.submit()">
|
|
||||||
{% for a in [10,20,50,100,] %}
|
|
||||||
<option {% if entities_per_page==a %}selected{% endif %} value="{{ a }}">{{ a }}</option>
|
|
||||||
{% endfor %}
|
|
||||||
</select>
|
|
||||||
<p class="help">Entities per Page</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</form>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
<div id="{{ entity_name }}table">
|
|
||||||
<form id="checked-rows">
|
|
||||||
<table class="table is-fullwidth is-hoverable">
|
|
||||||
<thead>
|
|
||||||
<tr>
|
|
||||||
<th>
|
|
||||||
<input type="checkbox" onclick="checkAll(this)">
|
|
||||||
</th>
|
|
||||||
<th>{{ view_model.primary_key | title }}</th>
|
|
||||||
{% for model_field in view_model.fields -%}
|
|
||||||
<th>{{ model_field.field_name | split(pat="_") | join(sep=" ") | title }}</th>
|
|
||||||
{%- endfor %}
|
|
||||||
<th>
|
|
||||||
<!-- Edit Action -->
|
|
||||||
<!-- Delete Action -->
|
|
||||||
</th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody hx-confirm="Are you sure?" hx-target="closest tr" hx-swap="outerHTML">
|
|
||||||
{% for entity in entities -%}
|
|
||||||
<tr>
|
|
||||||
<td><input type="checkbox" name="ids" value="{{ entity.primary_key }}"></td>
|
|
||||||
<td>{{ entity.primary_key }}</td>
|
|
||||||
{% for model_field in view_model.fields -%}
|
|
||||||
{% if model_field.field_type == "Checkbox" %}
|
|
||||||
<td>{{ entity.values | get(key=model_field.field_name) | get_icon | safe }}</td>
|
|
||||||
{% else %}
|
|
||||||
<td>{{ entity.values | get(key=model_field.field_name) }}</td>
|
|
||||||
{% endif %}
|
|
||||||
{%- endfor %}
|
|
||||||
<td>
|
|
||||||
<a href="edit/{{ entity.primary_key }}"><i class="fa-solid fa-pen-to-square"></i></a>
|
|
||||||
<a hx-delete="delete/{{ entity.primary_key }}"><i class="fa-solid fa-trash"></i></a>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
{%- endfor %}
|
|
||||||
</tbody>
|
|
||||||
<tfoot>
|
|
||||||
<tr>
|
|
||||||
<td colspan="{{ view_model.fields | length + 3 }}">
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
</tfoot>
|
|
||||||
</table>
|
|
||||||
</form>
|
|
||||||
<nav class="pagination" aria-label="pagination">
|
|
||||||
<a href="?page={{ page - 1 }}&entities_per_page={{ entities_per_page }}&search={{ search }}"
|
|
||||||
class="pagination-previous">Previous Page</a>
|
|
||||||
<a href="?page={{ page + 1 }}&entities_per_page={{ entities_per_page }}&search={{ search }}"
|
|
||||||
class="pagination-next">Next page</a>
|
|
||||||
<ul class="pagination-list">
|
|
||||||
<!-- <li>
|
|
||||||
<a class="pagination-link"
|
|
||||||
href="?page={{ page - 1 }}&entities_per_page={{ entities_per_page }}&search={{ search }}"
|
|
||||||
aria-label="Goto page 1">1</a>
|
|
||||||
</li> -->
|
|
||||||
<li>
|
|
||||||
<span class="pagination-ellipsis">…</span>
|
|
||||||
</li>
|
|
||||||
{% for i in range(end=num_pages) %}
|
|
||||||
<li><a class="pagination-link" aria-label="Goto page 45"
|
|
||||||
href="?page={{ i + 1 }}&entities_per_page={{ entities_per_page }}&search={{ search }}">{{
|
|
||||||
i + 1 }}</a></li>
|
|
||||||
{%- endfor %}
|
|
||||||
<li>
|
|
||||||
<span class="pagination-ellipsis">…</span>
|
|
||||||
</li>
|
|
||||||
<!-- <li>
|
|
||||||
<a class="pagination-link" aria-label="Goto page {{ num_pages }}">{{ num_pages }} </a>
|
|
||||||
</li> -->
|
|
||||||
</ul>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{% if not render_partial or render_partial == false %}
|
|
||||||
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
{% endblock content %}
|
|
BIN
database.db
BIN
database.db
Binary file not shown.
BIN
database.db-wal
BIN
database.db-wal
Binary file not shown.
@ -1 +0,0 @@
|
|||||||
-- Add migration script here
|
|
@ -1,24 +0,0 @@
|
|||||||
use sea_orm::entity::prelude::*;
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use actix_admin::prelude::*;
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize, DeriveActixAdminModel)]
|
|
||||||
#[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,
|
|
||||||
#[sea_orm(column_type = "Text")]
|
|
||||||
#[actix_admin(html_input_type = "email")]
|
|
||||||
pub user: String,
|
|
||||||
#[sea_orm(column_type = "DateTime")]
|
|
||||||
pub insert_date: DateTime,
|
|
||||||
pub is_visible: bool
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
|
||||||
pub enum Relation {}
|
|
||||||
|
|
||||||
impl ActiveModelBehavior for ActiveModel {}
|
|
@ -1,52 +0,0 @@
|
|||||||
// setup
|
|
||||||
use sea_orm::sea_query::{ColumnDef, TableCreateStatement};
|
|
||||||
use sea_orm::{error::*, sea_query, ConnectionTrait, DbConn, ExecResult};
|
|
||||||
pub mod comment;
|
|
||||||
pub mod post;
|
|
||||||
pub use comment::Entity as Comment;
|
|
||||||
pub use post::Entity as Post;
|
|
||||||
|
|
||||||
// setup
|
|
||||||
async fn create_table(db: &DbConn, stmt: &TableCreateStatement) -> Result<ExecResult, DbErr> {
|
|
||||||
let builder = db.get_database_backend();
|
|
||||||
db.execute(builder.build(stmt)).await
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn create_post_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
|
||||||
let stmt = sea_query::Table::create()
|
|
||||||
.table(post::Entity)
|
|
||||||
.if_not_exists()
|
|
||||||
.col(
|
|
||||||
ColumnDef::new(post::Column::Id)
|
|
||||||
.integer()
|
|
||||||
.not_null()
|
|
||||||
.auto_increment()
|
|
||||||
.primary_key(),
|
|
||||||
)
|
|
||||||
.col(ColumnDef::new(post::Column::Title).string().not_null())
|
|
||||||
.col(ColumnDef::new(post::Column::Text).string().not_null())
|
|
||||||
.col(ColumnDef::new(post::Column::TeaMandatory).string().not_null())
|
|
||||||
.col(ColumnDef::new(post::Column::TeaOptional).string())
|
|
||||||
.col(ColumnDef::new(post::Column::InsertDate).date())
|
|
||||||
.to_owned();
|
|
||||||
|
|
||||||
let _result = create_table(db, &stmt).await;
|
|
||||||
|
|
||||||
let stmt = sea_query::Table::create()
|
|
||||||
.table(comment::Entity)
|
|
||||||
.if_not_exists()
|
|
||||||
.col(
|
|
||||||
ColumnDef::new(post::Column::Id)
|
|
||||||
.integer()
|
|
||||||
.not_null()
|
|
||||||
.auto_increment()
|
|
||||||
.primary_key(),
|
|
||||||
)
|
|
||||||
.col(ColumnDef::new(comment::Column::Comment).string().not_null())
|
|
||||||
.col(ColumnDef::new(comment::Column::User).string().not_null())
|
|
||||||
.col(ColumnDef::new(comment::Column::InsertDate).date_time().not_null())
|
|
||||||
.col(ColumnDef::new(comment::Column::IsVisible).boolean().not_null())
|
|
||||||
.to_owned();
|
|
||||||
|
|
||||||
create_table(db, &stmt).await
|
|
||||||
}
|
|
@ -1,60 +0,0 @@
|
|||||||
use sea_orm::entity::prelude::*;
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use actix_admin::prelude::*;
|
|
||||||
use std::fmt;
|
|
||||||
use std::fmt::Display;
|
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize, DeriveActixAdminModel)]
|
|
||||||
#[sea_orm(table_name = "post")]
|
|
||||||
pub struct Model {
|
|
||||||
#[sea_orm(primary_key)]
|
|
||||||
#[serde(skip_deserializing)]
|
|
||||||
#[actix_admin(primary_key)]
|
|
||||||
pub id: i32,
|
|
||||||
#[actix_admin(searchable)]
|
|
||||||
pub title: String,
|
|
||||||
#[sea_orm(column_type = "Text")]
|
|
||||||
#[actix_admin(searchable)]
|
|
||||||
pub text: String,
|
|
||||||
#[actix_admin(select_list="Tea")]
|
|
||||||
pub tea_mandatory: Tea,
|
|
||||||
#[actix_admin(select_list="Tea")]
|
|
||||||
pub tea_optional: Option<Tea>,
|
|
||||||
pub insert_date: Date,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
|
||||||
pub enum Relation {}
|
|
||||||
|
|
||||||
impl ActiveModelBehavior for ActiveModel {}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, EnumIter, DeriveActiveEnum, Deserialize, Serialize, DeriveActixAdminSelectList)]
|
|
||||||
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "tea")]
|
|
||||||
pub enum Tea {
|
|
||||||
#[sea_orm(string_value = "EverydayTea")]
|
|
||||||
EverydayTea,
|
|
||||||
#[sea_orm(string_value = "BreakfastTea")]
|
|
||||||
BreakfastTea,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for Tea {
|
|
||||||
type Err = ();
|
|
||||||
|
|
||||||
fn from_str(input: &str) -> Result<Tea, Self::Err> {
|
|
||||||
match input {
|
|
||||||
"EverydayTea" => Ok(Tea::EverydayTea),
|
|
||||||
"BreakfastTea" => Ok(Tea::BreakfastTea),
|
|
||||||
_ => Err(()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for Tea {
|
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
match &*self {
|
|
||||||
Tea::EverydayTea => write!(formatter, "{}", String::from("EverydayTea")),
|
|
||||||
Tea::BreakfastTea => write!(formatter, "{}", String::from("BreakfastTea")),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
124
src/main.rs
124
src/main.rs
@ -1,124 +0,0 @@
|
|||||||
extern crate serde_derive;
|
|
||||||
|
|
||||||
use actix_admin::prelude::*;
|
|
||||||
use actix_session::{CookieSession, Session};
|
|
||||||
use actix_web::{web, App, HttpResponse, HttpServer, middleware};
|
|
||||||
use azure_auth::{AppDataTrait as AzureAuthAppDataTrait, AzureAuth, UserInfo};
|
|
||||||
use oauth2::basic::BasicClient;
|
|
||||||
use oauth2::RedirectUrl;
|
|
||||||
use sea_orm::{ConnectOptions, DatabaseConnection};
|
|
||||||
use std::env;
|
|
||||||
use std::time::Duration;
|
|
||||||
use tera::{Context, Tera};
|
|
||||||
|
|
||||||
mod entity;
|
|
||||||
use entity::{Post, Comment};
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct AppState {
|
|
||||||
pub oauth: BasicClient,
|
|
||||||
pub tmpl: Tera,
|
|
||||||
pub db: DatabaseConnection,
|
|
||||||
pub actix_admin: ActixAdmin,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ActixAdminAppDataTrait for AppState {
|
|
||||||
fn get_db(&self) -> &DatabaseConnection {
|
|
||||||
&self.db
|
|
||||||
}
|
|
||||||
fn get_actix_admin(&self) -> &ActixAdmin {
|
|
||||||
&self.actix_admin
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AzureAuthAppDataTrait for AppState {
|
|
||||||
fn get_oauth(&self) -> &BasicClient {
|
|
||||||
&self.oauth
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 mut ctx = Context::new();
|
|
||||||
ctx.insert("web_auth_link", web_auth_link);
|
|
||||||
let rendered = data.tmpl.render("index.html", &ctx).unwrap();
|
|
||||||
HttpResponse::Ok().body(rendered)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create_actix_admin_builder() -> ActixAdminBuilder {
|
|
||||||
let post_view_model = ActixAdminViewModel::from(Post);
|
|
||||||
let comment_view_model = ActixAdminViewModel::from(Comment);
|
|
||||||
|
|
||||||
let mut admin_builder = ActixAdminBuilder::new();
|
|
||||||
admin_builder.add_entity::<AppState, Post>(&post_view_model);
|
|
||||||
admin_builder.add_entity::<AppState, Comment>(&comment_view_model);
|
|
||||||
|
|
||||||
admin_builder
|
|
||||||
}
|
|
||||||
|
|
||||||
#[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 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 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);
|
|
||||||
opt.max_connections(100)
|
|
||||||
.min_connections(5)
|
|
||||||
.connect_timeout(Duration::from_secs(8))
|
|
||||||
.idle_timeout(Duration::from_secs(8))
|
|
||||||
.sqlx_logging(true);
|
|
||||||
|
|
||||||
let conn = sea_orm::Database::connect(opt).await.unwrap();
|
|
||||||
let _ = entity::create_post_table(&conn).await;
|
|
||||||
|
|
||||||
let actix_admin = create_actix_admin_builder().get_actix_admin();
|
|
||||||
|
|
||||||
let app_state = AppState {
|
|
||||||
oauth: client,
|
|
||||||
tmpl: tera,
|
|
||||||
db: conn,
|
|
||||||
actix_admin: actix_admin,
|
|
||||||
};
|
|
||||||
|
|
||||||
HttpServer::new(move || {
|
|
||||||
App::new()
|
|
||||||
.app_data(web::Data::new(app_state.clone()))
|
|
||||||
.wrap(CookieSession::signed(&[0; 32]).secure(false))
|
|
||||||
.route("/", web::get().to(index))
|
|
||||||
.service(azure_auth.clone().create_scope::<AppState>())
|
|
||||||
.service(
|
|
||||||
create_actix_admin_builder().get_scope::<AppState>()
|
|
||||||
)
|
|
||||||
.wrap(middleware::Logger::default())
|
|
||||||
})
|
|
||||||
.bind("127.0.0.1:5000")
|
|
||||||
.expect("Can not bind to port 5000")
|
|
||||||
.run()
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
@ -71,11 +71,11 @@ impl ActixAdminModel {
|
|||||||
self.get_value_by_closure(key, is_option_or_string, |val| val.parse::<T>())
|
self.get_value_by_closure(key, is_option_or_string, |val| val.parse::<T>())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_datetime(&self, key: &str, is_option_or_string: bool) -> Result<Option<DateTime>, String> {
|
pub fn get_datetime(&self, key: &str, is_option_or_string: bool) -> Result<Option<NaiveDateTime>, String> {
|
||||||
self.get_value_by_closure(key, is_option_or_string, |val| NaiveDateTime::parse_from_str(val, "%Y-%m-%dT%H:%M"))
|
self.get_value_by_closure(key, is_option_or_string, |val| NaiveDateTime::parse_from_str(val, "%Y-%m-%dT%H:%M"))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_date(&self, key: &str, is_option_or_string: bool) -> Result<Option<Date>, String> {
|
pub fn get_date(&self, key: &str, is_option_or_string: bool) -> Result<Option<NaiveDate>, String> {
|
||||||
self.get_value_by_closure(key, is_option_or_string, |val| NaiveDate::parse_from_str(val, "%Y-%m-%d"))
|
self.get_value_by_closure(key, is_option_or_string, |val| NaiveDate::parse_from_str(val, "%Y-%m-%d"))
|
||||||
}
|
}
|
||||||
|
|
@ -1,14 +1,63 @@
|
|||||||
|
{% if render_partial and render_partial == true %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
{% endblock content %}
|
||||||
|
|
||||||
|
{% else %}
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html lang="en">
|
||||||
<head>
|
|
||||||
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<title>Actix Web</title>
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<title>Actix Admin</title>
|
||||||
|
|
||||||
<link rel="stylesheet" href="https://unpkg.com/@picocss/pico@latest/css/pico.classless.min.css">
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css">
|
||||||
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/css/all.min.css"
|
||||||
|
integrity="sha512-1sCRPdkRXhBV2PBLUdRb4tMg1w2YPf37qatUFeS7zlBy7jJI8Lf4VHwWfZZfpXtYSLy85pkm9GaYVYMfw5BC1A=="
|
||||||
|
crossorigin="anonymous" referrerpolicy="no-referrer" />
|
||||||
|
<script src="https://unpkg.com/htmx.org@1.7.0"></script>
|
||||||
|
|
||||||
</head>
|
<script>
|
||||||
<body>
|
function checkAll(bx) {
|
||||||
|
var cbs = document.getElementsByTagName('input');
|
||||||
|
for (var i = 0; i < cbs.length; i++) {
|
||||||
|
if (cbs[i].type == 'checkbox') {
|
||||||
|
cbs[i].checked = bx.checked;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
|
// Get all "navbar-burger" elements
|
||||||
|
const $navbarBurgers = Array.prototype.slice.call(document.querySelectorAll('.navbar-burger'), 0);
|
||||||
|
|
||||||
|
// Add a click event on each of them
|
||||||
|
$navbarBurgers.forEach(el => {
|
||||||
|
el.addEventListener('click', () => {
|
||||||
|
|
||||||
|
// Get the target from the "data-target" attribute
|
||||||
|
const target = el.dataset.target;
|
||||||
|
const $target = document.getElementById(target);
|
||||||
|
|
||||||
|
// Toggle the "is-active" class on both the "navbar-burger" and the "navbar-menu"
|
||||||
|
el.classList.toggle('is-active');
|
||||||
|
$target.classList.toggle('is-active');
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
{% include "header.html" %}
|
||||||
|
<div class="container is-fluid">
|
||||||
{% block content %}
|
{% block content %}
|
||||||
{% endblock content %}
|
{% endblock content %}
|
||||||
</body>
|
</div>
|
||||||
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
|
{% endif %}
|
@ -1,5 +1,4 @@
|
|||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<a href="/{{ web_auth_link }}">{{ web_auth_link }}</a>
|
|
||||||
{% endblock content %}
|
{% endblock content %}
|
||||||
|
@ -1,8 +1,145 @@
|
|||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<p>posts: {{ posts }}</p>
|
|
||||||
<p>page: {{ page }}</p>
|
{% if not render_partial or render_partial == false %}
|
||||||
<p>posts_per_page: {{ posts_per_page }}</p>
|
<div class="columns">
|
||||||
<p>num_pages: {{ num_pages }}</p>
|
<div class="column">
|
||||||
|
<div class="buttons">
|
||||||
|
<a class="button is-primary" href="create">Create</a>
|
||||||
|
|
||||||
|
<div hx-include="#checked-rows" hx-target="#{{ entity_name }}table" class="dropdown is-hoverable">
|
||||||
|
<div class="dropdown-trigger">
|
||||||
|
<button class="button" aria-haspopup="true" aria-controls="dropdown-menu4">
|
||||||
|
<span>With selected</span>
|
||||||
|
<span class="icon is-small">
|
||||||
|
<i class="fas fa-angle-down" aria-hidden="true"></i>
|
||||||
|
</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div class="dropdown-menu" id="dropdown-menu4">
|
||||||
|
<div class="dropdown-content">
|
||||||
|
<div class="dropdown-item">
|
||||||
|
<a href="#" hx-confirm="Are you sure?" hx-delete="delete">Delete</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="column is-narrow">
|
||||||
|
{% if view_model.show_search %}
|
||||||
|
<div class="field">
|
||||||
|
<p class="control has-icons-left has-icons-right">
|
||||||
|
<input class="input is-rounded" type="search" id="search" value="{{ search }}" name="search"
|
||||||
|
placeholder="Search"
|
||||||
|
hx-get="/admin/{{ entity_name }}/list?render_partial=true&entities_per_page={{ entities_per_page }}&page={{ page }}"
|
||||||
|
hx-trigger="keyup changed delay:500ms, search" hx-target="#{{ entity_name }}table"
|
||||||
|
hx-indicator=".htmx-indicator">
|
||||||
|
<span class="icon is-small is-left">
|
||||||
|
<i class="fas fa-search"></i>
|
||||||
|
</span>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
<div class="column is-narrow">
|
||||||
|
<div>
|
||||||
|
<form>
|
||||||
|
<input type="hidden" value="{{ search }}" name="search">
|
||||||
|
<div class="select">
|
||||||
|
<div class="control has-icons-left has-icons-right">
|
||||||
|
<select class="select" name="entities_per_page" onchange="this.form.submit()">
|
||||||
|
{% for a in [10,20,50,100,] %}
|
||||||
|
<option {% if entities_per_page==a %}selected{% endif %} value="{{ a }}">{{ a }}</option>
|
||||||
|
{% endfor %}
|
||||||
|
</select>
|
||||||
|
<p class="help">Entities per Page</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
<div id="{{ entity_name }}table">
|
||||||
|
<form id="checked-rows">
|
||||||
|
<table class="table is-fullwidth is-hoverable">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>
|
||||||
|
<input type="checkbox" onclick="checkAll(this)">
|
||||||
|
</th>
|
||||||
|
<th>{{ view_model.primary_key | title }}</th>
|
||||||
|
{% for model_field in view_model.fields -%}
|
||||||
|
<th>{{ model_field.field_name | split(pat="_") | join(sep=" ") | title }}</th>
|
||||||
|
{%- endfor %}
|
||||||
|
<th>
|
||||||
|
<!-- Edit Action -->
|
||||||
|
<!-- Delete Action -->
|
||||||
|
</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody hx-confirm="Are you sure?" hx-target="closest tr" hx-swap="outerHTML">
|
||||||
|
{% for entity in entities -%}
|
||||||
|
<tr>
|
||||||
|
<td><input type="checkbox" name="ids" value="{{ entity.primary_key }}"></td>
|
||||||
|
<td>{{ entity.primary_key }}</td>
|
||||||
|
{% for model_field in view_model.fields -%}
|
||||||
|
{% if model_field.field_type == "Checkbox" %}
|
||||||
|
<td>{{ entity.values | get(key=model_field.field_name) | get_icon | safe }}</td>
|
||||||
|
{% else %}
|
||||||
|
<td>{{ entity.values | get(key=model_field.field_name) }}</td>
|
||||||
|
{% endif %}
|
||||||
|
{%- endfor %}
|
||||||
|
<td>
|
||||||
|
<a href="edit/{{ entity.primary_key }}"><i class="fa-solid fa-pen-to-square"></i></a>
|
||||||
|
<a hx-delete="delete/{{ entity.primary_key }}"><i class="fa-solid fa-trash"></i></a>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
{%- endfor %}
|
||||||
|
</tbody>
|
||||||
|
<tfoot>
|
||||||
|
<tr>
|
||||||
|
<td colspan="{{ view_model.fields | length + 3 }}">
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</tfoot>
|
||||||
|
</table>
|
||||||
|
</form>
|
||||||
|
<nav class="pagination" aria-label="pagination">
|
||||||
|
<a href="?page={{ page - 1 }}&entities_per_page={{ entities_per_page }}&search={{ search }}"
|
||||||
|
class="pagination-previous">Previous Page</a>
|
||||||
|
<a href="?page={{ page + 1 }}&entities_per_page={{ entities_per_page }}&search={{ search }}"
|
||||||
|
class="pagination-next">Next page</a>
|
||||||
|
<ul class="pagination-list">
|
||||||
|
<!-- <li>
|
||||||
|
<a class="pagination-link"
|
||||||
|
href="?page={{ page - 1 }}&entities_per_page={{ entities_per_page }}&search={{ search }}"
|
||||||
|
aria-label="Goto page 1">1</a>
|
||||||
|
</li> -->
|
||||||
|
<li>
|
||||||
|
<span class="pagination-ellipsis">…</span>
|
||||||
|
</li>
|
||||||
|
{% for i in range(end=num_pages) %}
|
||||||
|
<li><a class="pagination-link" aria-label="Goto page 45"
|
||||||
|
href="?page={{ i + 1 }}&entities_per_page={{ entities_per_page }}&search={{ search }}">{{
|
||||||
|
i + 1 }}</a></li>
|
||||||
|
{%- endfor %}
|
||||||
|
<li>
|
||||||
|
<span class="pagination-ellipsis">…</span>
|
||||||
|
</li>
|
||||||
|
<!-- <li>
|
||||||
|
<a class="pagination-link" aria-label="Goto page {{ num_pages }}">{{ num_pages }} </a>
|
||||||
|
</li> -->
|
||||||
|
</ul>
|
||||||
|
</nav>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{% if not render_partial or render_partial == false %}
|
||||||
|
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
{% endblock content %}
|
{% endblock content %}
|
Loading…
Reference in New Issue
Block a user