From ab840126b3f2a7ddb5337b6a5fe21eb2c6d0e6d7 Mon Sep 17 00:00:00 2001 From: GW_MC <72297530+GWMCwing@users.noreply.github.com> Date: Wed, 7 Jan 2026 15:57:13 +0800 Subject: [PATCH] feat: add location and proxy host entities with migrations --- .../src/generated/entities/location.rs | 46 +++++++ public/database/src/generated/entities/mod.rs | 2 + .../src/generated/entities/prelude.rs | 2 + .../src/generated/entities/proxy_host.rs | 48 +++++++ .../src/generated/entities/upstream.rs | 4 + .../database/src/generated/entities/user.rs | 2 + public/migration/src/lib.rs | 2 + public/migration/src/migrations.rs | 2 + .../m20260102_000006_create_proxy_table.rs | 124 ++++++++++++++++++ .../m20260102_000007_create_location_table.rs | 115 ++++++++++++++++ 10 files changed, 347 insertions(+) create mode 100644 public/database/src/generated/entities/location.rs create mode 100644 public/database/src/generated/entities/proxy_host.rs create mode 100644 public/migration/src/migrations/m20260102_000006_create_proxy_table.rs create mode 100644 public/migration/src/migrations/m20260102_000007_create_location_table.rs diff --git a/public/database/src/generated/entities/location.rs b/public/database/src/generated/entities/location.rs new file mode 100644 index 0000000..bd0e8bb --- /dev/null +++ b/public/database/src/generated/entities/location.rs @@ -0,0 +1,46 @@ +//! `SeaORM` Entity, @generated by sea-orm-codegen 2.0.0-rc.18 + +use sea_orm::entity::prelude::*; +use serde::{Deserialize, Serialize}; + +#[sea_orm::model] +#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)] +#[sea_orm(table_name = "location")] +pub struct Model { + #[sea_orm(primary_key, auto_increment = false)] + pub id: Uuid, + pub host_id: Uuid, + pub path: String, + pub match_type: String, + pub order: i64, + pub upstream_id: Option, + pub proxy_pass_protocol: Option, + pub proxy_pass_host: Option, + pub proxy_pass_port: Option, + pub preserve_host_header: Option, + #[sea_orm(column_type = "JsonBinary", nullable)] + pub allowed_methods: Option, + #[sea_orm(column_type = "Text", nullable)] + pub custom_config: Option, + pub enabled: bool, + pub created_at: DateTimeUtc, + pub updated_at: DateTimeUtc, + #[sea_orm( + belongs_to, + from = "host_id", + to = "id", + on_update = "Cascade", + on_delete = "Cascade" + )] + pub proxy_host: HasOne, + #[sea_orm( + belongs_to, + from = "upstream_id", + to = "id", + on_update = "Cascade", + on_delete = "SetNull" + )] + pub upstream: HasOne, +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/public/database/src/generated/entities/mod.rs b/public/database/src/generated/entities/mod.rs index 8adf245..0ce0143 100644 --- a/public/database/src/generated/entities/mod.rs +++ b/public/database/src/generated/entities/mod.rs @@ -3,6 +3,8 @@ pub mod prelude; pub mod config; +pub mod location; +pub mod proxy_host; pub mod upstream; pub mod upstream_target; pub mod user; diff --git a/public/database/src/generated/entities/prelude.rs b/public/database/src/generated/entities/prelude.rs index 537ed66..9e4d28b 100644 --- a/public/database/src/generated/entities/prelude.rs +++ b/public/database/src/generated/entities/prelude.rs @@ -1,6 +1,8 @@ //! `SeaORM` Entity, @generated by sea-orm-codegen 2.0.0-rc.18 pub use super::config::Entity as Config; +pub use super::location::Entity as Location; +pub use super::proxy_host::Entity as ProxyHost; pub use super::upstream::Entity as Upstream; pub use super::upstream_target::Entity as UpstreamTarget; pub use super::user::Entity as User; diff --git a/public/database/src/generated/entities/proxy_host.rs b/public/database/src/generated/entities/proxy_host.rs new file mode 100644 index 0000000..0b5997f --- /dev/null +++ b/public/database/src/generated/entities/proxy_host.rs @@ -0,0 +1,48 @@ +//! `SeaORM` Entity, @generated by sea-orm-codegen 2.0.0-rc.18 + +use sea_orm::entity::prelude::*; +use serde::{Deserialize, Serialize}; + +#[sea_orm::model] +#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)] +#[sea_orm(table_name = "proxy_host")] +pub struct Model { + #[sea_orm(primary_key, auto_increment = false)] + pub id: Uuid, + pub name: Option, + pub domain: String, + pub scheme: String, + pub listen_port: i64, + pub forward_scheme: String, + pub forward_host: Option, + pub forward_port: Option, + pub preserve_host_header: bool, + pub enable_websocket: bool, + pub enabled: bool, + #[sea_orm(column_type = "JsonBinary", nullable)] + pub meta: Option, + pub default_upstream_id: Option, + pub created_by: Option, + pub created_at: DateTimeUtc, + pub updated_at: DateTimeUtc, + #[sea_orm(has_many)] + pub locations: HasMany, + #[sea_orm( + belongs_to, + from = "default_upstream_id", + to = "id", + on_update = "Cascade", + on_delete = "SetNull" + )] + pub upstream: HasOne, + #[sea_orm( + belongs_to, + from = "created_by", + to = "id", + on_update = "Cascade", + on_delete = "SetNull" + )] + pub user: HasOne, +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/public/database/src/generated/entities/upstream.rs b/public/database/src/generated/entities/upstream.rs index bbbe5de..0d88dd6 100644 --- a/public/database/src/generated/entities/upstream.rs +++ b/public/database/src/generated/entities/upstream.rs @@ -17,6 +17,10 @@ pub struct Model { pub created_at: DateTimeUtc, pub updated_at: DateTimeUtc, #[sea_orm(has_many)] + pub locations: HasMany, + #[sea_orm(has_many)] + pub proxy_hosts: HasMany, + #[sea_orm(has_many)] pub upstream_targets: HasMany, } diff --git a/public/database/src/generated/entities/user.rs b/public/database/src/generated/entities/user.rs index ab2edb2..b40aea5 100644 --- a/public/database/src/generated/entities/user.rs +++ b/public/database/src/generated/entities/user.rs @@ -18,6 +18,8 @@ pub struct Model { pub last_login_at: Option, pub deleted_at: Option, #[sea_orm(has_many)] + pub proxy_hosts: HasMany, + #[sea_orm(has_many)] pub user_identities: HasMany, } diff --git a/public/migration/src/lib.rs b/public/migration/src/lib.rs index f31841c..19a171a 100644 --- a/public/migration/src/lib.rs +++ b/public/migration/src/lib.rs @@ -15,6 +15,8 @@ impl MigratorTrait for Migrator { Box::new(m20251011_000003_create_user_identity_table::Migration), Box::new(m20251223_000004_create_upstream_table::Migration), Box::new(m20251223_000005_create_upstream_target_table::Migration), + Box::new(m20260102_000006_create_proxy_table::Migration), + Box::new(m20260102_000007_create_location_table::Migration), ] } } diff --git a/public/migration/src/migrations.rs b/public/migration/src/migrations.rs index aef516b..370a7ea 100644 --- a/public/migration/src/migrations.rs +++ b/public/migration/src/migrations.rs @@ -3,3 +3,5 @@ pub mod m20251011_000002_create_user_table; pub mod m20251011_000003_create_user_identity_table; pub mod m20251223_000004_create_upstream_table; pub mod m20251223_000005_create_upstream_target_table; +pub mod m20260102_000006_create_proxy_table; +pub mod m20260102_000007_create_location_table; diff --git a/public/migration/src/migrations/m20260102_000006_create_proxy_table.rs b/public/migration/src/migrations/m20260102_000006_create_proxy_table.rs new file mode 100644 index 0000000..bcdd3fa --- /dev/null +++ b/public/migration/src/migrations/m20260102_000006_create_proxy_table.rs @@ -0,0 +1,124 @@ +use sea_orm_migration::{prelude::*, schema::*}; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[forbid(dead_code)] +#[derive(DeriveIden)] +pub enum ProxyHost { + Table, + Id, + Name, + Domain, + Scheme, + ListenPort, + ForwardScheme, + ForwardHost, + ForwardPort, + PreserveHostHeader, + EnableWebsocket, + Enabled, + Meta, + DefaultUpstreamId, + CreatedBy, + CreatedAt, + UpdatedAt, +} + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_table( + Table::create() + .table(ProxyHost::Table) + .if_not_exists() + .col(pk_uuid(ProxyHost::Id)) + .col(ColumnDef::new(ProxyHost::Name).string().null()) + .col(ColumnDef::new(ProxyHost::Domain).string().not_null()) + .col( + ColumnDef::new(ProxyHost::Scheme) + .string() + .default("http") + .not_null(), + ) + .col( + ColumnDef::new(ProxyHost::ListenPort) + .integer() + .default(80) + .not_null(), + ) + .col( + ColumnDef::new(ProxyHost::ForwardScheme) + .string() + .default("http") + .not_null(), + ) + .col(ColumnDef::new(ProxyHost::ForwardHost).string().null()) + .col(ColumnDef::new(ProxyHost::ForwardPort).integer().null()) + .col( + ColumnDef::new(ProxyHost::PreserveHostHeader) + .boolean() + .default(false) + .not_null(), + ) + .col( + ColumnDef::new(ProxyHost::EnableWebsocket) + .boolean() + .default(false) + .not_null(), + ) + .col( + ColumnDef::new(ProxyHost::Enabled) + .boolean() + .default(true) + .not_null(), + ) + .col(ColumnDef::new(ProxyHost::Meta).json_binary().null()) + .col(ColumnDef::new(ProxyHost::DefaultUpstreamId).uuid().null()) + .foreign_key( + ForeignKey::create() + .name("fk-proxy-host-default-upstream-id") + .from(ProxyHost::Table, ProxyHost::DefaultUpstreamId) + .to( + super::m20251223_000004_create_upstream_table::Upstream::Table, + super::m20251223_000004_create_upstream_table::Upstream::Id, + ) + .on_delete(ForeignKeyAction::SetNull) + .on_update(ForeignKeyAction::Cascade), + ) + .col(ColumnDef::new(ProxyHost::CreatedBy).uuid().null()) + .foreign_key( + ForeignKey::create() + .name("fk-proxy-host-created-by") + .from(ProxyHost::Table, ProxyHost::CreatedBy) + .to( + super::m20251011_000002_create_user_table::User::Table, + super::m20251011_000002_create_user_table::User::Id, + ) + .on_delete(ForeignKeyAction::SetNull) + .on_update(ForeignKeyAction::Cascade), + ) + .col( + ColumnDef::new(ProxyHost::CreatedAt) + .timestamp() + .default(SimpleExpr::Keyword(Keyword::CurrentTimestamp)) + .not_null(), + ) + .col( + ColumnDef::new(ProxyHost::UpdatedAt) + .timestamp() + .default(SimpleExpr::Keyword(Keyword::CurrentTimestamp)) + .not_null(), + ) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(ProxyHost::Table).to_owned()) + .await + } +} diff --git a/public/migration/src/migrations/m20260102_000007_create_location_table.rs b/public/migration/src/migrations/m20260102_000007_create_location_table.rs new file mode 100644 index 0000000..31c1ea8 --- /dev/null +++ b/public/migration/src/migrations/m20260102_000007_create_location_table.rs @@ -0,0 +1,115 @@ +use sea_orm_migration::{prelude::*, schema::*}; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[forbid(dead_code)] +#[derive(DeriveIden)] +pub enum Location { + Table, + Id, + HostId, + Path, + MatchType, + Order, + UpstreamId, + ProxyPassProtocol, + ProxyPassHost, + ProxyPassPort, + PreserveHostHeader, + AllowedMethods, + CustomConfig, + Enabled, + CreatedAt, + UpdatedAt, +} + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_table( + Table::create() + .table(Location::Table) + .if_not_exists() + .col(pk_uuid(Location::Id)) + .col(ColumnDef::new(Location::HostId).uuid().not_null()) + .foreign_key( + ForeignKey::create() + .name("fk-location-host-id") + .from(Location::Table, Location::HostId) + .to( + super::m20260102_000006_create_proxy_table::ProxyHost::Table, + super::m20260102_000006_create_proxy_table::ProxyHost::Id, + ) + .on_delete(ForeignKeyAction::Cascade) + .on_update(ForeignKeyAction::Cascade), + ) + .col(ColumnDef::new(Location::Path).string().not_null()) + .col( + ColumnDef::new(Location::MatchType) + .string() + .default("prefix") + .not_null(), + ) + .col( + ColumnDef::new(Location::Order) + .integer() + .default(0) + .not_null(), + ) + .col(ColumnDef::new(Location::UpstreamId).uuid().null()) + .foreign_key( + ForeignKey::create() + .name("fk-location-upstream-id") + .from(Location::Table, Location::UpstreamId) + .to( + super::m20251223_000004_create_upstream_table::Upstream::Table, + super::m20251223_000004_create_upstream_table::Upstream::Id, + ) + .on_delete(ForeignKeyAction::SetNull) + .on_update(ForeignKeyAction::Cascade), + ) + .col(ColumnDef::new(Location::ProxyPassProtocol).string().null()) + .col(ColumnDef::new(Location::ProxyPassHost).string().null()) + .col(ColumnDef::new(Location::ProxyPassPort).integer().null()) + .col( + ColumnDef::new(Location::PreserveHostHeader) + .boolean() + .null(), + ) + .col( + ColumnDef::new(Location::AllowedMethods) + .json_binary() + .null(), + ) + .col(ColumnDef::new(Location::CustomConfig).text().null()) + .col( + ColumnDef::new(Location::Enabled) + .boolean() + .default(true) + .not_null(), + ) + .col( + ColumnDef::new(Location::CreatedAt) + .timestamp() + .default(SimpleExpr::Keyword(Keyword::CurrentTimestamp)) + .not_null(), + ) + .col( + ColumnDef::new(Location::UpdatedAt) + .timestamp() + .default(SimpleExpr::Keyword(Keyword::CurrentTimestamp)) + .not_null(), + ) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(Location::Table).to_owned()) + .await + } +}