diff --git a/crates/bsnext_client/dist/index.js b/crates/bsnext_client/dist/index.js index 90db39e..d01b865 100644 --- a/crates/bsnext_client/dist/index.js +++ b/crates/bsnext_client/dist/index.js @@ -6259,6 +6259,51 @@ var ChangeKind = /* @__PURE__ */ ((ChangeKind2) => { })(ChangeKind || {}); // generated/schema.ts +var routeKindDTOSchema = z.union([ + z.object({ + kind: z.literal("Html"), + payload: z.object({ + html: z.string() + }) + }), + z.object({ + kind: z.literal("Json"), + payload: z.object({ + json_str: z.string() + }) + }), + z.object({ + kind: z.literal("Raw"), + payload: z.object({ + raw: z.string() + }) + }), + z.object({ + kind: z.literal("Sse"), + payload: z.object({ + sse: z.string() + }) + }), + z.object({ + kind: z.literal("Proxy"), + payload: z.object({ + proxy: z.string() + }) + }), + z.object({ + kind: z.literal("Dir"), + payload: z.object({ + dir: z.string() + }) + }) +]); +var routeDTOSchema = z.object({ + path: z.string(), + kind: routeKindDTOSchema +}); +var serverDescSchema = z.object({ + routes: z.array(routeDTOSchema) +}); var identityDTOSchema = z.union([ z.object({ kind: z.literal("Both"), @@ -6280,12 +6325,12 @@ var identityDTOSchema = z.union([ }) }) ]); -var serversDTOSchema = z.object({ +var serverDTOSchema = z.object({ identity: identityDTOSchema, socket_addr: z.string() }); var getServersMessageResponseSchema = z.object({ - servers: z.array(serversDTOSchema) + servers: z.array(serverDTOSchema) }); var serverChangeSchema = z.union([ z.object({ diff --git a/crates/bsnext_client/generated/dto.ts b/crates/bsnext_client/generated/dto.ts index 0629f7a..6f6cc2d 100644 --- a/crates/bsnext_client/generated/dto.ts +++ b/crates/bsnext_client/generated/dto.ts @@ -2,6 +2,35 @@ Generated by typeshare 1.9.2 */ +export type RouteKindDTO = + | { kind: "Html", payload: { + html: string; +}} + | { kind: "Json", payload: { + json_str: string; +}} + | { kind: "Raw", payload: { + raw: string; +}} + | { kind: "Sse", payload: { + sse: string; +}} + | { kind: "Proxy", payload: { + proxy: string; +}} + | { kind: "Dir", payload: { + dir: string; +}}; + +export interface RouteDTO { + path: string; + kind: RouteKindDTO; +} + +export interface ServerDesc { + routes: RouteDTO[]; +} + export type IdentityDTO = | { kind: "Both", payload: { name: string; @@ -14,13 +43,13 @@ export type IdentityDTO = name: string; }}; -export interface ServersDTO { +export interface ServerDTO { identity: IdentityDTO; socket_addr: string; } export interface GetServersMessageResponse { - servers: ServersDTO[]; + servers: ServerDTO[]; } export type ServerChange = diff --git a/crates/bsnext_client/generated/schema.ts b/crates/bsnext_client/generated/schema.ts index 8d94fe7..8f1add5 100644 --- a/crates/bsnext_client/generated/schema.ts +++ b/crates/bsnext_client/generated/schema.ts @@ -2,6 +2,54 @@ import { z } from "zod"; import { EventLevel, ChangeKind, ChangeDTO } from "./dto"; +export const routeKindDTOSchema = z.union([ + z.object({ + kind: z.literal("Html"), + payload: z.object({ + html: z.string(), + }), + }), + z.object({ + kind: z.literal("Json"), + payload: z.object({ + json_str: z.string(), + }), + }), + z.object({ + kind: z.literal("Raw"), + payload: z.object({ + raw: z.string(), + }), + }), + z.object({ + kind: z.literal("Sse"), + payload: z.object({ + sse: z.string(), + }), + }), + z.object({ + kind: z.literal("Proxy"), + payload: z.object({ + proxy: z.string(), + }), + }), + z.object({ + kind: z.literal("Dir"), + payload: z.object({ + dir: z.string(), + }), + }), +]); + +export const routeDTOSchema = z.object({ + path: z.string(), + kind: routeKindDTOSchema, +}); + +export const serverDescSchema = z.object({ + routes: z.array(routeDTOSchema), +}); + export const identityDTOSchema = z.union([ z.object({ kind: z.literal("Both"), @@ -24,13 +72,13 @@ export const identityDTOSchema = z.union([ }), ]); -export const serversDTOSchema = z.object({ +export const serverDTOSchema = z.object({ identity: identityDTOSchema, socket_addr: z.string(), }); export const getServersMessageResponseSchema = z.object({ - servers: z.array(serversDTOSchema), + servers: z.array(serverDTOSchema), }); export const serverChangeSchema = z.union([ diff --git a/crates/bsnext_core/src/dto.rs b/crates/bsnext_core/src/dto.rs index 831c392..32c626f 100644 --- a/crates/bsnext_core/src/dto.rs +++ b/crates/bsnext_core/src/dto.rs @@ -6,8 +6,66 @@ use std::path::Path; use crate::server::handler_change::{Change, ChangeKind}; use bsnext_fs::Debounce; +use bsnext_input::route::{DirRoute, ProxyRoute, Route, RouteKind}; use typeshare::typeshare; +#[typeshare] +#[derive(Debug, serde::Serialize)] +pub struct ServerDesc { + pub routes: Vec, +} + +#[typeshare] +#[derive(Debug, serde::Serialize)] +pub struct RouteDTO { + pub path: String, + pub kind: RouteKindDTO, +} + +impl From for RouteDTO { + fn from(value: Route) -> Self { + Self { + path: value.path.to_owned(), + kind: RouteKindDTO::from(value.kind), + } + } +} +impl From<&Route> for RouteDTO { + fn from(value: &Route) -> Self { + Self { + path: value.path.to_owned(), + kind: RouteKindDTO::from(value.kind.clone()), + } + } +} + +#[typeshare] +#[derive(Debug, serde::Serialize)] +#[serde(tag = "kind", content = "payload")] +pub enum RouteKindDTO { + Html { html: String }, + Json { json_str: String }, + Raw { raw: String }, + Sse { sse: String }, + Proxy { proxy: String }, + Dir { dir: String }, +} + +impl From for RouteKindDTO { + fn from(value: RouteKind) -> Self { + match value { + RouteKind::Html { html } => RouteKindDTO::Html { html }, + RouteKind::Json { json } => RouteKindDTO::Json { + json_str: serde_json::to_string(&json).expect("unreachable"), + }, + RouteKind::Raw { raw } => RouteKindDTO::Raw { raw }, + RouteKind::Sse { sse } => RouteKindDTO::Sse { sse }, + RouteKind::Proxy(ProxyRoute { proxy }) => RouteKindDTO::Proxy { proxy }, + RouteKind::Dir(DirRoute { dir }) => RouteKindDTO::Dir { dir }, + } + } +} + #[typeshare] #[derive(Debug, serde::Serialize)] pub struct ServersStarted { @@ -157,12 +215,12 @@ pub struct ServerChangeSet { #[typeshare::typeshare] #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, MessageResponse)] pub struct GetServersMessageResponse { - pub servers: Vec, + pub servers: Vec, } #[typeshare::typeshare] #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] -pub struct ServersDTO { +pub struct ServerDTO { pub identity: IdentityDTO, pub socket_addr: String, } diff --git a/crates/bsnext_core/src/not_found/not_found_service.rs b/crates/bsnext_core/src/not_found/not_found_service.rs index e7cb4d4..3cc8d6f 100644 --- a/crates/bsnext_core/src/not_found/not_found_service.rs +++ b/crates/bsnext_core/src/not_found/not_found_service.rs @@ -8,6 +8,7 @@ use http::header::CONTENT_TYPE; use http::{HeaderValue, StatusCode}; use mime_guess::mime; +use crate::dto::{RouteDTO, ServerDesc}; use crate::handlers::proxy::AnyAppError; use crate::not_found::route_list::create_route_list_html; use crate::server::state::ServerState; @@ -27,7 +28,10 @@ pub async fn not_found_loader( }; let routes = state.routes.read().await; - let markup = create_route_list_html(&routes); + let dto = ServerDesc { + routes: routes.iter().map(|r| RouteDTO::from(r)).collect(), + }; + let markup = create_route_list_html(&dto); Ok(( StatusCode::NOT_FOUND, diff --git a/crates/bsnext_core/src/not_found/route_list.rs b/crates/bsnext_core/src/not_found/route_list.rs index 6fb14b0..b0c57df 100644 --- a/crates/bsnext_core/src/not_found/route_list.rs +++ b/crates/bsnext_core/src/not_found/route_list.rs @@ -1,7 +1,7 @@ -use bsnext_input::route::{DirRoute, ProxyRoute, Route, RouteKind}; +use crate::dto::{RouteKindDTO, ServerDesc}; use htmlescape::encode_minimal; -pub fn create_route_list_html(routes: &[Route]) -> String { +pub fn create_route_list_html(server_desc: &ServerDesc) -> String { let wrapper = include_str!("not_found.html"); let mut markup = String::from(""); markup.push_str( @@ -14,17 +14,17 @@ pub fn create_route_list_html(routes: &[Route]) -> String { "##, ); - for x in routes.iter() { + for x in server_desc.routes.iter() { let mut item = String::from(""); let kind = match &x.kind { - RouteKind::Html { .. } => String::from("RouteKind::Html"), - RouteKind::Json { .. } => String::from("RouteKind::Json"), - RouteKind::Raw { .. } => String::from("RouteKind::Raw"), - RouteKind::Sse { .. } => String::from("RouteKind::Sse"), - RouteKind::Proxy(ProxyRoute { proxy }) => { + RouteKindDTO::Html { .. } => String::from("RouteKind::Html"), + RouteKindDTO::Json { .. } => String::from("RouteKind::Json"), + RouteKindDTO::Raw { .. } => String::from("RouteKind::Raw"), + RouteKindDTO::Sse { .. } => String::from("RouteKind::Sse"), + RouteKindDTO::Proxy { proxy } => { format!("RouteKind::Proxy('{}')", proxy) } - RouteKind::Dir(DirRoute { dir }) => { + RouteKindDTO::Dir { dir } => { format!("RouteKind::Dir('{}')", dir.clone()) } }; @@ -42,7 +42,7 @@ pub fn create_route_list_html(routes: &[Route]) -> String { } markup.push_str("
"); markup.push_str("
");
-    let json = serde_json::to_string_pretty(routes).expect("serde");
+    let json = serde_json::to_string_pretty(&server_desc).expect("serde");
     markup.push_str(&encode_minimal(&json));
     markup.push_str("
"); wrapper.replace("{{route_list}}", markup.as_str()) diff --git a/crates/bsnext_core/src/server/router/mod.rs b/crates/bsnext_core/src/server/router/mod.rs index 04ca1ef..fb89e7b 100644 --- a/crates/bsnext_core/src/server/router/mod.rs +++ b/crates/bsnext_core/src/server/router/mod.rs @@ -21,6 +21,7 @@ use tower::ServiceBuilder; use tower_http::catch_panic::CatchPanicLayer; use crate::dir_loader::serve_dir_loader; +use crate::dto::{RouteDTO, ServerDesc}; use crate::meta::MetaData; use crate::not_found::not_found_service::not_found_loader; use crate::not_found::route_list::create_route_list_html; @@ -49,7 +50,10 @@ pub fn built_ins(state: Arc) -> Router { async fn handler(State(app): State>, _uri: Uri) -> impl IntoResponse { // let v = app.lock().await; let routes = app.routes.read().await; - let markup = create_route_list_html(&routes); + let dto = ServerDesc { + routes: routes.iter().map(|r| RouteDTO::from(r)).collect(), + }; + let markup = create_route_list_html(&dto); ( [( CONTENT_TYPE, diff --git a/crates/bsnext_core/src/servers_supervisor/get_servers_handler.rs b/crates/bsnext_core/src/servers_supervisor/get_servers_handler.rs index 3db9493..79ea66c 100644 --- a/crates/bsnext_core/src/servers_supervisor/get_servers_handler.rs +++ b/crates/bsnext_core/src/servers_supervisor/get_servers_handler.rs @@ -1,4 +1,4 @@ -use crate::dto::{GetServersMessageResponse, ServersDTO}; +use crate::dto::{GetServersMessageResponse, ServerDTO}; use crate::servers_supervisor::actor::ServersSupervisor; #[derive(actix::Message)] @@ -13,7 +13,7 @@ impl actix::Handler for ServersSupervisor { servers: self .handlers .iter() - .map(|(identity, child_handler)| ServersDTO { + .map(|(identity, child_handler)| ServerDTO { identity: identity.into(), socket_addr: child_handler.socket_addr.to_string(), })