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.
3use serde::{Serialize, Deserialize};
4use std::collections::HashMap;
5/*▗▄▄▄▖▗▖ ▗▖▗▄▄▖ ▗▄▄▄▖ ▗▄▄▖
6 █ ▝▚▞▘ ▐▌ ▐▌▐▌ ▐▌
7 █ ▐▌ ▐▛▀▘ ▐▛▀▀▘ ▝▀▚▖
8 █ ▐▌ ▐▌ ▐▙▄▄▖▗▄▄▞▘
9This module defines the `EnvRequest` enum, which encapsulates different types of requests. */
10/// A request descriptor representing the source of a request.
11/// This enum abstracts various ingress types (e.g., HTTP, WebSocket, CLI)
12/// into a unified request structure for environment recognition and handling.
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub enum EnvRequest {
15 /// HTTP-based request (e.g., Actix, Express, Hyper, etc.)
16 Http(EnvRequestHttp),
17 /// WebSocket-based request
18 Ws(EnvRequestWs),
19 /// Command-line or programmatic trigger
20 Cli(EnvRequestCli),
21}
22/*▗▄▄▖▗▄▄▄▖▗▄▄▖ ▗▖ ▗▖ ▗▄▄▖▗▄▄▄▖▗▖ ▗▖▗▄▄▖ ▗▄▄▄▖ ▗▄▄▖
23 ▐▌ █ ▐▌ ▐▌▐▌ ▐▌▐▌ █ ▐▌ ▐▌▐▌ ▐▌▐▌ ▐▌
24 ▝▀▚▖ █ ▐▛▀▚▖▐▌ ▐▌▐▌ █ ▐▌ ▐▌▐▛▀▚▖▐▛▀▀▘ ▝▀▚▖
25 ▗▄▄▞▘ █ ▐▌ ▐▌▝▚▄▞▘▝▚▄▄▖ █ ▝▚▄▞▘▐▌ ▐▌▐▙▄▄▖▗▄▄▞▘*/
26///this.env - Environment Request Types
27///This module defines the request types used by this.env to handle various ingress sources.
28///It includes HTTP, WebSocket, and CLI requests, each with its own structure.
29///These types are used to standardize how requests are processed and analyzed within the this.env framework.
30///This allows for consistent handling of requests across different protocols and frameworks,
31///making it easier to implement environment recognition, routing, and analytics.
32///This module is designed to be extensible, allowing for future ingress types to be added as needed. */
33/// A request made through HTTP, carrying host, path, headers and metadata.
34/*
35▗▖ ▗▖▗▄▄▄▖▗▄▄▄▖▗▄▄▖
36▐▌ ▐▌ █ █ ▐▌ ▐▌
37▐▛▀▜▌ █ █ ▐▛▀▘
38▐▌ ▐▌ █ █ ▐▌ */
39/// Represents an HTTP request with metadata and headers.
40#[derive(Debug, Clone, Serialize, Deserialize)]
41pub struct EnvRequestHttp {
42 /// Host header or domain of the incoming request.
43 pub host: String,
44 /// Optional remote IP address, if available.
45 pub ip: Option<String>,
46 /// HTTP method (e.g., GET, POST).
47 pub method: String,
48 /// Request path (e.g., /api/data).
49 pub path: String,
50 /// Headers from the incoming request, flattened as string pairs.
51 pub headers: HashMap<String, String>,
52}
53/*
54▗▖ ▗▖▗▄▄▄▖▗▄▄▖ ▗▄▄▖ ▗▄▖ ▗▄▄▖▗▖ ▗▖▗▄▄▄▖▗▄▄▄▖
55▐▌ ▐▌▐▌ ▐▌ ▐▌▐▌ ▐▌ ▐▌▐▌ ▐▌▗▞▘▐▌ █
56▐▌ ▐▌▐▛▀▀▘▐▛▀▚▖ ▝▀▚▖▐▌ ▐▌▐▌ ▐▛▚▖ ▐▛▀▀▘ █
57▐▙█▟▌▐▙▄▄▖▐▙▄▞▘▗▄▄▞▘▝▚▄▞▘▝▚▄▄▖▐▌ ▐▌▐▙▄▄▖ █ */
58/// A WebSocket-based request, including connection metadata and payload.
59#[derive(Debug, Clone, Serialize, Deserialize)]
60pub struct EnvRequestWs {
61 /// Host header or domain from the websocket upgrade request.
62 pub host: String,
63 /// Optional IP address of the connecting client.
64 pub ip: Option<String>,
65 /// Flattened headers at the moment of upgrade.
66 pub headers: HashMap<String, String>,
67 /// Optional string-based payload received.
68 pub payload: Option<String>,
69}
70/*
71 ▗▄▄▖▗▖ ▗▄▄▄▖
72▐▌ ▐▌ █
73▐▌ ▐▌ █
74▝▚▄▄▖▐▙▄▄▖▗▄█▄▖*/
75/// A CLI-triggered request, representing non-network system events or invocations.
76#[derive(Debug, Clone, Serialize, Deserialize)]
77pub struct EnvRequestCli {
78 /// The command invoked from CLI or automation.
79 pub command: String,
80 /// Arguments passed to the command.
81 pub args: Vec<String>,
82 /// Environment variables or runtime context.
83 pub env: HashMap<String, String>,
84}