From a38fd2bbb17727da41b58cc42303b44ea1cd3921 Mon Sep 17 00:00:00 2001 From: Eugene Pankov Date: Thu, 18 May 2023 21:57:55 +0200 Subject: [PATCH] ui: added search boxes - #761 --- warpgate-admin/src/api/roles.rs | 18 +- warpgate-admin/src/api/targets.rs | 16 +- warpgate-admin/src/api/users.rs | 16 +- .../src/api/targets_list.rs | 12 +- warpgate-web/src/admin/Config.svelte | 159 +++++++++--------- warpgate-web/src/admin/Home.svelte | 2 +- .../src/admin/lib/openapi-schema.json | 38 ++++- warpgate-web/src/common/ItemList.svelte | 53 +++++- warpgate-web/src/gateway/TargetList.svelte | 120 ++++++------- .../src/gateway/lib/openapi-schema.json | 14 +- 10 files changed, 280 insertions(+), 168 deletions(-) diff --git a/warpgate-admin/src/api/roles.rs b/warpgate-admin/src/api/roles.rs index ca943d5..1e31558 100644 --- a/warpgate-admin/src/api/roles.rs +++ b/warpgate-admin/src/api/roles.rs @@ -1,10 +1,13 @@ use std::sync::Arc; use poem::web::Data; -use poem_openapi::param::Path; +use poem_openapi::param::{Path, Query}; use poem_openapi::payload::Json; use poem_openapi::{ApiResponse, Object, OpenApi}; -use sea_orm::{ActiveModelTrait, DatabaseConnection, EntityTrait, ModelTrait, QueryOrder, Set}; +use sea_orm::{ + ActiveModelTrait, ColumnTrait, DatabaseConnection, EntityTrait, ModelTrait, QueryFilter, + QueryOrder, Set, +}; use tokio::sync::Mutex; use uuid::Uuid; use warpgate_common::{Role as RoleConfig, WarpgateError}; @@ -38,11 +41,18 @@ impl ListApi { async fn api_get_all_roles( &self, db: Data<&Arc>>, + search: Query>, ) -> poem::Result { let db = db.lock().await; - let roles = Role::Entity::find() - .order_by_asc(Role::Column::Name) + let mut roles = Role::Entity::find().order_by_asc(Role::Column::Name); + + if let Some(ref search) = *search { + let search = format!("%{}%", search); + roles = roles.filter(Role::Column::Name.like(&*search)); + } + + let roles = roles .all(&*db) .await .map_err(poem::error::InternalServerError)?; diff --git a/warpgate-admin/src/api/targets.rs b/warpgate-admin/src/api/targets.rs index b4f3d1d..43f0323 100644 --- a/warpgate-admin/src/api/targets.rs +++ b/warpgate-admin/src/api/targets.rs @@ -1,7 +1,7 @@ use std::sync::Arc; use poem::web::Data; -use poem_openapi::param::Path; +use poem_openapi::param::{Path, Query}; use poem_openapi::payload::Json; use poem_openapi::{ApiResponse, Object, OpenApi}; use sea_orm::{ @@ -43,14 +43,18 @@ impl ListApi { async fn api_get_all_targets( &self, db: Data<&Arc>>, + search: Query>, ) -> poem::Result { let db = db.lock().await; - let targets = Target::Entity::find() - .order_by_asc(Target::Column::Name) - .all(&*db) - .await - .map_err(WarpgateError::from)?; + let mut targets = Target::Entity::find().order_by_asc(Target::Column::Name); + + if let Some(ref search) = *search { + let search = format!("%{}%", search); + targets = targets.filter(Target::Column::Name.like(&*search)); + } + + let targets = targets.all(&*db).await.map_err(WarpgateError::from)?; let targets: Result, _> = targets.into_iter().map(|t| t.try_into()).collect(); diff --git a/warpgate-admin/src/api/users.rs b/warpgate-admin/src/api/users.rs index 48eecb0..df589b8 100644 --- a/warpgate-admin/src/api/users.rs +++ b/warpgate-admin/src/api/users.rs @@ -1,7 +1,7 @@ use std::sync::Arc; use poem::web::Data; -use poem_openapi::param::Path; +use poem_openapi::param::{Path, Query}; use poem_openapi::payload::Json; use poem_openapi::{ApiResponse, Object, OpenApi}; use sea_orm::{ @@ -46,14 +46,18 @@ impl ListApi { async fn api_get_all_users( &self, db: Data<&Arc>>, + search: Query>, ) -> poem::Result { let db = db.lock().await; - let users = User::Entity::find() - .order_by_asc(User::Column::Username) - .all(&*db) - .await - .map_err(WarpgateError::from)?; + let mut users = User::Entity::find().order_by_asc(User::Column::Username); + + if let Some(ref search) = *search { + let search = format!("%{}%", search); + users = users.filter(User::Column::Username.like(&*search)); + } + + let users = users.all(&*db).await.map_err(WarpgateError::from)?; let users: Result, _> = users.into_iter().map(|t| t.try_into()).collect(); let users = users.map_err(WarpgateError::from)?; diff --git a/warpgate-protocol-http/src/api/targets_list.rs b/warpgate-protocol-http/src/api/targets_list.rs index d58c405..019c9ec 100644 --- a/warpgate-protocol-http/src/api/targets_list.rs +++ b/warpgate-protocol-http/src/api/targets_list.rs @@ -1,5 +1,6 @@ use futures::{stream, StreamExt}; use poem::web::Data; +use poem_openapi::param::Query; use poem_openapi::payload::Json; use poem_openapi::{ApiResponse, Object, OpenApi}; use serde::Serialize; @@ -36,11 +37,20 @@ impl Api { &self, services: Data<&Services>, auth: Data<&SessionAuthorization>, + search: Query>, ) -> poem::Result { - let targets = { + let mut targets = { let mut config_provider = services.config_provider.lock().await; config_provider.list_targets().await? }; + + if let Some(ref search) = *search { + targets = targets + .into_iter() + .filter(|t| t.name.contains(search)) + .collect() + } + let mut targets = stream::iter(targets) .filter(|t| { let services = services.clone(); diff --git a/warpgate-web/src/admin/Config.svelte b/warpgate-web/src/admin/Config.svelte index 7c5a7d7..eed26a7 100644 --- a/warpgate-web/src/admin/Config.svelte +++ b/warpgate-web/src/admin/Config.svelte @@ -1,8 +1,38 @@
@@ -17,39 +47,32 @@ import { Alert } from 'sveltestrap'
- {#await api.getTargets()} - - {:then targets} - - {:catch error} - {error} - {/await} + + + + + {target.name} + + + {#if target.options.kind === 'Http'} + HTTP + {/if} + {#if target.options.kind === 'MySql'} + MySQL + {/if} + {#if target.options.kind === 'Ssh'} + SSH + {/if} + {#if target.options.kind === 'WebAdmin'} + This web admin interface + {/if} + + +
@@ -63,25 +86,18 @@ import { Alert } from 'sveltestrap'
- {#await api.getUsers()} - - {:then users} -
- {#each users as user} - - - - {user.username} - - - {/each} -
- {:catch error} - {error} - {/await} + + + + + {user.username} + + +

Roles

@@ -93,25 +109,18 @@ import { Alert } from 'sveltestrap'
- {#await api.getRoles()} - - {:then roles} -
- {#each roles as role} - - - - {role.name} - - - {/each} -
- {:catch error} - {error} - {/await} + + + + + {role.name} + + + diff --git a/warpgate-web/src/admin/Home.svelte b/warpgate-web/src/admin/Home.svelte index 45e2c85..cb12458 100644 --- a/warpgate-web/src/admin/Home.svelte +++ b/warpgate-web/src/admin/Home.svelte @@ -80,7 +80,7 @@ {/if} -
+
diff --git a/warpgate-web/src/admin/lib/openapi-schema.json b/warpgate-web/src/admin/lib/openapi-schema.json index 46b08bc..b7288cc 100644 --- a/warpgate-web/src/admin/lib/openapi-schema.json +++ b/warpgate-web/src/admin/lib/openapi-schema.json @@ -2,7 +2,7 @@ "openapi": "3.0.0", "info": { "title": "Warpgate Web Admin", - "version": "0.7.0" + "version": "0.7.1" }, "servers": [ { @@ -207,6 +207,18 @@ }, "/roles": { "get": { + "parameters": [ + { + "name": "search", + "schema": { + "type": "string" + }, + "in": "query", + "required": false, + "deprecated": false, + "explode": true + } + ], "responses": { "200": { "description": "", @@ -366,6 +378,18 @@ }, "/targets": { "get": { + "parameters": [ + { + "name": "search", + "schema": { + "type": "string" + }, + "in": "query", + "required": false, + "deprecated": false, + "explode": true + } + ], "responses": { "200": { "description": "", @@ -636,6 +660,18 @@ }, "/users": { "get": { + "parameters": [ + { + "name": "search", + "schema": { + "type": "string" + }, + "in": "query", + "required": false, + "deprecated": false, + "explode": true + } + ], "responses": { "200": { "description": "", diff --git a/warpgate-web/src/common/ItemList.svelte b/warpgate-web/src/common/ItemList.svelte index 53746a0..240b831 100644 --- a/warpgate-web/src/common/ItemList.svelte +++ b/warpgate-web/src/common/ItemList.svelte @@ -1,7 +1,8 @@ + {#await $items} {:then items} {#if items} -
{#each items as item} @@ -63,10 +94,16 @@ {:else} {/if} + + {#if filter && loaded && !items?.length} + + Nothing found + + {/if} {/await} {#await $total then total} - {#if total > pageSize} + {#if pageSize && total > pageSize} {/if} {/await} diff --git a/warpgate-web/src/gateway/TargetList.svelte b/warpgate-web/src/gateway/TargetList.svelte index ed1b061..2f63cb0 100644 --- a/warpgate-web/src/gateway/TargetList.svelte +++ b/warpgate-web/src/gateway/TargetList.svelte @@ -1,100 +1,90 @@ -{#if targets} - -{:else} - -{/if} + + + {#if target.kind === TargetKind.Ssh} + SSH + {/if} + {#if target.kind === TargetKind.MySql} + MySQL + {/if} + + {#if target.kind === TargetKind.Http || target.kind === TargetKind.WebAdmin} + + {/if} + + selectedTarget = undefined}> selectedTarget = undefined}> diff --git a/warpgate-web/src/gateway/lib/openapi-schema.json b/warpgate-web/src/gateway/lib/openapi-schema.json index 6fb5c5a..eef609f 100644 --- a/warpgate-web/src/gateway/lib/openapi-schema.json +++ b/warpgate-web/src/gateway/lib/openapi-schema.json @@ -2,7 +2,7 @@ "openapi": "3.0.0", "info": { "title": "Warpgate HTTP proxy", - "version": "0.7.0" + "version": "0.7.1" }, "servers": [ { @@ -237,6 +237,18 @@ }, "/targets": { "get": { + "parameters": [ + { + "name": "search", + "schema": { + "type": "string" + }, + "in": "query", + "required": false, + "deprecated": false, + "explode": true + } + ], "responses": { "200": { "description": "",