Skip to content

Commit

Permalink
data prep for a better page exp
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane Osbourne committed May 16, 2024
1 parent eacaa77 commit 41f3ac9
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 22 deletions.
49 changes: 47 additions & 2 deletions crates/bsnext_client/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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({
Expand Down
33 changes: 31 additions & 2 deletions crates/bsnext_client/generated/dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 =
Expand Down
52 changes: 50 additions & 2 deletions crates/bsnext_client/generated/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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([
Expand Down
62 changes: 60 additions & 2 deletions crates/bsnext_core/src/dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RouteDTO>,
}

#[typeshare]
#[derive(Debug, serde::Serialize)]
pub struct RouteDTO {
pub path: String,
pub kind: RouteKindDTO,
}

impl From<Route> 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<RouteKind> 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 {
Expand Down Expand Up @@ -157,12 +215,12 @@ pub struct ServerChangeSet {
#[typeshare::typeshare]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, MessageResponse)]
pub struct GetServersMessageResponse {
pub servers: Vec<ServersDTO>,
pub servers: Vec<ServerDTO>,
}

#[typeshare::typeshare]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ServersDTO {
pub struct ServerDTO {
pub identity: IdentityDTO,
pub socket_addr: String,
}
Expand Down
6 changes: 5 additions & 1 deletion crates/bsnext_core/src/not_found/not_found_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
20 changes: 10 additions & 10 deletions crates/bsnext_core/src/not_found/route_list.rs
Original file line number Diff line number Diff line change
@@ -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("<table>");
markup.push_str(
Expand All @@ -14,17 +14,17 @@ pub fn create_route_list_html(routes: &[Route]) -> String {
</thead>
"##,
);
for x in routes.iter() {
for x in server_desc.routes.iter() {
let mut item = String::from("<tr>");
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())
}
};
Expand All @@ -42,7 +42,7 @@ pub fn create_route_list_html(routes: &[Route]) -> String {
}
markup.push_str("</table>");
markup.push_str("<pre><code>");
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("</pre></code>");
wrapper.replace("{{route_list}}", markup.as_str())
Expand Down
6 changes: 5 additions & 1 deletion crates/bsnext_core/src/server/router/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,7 +50,10 @@ pub fn built_ins(state: Arc<ServerState>) -> Router {
async fn handler(State(app): State<Arc<ServerState>>, _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,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::dto::{GetServersMessageResponse, ServersDTO};
use crate::dto::{GetServersMessageResponse, ServerDTO};
use crate::servers_supervisor::actor::ServersSupervisor;

#[derive(actix::Message)]
Expand All @@ -13,7 +13,7 @@ impl actix::Handler<GetServersMessage> 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(),
})
Expand Down

0 comments on commit 41f3ac9

Please sign in to comment.