this_env/middleware/env_request.rs
1// this.env/crate/src/middleware/env_request.rs by suiGn
2// Module for standardizing inbound request translation to internal EnvRequest enums.
3// env_request.rs only declares the portable structs/enums that
4// describe an incoming request in a framework-neutral way.
5use serde::{Serialize, Deserialize};
6use std::collections::HashMap;
7/*▗▄▄▄▖▗▖ ▗▖▗▄▄▖ ▗▄▄▄▖ ▗▄▄▖
8 █ ▝▚▞▘ ▐▌ ▐▌▐▌ ▐▌
9 █ ▐▌ ▐▛▀▘ ▐▛▀▀▘ ▝▀▚▖
10 █ ▐▌ ▐▌ ▐▙▄▄▖▗▄▄▞▘
11This module defines the `EnvRequest` enum, which encapsulates different types of requests. */
12/// A request descriptor representing the source of a request.
13/// This enum abstracts various ingress types (e.g., HTTP, WebSocket, CLI)
14/// into a unified request structure for environment recognition and handling.
15#[derive(Debug, Clone, Serialize, Deserialize)]
16pub enum EnvRequest {
17 /// HTTP-based request (e.g., Actix, Express, Hyper, etc.)
18 Http(EnvRequestHttp),
19 /// WebSocket-based request
20 Ws(EnvRequestWs),
21 /// Command-line or programmatic trigger
22 Cli(EnvRequestCli),
23}
24
25/// A simplified, serializable form of EnvRequest for response bodies or logs.
26#[derive(Debug, Clone, Serialize, Deserialize)]
27pub struct EnvRequestInfo {
28 pub r#type: String,
29 pub host: String,
30 pub ip: Option<String>,
31 pub method: Option<String>,
32 pub path: Option<String>,
33 pub headers: Option<HashMap<String, String>>,
34 pub args: Option<Vec<String>>,
35}
36
37impl From<&EnvRequest> for EnvRequestInfo {
38 fn from(req: &EnvRequest) -> Self {
39 match req {
40 EnvRequest::Http(http) => EnvRequestInfo {
41 r#type: "http".into(),
42 host: http.host.clone(),
43 ip: http.ip.clone(),
44 method: Some(http.method.clone()),
45 path: Some(http.path.clone()),
46 headers: Some(http.headers.clone()),
47 args: None,
48 },
49 EnvRequest::Ws(ws) => EnvRequestInfo {
50 r#type: "ws".into(),
51 host: ws.host.clone(),
52 ip: ws.ip.clone(),
53 method: None,
54 path: None,
55 headers: Some(ws.headers.clone()),
56 args: None,
57 },
58 EnvRequest::Cli(cli) => EnvRequestInfo {
59 r#type: "cli".into(),
60 host: "localhost".into(),
61 ip: None,
62 method: Some(cli.command.clone()),
63 path: Some(cli.args.join(" ")),
64 headers: None,
65 args: Some(cli.args.clone()),
66 },
67 }
68 }
69}
70/*▗▄▄▖▗▄▄▄▖▗▄▄▖ ▗▖ ▗▖ ▗▄▄▖▗▄▄▄▖▗▖ ▗▖▗▄▄▖ ▗▄▄▄▖ ▗▄▄▖
71 ▐▌ █ ▐▌ ▐▌▐▌ ▐▌▐▌ █ ▐▌ ▐▌▐▌ ▐▌▐▌ ▐▌
72 ▝▀▚▖ █ ▐▛▀▚▖▐▌ ▐▌▐▌ █ ▐▌ ▐▌▐▛▀▚▖▐▛▀▀▘ ▝▀▚▖
73 ▗▄▄▞▘ █ ▐▌ ▐▌▝▚▄▞▘▝▚▄▄▖ █ ▝▚▄▞▘▐▌ ▐▌▐▙▄▄▖▗▄▄▞▘*/
74///this.env - Environment Request Types
75///This module defines the request types used by this.env to handle various ingress sources.
76///It includes HTTP, WebSocket, and CLI requests, each with its own structure.
77///These types are used to standardize how requests are processed and analyzed within the this.env framework.
78///This allows for consistent handling of requests across different protocols and frameworks,
79///making it easier to implement environment recognition, routing, and analytics.
80///This module is designed to be extensible, allowing for future ingress types to be added as needed. */
81/// A request made through HTTP, carrying host, path, headers and metadata.
82/*
83▗▖ ▗▖▗▄▄▄▖▗▄▄▄▖▗▄▄▖
84▐▌ ▐▌ █ █ ▐▌ ▐▌
85▐▛▀▜▌ █ █ ▐▛▀▘
86▐▌ ▐▌ █ █ ▐▌ */
87/// Represents an HTTP request with metadata and headers.
88#[derive(Debug, Clone, Serialize, Deserialize)]
89pub struct EnvRequestHttp {
90 /// Host header or domain of the incoming request.
91 pub host: String,
92 /// Optional remote IP address, if available.
93 pub ip: Option<String>,
94 /// HTTP method (e.g., GET, POST).
95 pub method: String,
96 /// Request path (e.g., /api/data).
97 pub path: String,
98 /// Headers from the incoming request, flattened as string pairs.
99 pub headers: HashMap<String, String>,
100}
101/*
102▗▖ ▗▖▗▄▄▄▖▗▄▄▖ ▗▄▄▖ ▗▄▖ ▗▄▄▖▗▖ ▗▖▗▄▄▄▖▗▄▄▄▖
103▐▌ ▐▌▐▌ ▐▌ ▐▌▐▌ ▐▌ ▐▌▐▌ ▐▌▗▞▘▐▌ █
104▐▌ ▐▌▐▛▀▀▘▐▛▀▚▖ ▝▀▚▖▐▌ ▐▌▐▌ ▐▛▚▖ ▐▛▀▀▘ █
105▐▙█▟▌▐▙▄▄▖▐▙▄▞▘▗▄▄▞▘▝▚▄▞▘▝▚▄▄▖▐▌ ▐▌▐▙▄▄▖ █ */
106/// A WebSocket-based request, including connection metadata and payload.
107#[derive(Debug, Clone, Serialize, Deserialize)]
108pub struct EnvRequestWs {
109 /// Host header or domain from the websocket upgrade request.
110 pub host: String,
111 /// Optional IP address of the connecting client.
112 pub ip: Option<String>,
113 /// Flattened headers at the moment of upgrade.
114 pub headers: HashMap<String, String>,
115 /// Optional string-based payload received.
116 pub payload: Option<String>,
117}
118/*
119 ▗▄▄▖▗▖ ▗▄▄▄▖
120▐▌ ▐▌ █
121▐▌ ▐▌ █
122▝▚▄▄▖▐▙▄▄▖▗▄█▄▖*/
123/// A CLI-triggered request, representing non-network system events or invocations.
124#[derive(Debug, Clone, Serialize, Deserialize, Default)]
125pub struct EnvRequestCli {
126 /// The command invoked from CLI or automation.
127 pub command: String,
128 /// Arguments passed to the command.
129 pub args: Vec<String>,
130 /// Environment variables or runtime context.
131 pub env: HashMap<String, String>,
132}