Spaces:
Runtime error
Runtime error
File size: 4,416 Bytes
9552aa0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | use std::error::Error;
use crate::ferron_common::{
ErrorLogger, HyperUpgraded, RequestData, ResponseData, ServerConfig, ServerModule,
ServerModuleHandlers, SocketData,
};
use crate::ferron_common::{HyperResponse, WithRuntime};
use async_trait::async_trait;
use http_body_util::{BodyExt, Full};
use hyper::Response;
use hyper_tungstenite::HyperWebsocket;
use tokio::runtime::Handle;
// Define a struct for the module implementation
struct ExampleModule;
/// Initializes the server module and returns an instance of `ExampleModule`.
pub fn server_module_init(
_config: &ServerConfig, // This is YAML configuration parsed as-is.
) -> Result<Box<dyn ServerModule + Send + Sync>, Box<dyn Error + Send + Sync>> {
Ok(Box::new(ExampleModule::new()))
}
impl ExampleModule {
/// Creates a new instance of `ExampleModule`.
fn new() -> Self {
ExampleModule
}
}
/// Implements the `ServerModule` trait for `ExampleModule`.
impl ServerModule for ExampleModule {
/// Returns an instance of `ExampleModuleHandlers` to handle HTTP requests.
fn get_handlers(&self, handle: Handle) -> Box<dyn ServerModuleHandlers + Send> {
Box::new(ExampleModuleHandlers { handle })
}
}
// Define a struct to handle HTTP requests
struct ExampleModuleHandlers {
handle: Handle,
}
/// Implements the `ServerModuleHandlers` trait for `ExampleModuleHandlers`.
#[async_trait]
impl ServerModuleHandlers for ExampleModuleHandlers {
/// Handles incoming HTTP requests.
/// If the request path is `/hello`, it responds with "Hello World!".
async fn request_handler(
&mut self,
request: RequestData,
_config: &ServerConfig,
_socket_data: &SocketData,
_error_logger: &ErrorLogger,
) -> Result<ResponseData, Box<dyn Error + Send + Sync>> {
WithRuntime::new(self.handle.clone(), async move {
if request.get_hyper_request().uri().path() == "/hello" {
Ok(
ResponseData::builder(request)
.response(
Response::builder().body(
Full::new("Hello World!".into())
.map_err(|e| match e {})
.boxed(),
)?,
)
.build(),
)
} else {
Ok(ResponseData::builder(request).build())
}
})
.await
}
/// Handles non-CONNECT proxy requests (not used in this module).
async fn proxy_request_handler(
&mut self,
request: RequestData,
_config: &ServerConfig,
_socket_data: &SocketData,
_error_logger: &ErrorLogger,
) -> Result<ResponseData, Box<dyn Error + Send + Sync>> {
// No proxy request handling needed.
Ok(ResponseData::builder(request).build())
}
/// Modifies outgoing responses (not used in this module).
async fn response_modifying_handler(
&mut self,
response: HyperResponse,
) -> Result<HyperResponse, Box<dyn Error + Send + Sync>> {
// No response modification needed.
Ok(response)
}
/// Modifies outgoing proxy responses (not used in this module).
async fn proxy_response_modifying_handler(
&mut self,
response: HyperResponse,
) -> Result<HyperResponse, Box<dyn Error + Send + Sync>> {
// No proxy response modification needed.
Ok(response)
}
/// Handles CONNECT proxy requests (not used in this module).
async fn connect_proxy_request_handler(
&mut self,
_upgraded_request: HyperUpgraded,
_connect_address: &str,
_config: &ServerConfig,
_socket_data: &SocketData,
_error_logger: &ErrorLogger,
) -> Result<(), Box<dyn Error + Send + Sync>> {
// No proxy request handling needed.
Ok(())
}
/// Checks if the module is a forward proxy module utilizing CONNECT method.
fn does_connect_proxy_requests(&mut self) -> bool {
// This is not a forward proxy module utilizing CONNECT method
false
}
/// Handles WebSocket requests (not used in this module).
async fn websocket_request_handler(
&mut self,
_websocket: HyperWebsocket,
_uri: &hyper::Uri,
_config: &ServerConfig,
_socket_data: &SocketData,
_error_logger: &ErrorLogger,
) -> Result<(), Box<dyn Error + Send + Sync>> {
// No proxy request handling needed.
Ok(())
}
/// Checks if the module supports WebSocket connections.
fn does_websocket_requests(&mut self, _config: &ServerConfig, _socket_data: &SocketData) -> bool {
// This module doesn't support WebSocket connections.
false
}
}
|