pub struct ProductOSServer<S, E, X>{ /* private fields */ }Expand description
The main server struct for Product OS Server
ProductOSServer provides a complete web server implementation with support for:
- HTTP and HTTPS protocols
- Routing and middleware
- TLS/SSL via rustls
- WebSockets and Server-Sent Events
- Command-and-control distributed networking (with
controllerfeature) - Compression, CORS, CSRF, and security headers
§Type Parameters
S: The shared state type (must beClone + Send + Sync + 'static)E: The executor type implementingExecutortraitsX: The underlying executor context type
§Examples
§Basic HTTP Server
use product_os_server::{ProductOSServer, StatusCode, Response, Body};
use product_os_server::ServerConfig;
use product_os_async_executor::TokioExecutor;
let config = ServerConfig::new();
let mut server: ProductOSServer<(), TokioExecutor, _> =
ProductOSServer::new_with_config(config);
async fn handler() -> Result<Response<Body>, StatusCode> {
Ok(Response::new(Body::empty()))
}
server.add_get("/", handler);§Server with Shared State
use product_os_server::{ProductOSServer, StatusCode, Response};
use product_os_server::ServerConfig;
use product_os_async_executor::TokioExecutor;
#[derive(Clone)]
struct AppState {
counter: std::sync::Arc<parking_lot::Mutex<i32>>,
}
let state = AppState {
counter: std::sync::Arc::new(parking_lot::Mutex::new(0)),
};
let config = ServerConfig::new();
let mut server: ProductOSServer<AppState, TokioExecutor, _> =
ProductOSServer::new_with_state_with_config(config, state);Implementations§
Source§impl<E, X> ProductOSServer<(), E, X>
impl<E, X> ProductOSServer<(), E, X>
Sourcepub fn new_with_executor_with_config(
executor: Option<Arc<E>>,
config: ServerConfig,
) -> Self
pub fn new_with_executor_with_config( executor: Option<Arc<E>>, config: ServerConfig, ) -> Self
Creates a new server with a custom executor and configuration.
This constructor allows specifying a custom executor for handling async operations.
§Arguments
executor- Optional executor Arc. If None, will panic.config- Server configuration
Sourcepub fn new_with_executor(executor: Option<Arc<E>>) -> Self
pub fn new_with_executor(executor: Option<Arc<E>>) -> Self
Creates a new server with a custom executor and default configuration.
§Arguments
executor- Optional executor Arc. If None, will panic.
Source§impl<S, E, X> ProductOSServer<S, E, X>
impl<S, E, X> ProductOSServer<S, E, X>
Sourcepub fn new_with_executor_with_state_with_config(
executor: Option<Arc<E>>,
config: ServerConfig,
state: S,
) -> Self
pub fn new_with_executor_with_state_with_config( executor: Option<Arc<E>>, config: ServerConfig, state: S, ) -> Self
Creates a new server with custom executor, state, and configuration.
This is the most flexible constructor, allowing full customization of executor, state, and configuration.
§Arguments
executor- Optional executor Arc. If None, will panic.config- Server configuration including network, security, compression settingsstate- Shared state that will be available to all handlers
Sourcepub fn new_with_executor_with_state(executor: Option<Arc<E>>, state: S) -> Self
pub fn new_with_executor_with_state(executor: Option<Arc<E>>, state: S) -> Self
Creates a new server with custom executor and state.
Uses default configuration.
§Arguments
executor- Optional executor Arc. If None, will panic.state- Shared state available to all handlers
Sourcepub fn set_certificate(&mut self, certificate_config: &Option<Certificate>)
pub fn set_certificate(&mut self, certificate_config: &Option<Certificate>)
Sets or updates the TLS certificate configuration.
§Arguments
certificate_config- Optional certificate configuration. If None, will generate self-signed.
Sourcepub fn set_security(&mut self, security: Option<Security>)
pub fn set_security(&mut self, security: Option<Security>)
Sets or updates the security configuration.
This includes settings for security headers, CSRF protection, etc.
§Arguments
security- Optional security configuration. If None, security features are disabled.
Sourcepub fn get_router(&mut self) -> &mut ProductOSRouter<S>
pub fn get_router(&mut self) -> &mut ProductOSRouter<S>
Sourcepub fn add_route(&mut self, path: &str, service_handler: MethodRouter<S>)
pub fn add_route(&mut self, path: &str, service_handler: MethodRouter<S>)
Adds a route with a method router.
§Arguments
path- The URL path for the route (e.g., “/api/users”)service_handler- The method router handling the route
§Examples
use product_os_router::MethodRouter;
async fn handler() -> Result<Response<Body>, StatusCode> {
Ok(Response::new(Body::empty()))
}
let method_router = MethodRouter::new().get(handler);
server.add_route("/test", method_router);Sourcepub fn set_fallback(&mut self, service_handler: MethodRouter<S>)
pub fn set_fallback(&mut self, service_handler: MethodRouter<S>)
Sets a fallback handler for unmatched routes.
The fallback handler is called when no route matches the incoming request. This is typically used to return a 404 Not Found response.
§Arguments
service_handler- The method router for handling unmatched routes
§Examples
use product_os_router::MethodRouter;
async fn not_found() -> Result<Response<Body>, StatusCode> {
Err(StatusCode::NOT_FOUND)
}
let fallback = MethodRouter::new().fallback(not_found);
server.set_fallback(fallback);Sourcepub fn add_get<H, T>(&mut self, path: &str, handler: H)where
H: Handler<T, S>,
T: 'static,
pub fn add_get<H, T>(&mut self, path: &str, handler: H)where
H: Handler<T, S>,
T: 'static,
Adds a GET request handler.
Convenience method for adding a handler that responds to GET requests.
§Arguments
path- The URL path (e.g., “/api/users”)handler- The async function handling the request
§Examples
async fn get_users() -> Result<Response<Body>, StatusCode> {
Ok(Response::new(Body::empty()))
}
server.add_get("/api/users", get_users);Sourcepub fn add_post<H, T>(&mut self, path: &str, handler: H)where
H: Handler<T, S>,
T: 'static,
pub fn add_post<H, T>(&mut self, path: &str, handler: H)where
H: Handler<T, S>,
T: 'static,
Adds a POST request handler.
Convenience method for adding a handler that responds to POST requests.
§Arguments
path- The URL path (e.g., “/api/users”)handler- The async function handling the request
§Examples
async fn create_user(Json(user): Json<User>) -> Result<Response<Body>, StatusCode> {
Ok(Response::new(Body::empty()))
}
server.add_post("/api/users", create_user);Sourcepub fn add_handler<H, T>(&mut self, path: &str, method: Method, handler: H)where
H: Handler<T, S>,
T: 'static,
pub fn add_handler<H, T>(&mut self, path: &str, method: Method, handler: H)where
H: Handler<T, S>,
T: 'static,
Adds a handler for a specific HTTP method and path.
§Arguments
path- The URL pathmethod- The HTTP method (GET, POST, PUT, DELETE, etc.)handler- The async function to handle the request
§Examples
async fn patch_handler() -> Result<Response<product_os_server::Body>, StatusCode> {
Ok(Response::new(product_os_server::Body::empty()))
}
server.add_handler("/resource", Method::PATCH, patch_handler);Sourcepub fn set_fallback_handler<H, T>(&mut self, handler: H)where
H: Handler<T, S>,
T: 'static,
pub fn set_fallback_handler<H, T>(&mut self, handler: H)where
H: Handler<T, S>,
T: 'static,
Sourcepub fn add_handlers<H, T>(&mut self, path: &str, handlers: BTreeMap<Method, H>)where
H: Handler<T, S>,
T: 'static,
pub fn add_handlers<H, T>(&mut self, path: &str, handlers: BTreeMap<Method, H>)where
H: Handler<T, S>,
T: 'static,
Adds multiple handlers for different HTTP methods on the same path.
§Arguments
path- The URL pathhandlers- A map of HTTP methods to their handlers
§Examples
// Note: This API requires handlers of the same type. In practice,
// use add_get, add_post, etc. for different handler implementations.
async fn handler() -> Result<Response<Body>, StatusCode> {
Ok(Response::new(Body::empty()))
}
// Use add_handler for each method instead:
server.add_handler("/resource", Method::GET, handler);
server.add_handler("/resource", Method::POST, handler);Sourcepub fn set_router(&mut self, router: ProductOSRouter<S>)
pub fn set_router(&mut self, router: ProductOSRouter<S>)
Sourcepub async fn create_https_server(
&mut self,
serve_on_main_thread: bool,
listen_all_interfaces: bool,
custom_port: Option<u16>,
custom_router: Option<Router>,
with_connect_info: bool,
) -> Result<(), Box<dyn Error + Send + Sync>>
pub async fn create_https_server( &mut self, serve_on_main_thread: bool, listen_all_interfaces: bool, custom_port: Option<u16>, custom_router: Option<Router>, with_connect_info: bool, ) -> Result<(), Box<dyn Error + Send + Sync>>
Creates and starts an HTTPS server.
Internal method for creating HTTPS servers with TLS support.
Use start() instead for normal server initialization.
§Arguments
_serve_on_main_thread- Whether to block the main thread_listen_all_interfaces- Whether to listen on all network interfaces_custom_port- Optional custom port number_custom_router- Optional custom router to use_with_connect_info- Whether to extract connection info
Sourcepub async fn create_http_server(
&mut self,
serve_on_main_thread: bool,
listen_all_interfaces: bool,
custom_port: Option<u16>,
custom_router: Option<Router>,
with_connect_info: bool,
) -> Result<(), Box<dyn Error + Send + Sync>>
pub async fn create_http_server( &mut self, serve_on_main_thread: bool, listen_all_interfaces: bool, custom_port: Option<u16>, custom_router: Option<Router>, with_connect_info: bool, ) -> Result<(), Box<dyn Error + Send + Sync>>
Creates and starts an HTTP server.
Internal method for creating HTTP servers.
Use start() instead for normal server initialization.
§Arguments
serve_on_main_thread- Whether to block the main threadlisten_all_interfaces- Whether to listen on all network interfacescustom_port- Optional custom port numbercustom_router- Optional custom router to usewith_connect_info- Whether to extract connection info
Sourcepub async fn init_stores(&mut self)
pub async fn init_stores(&mut self)
Initializes data stores.
Internal method for store initialization. Currently a no-op but reserved for future store initialization logic.
Sourcepub fn get_config(&self) -> ServerConfig
pub fn get_config(&self) -> ServerConfig
Sourcepub fn update_config(&mut self, config: ServerConfig)
pub fn update_config(&mut self, config: ServerConfig)
Sourcepub fn get_base_url(&self) -> Uri
👎Deprecated since 0.0.53: Use try_get_base_url() which returns Result instead of panicking
pub fn get_base_url(&self) -> Uri
Sourcepub fn try_get_base_url(&self) -> Result<Uri>
pub fn try_get_base_url(&self) -> Result<Uri>
Returns the base URL as a Uri, or an error if URL parsing fails.
This is the non-panicking version of get_base_url.