Skip to main content

pharos_native/binaries/
artifact_server.rs

1#![allow(clippy::result_unit_err)]
2
3use std::path::Path;
4
5use crate::config::AppRuntime;
6
7pub use pharos_runtime_static::{
8    StaticResponse, StreamError, StreamResponse, path_has_parent_reference,
9};
10
11/// Serves one request using the extracted static-runtime crate.
12///
13/// # Parameters
14///
15/// - `app`: Application runtime whose packaged artifact tree is being served.
16/// - `method`: HTTP method for the static asset request.
17/// - `target`: Request path within the packaged artifact tree.
18///
19/// # Returns
20///
21/// Returns the static response produced for the requested artifact path.
22///
23/// # Examples
24///
25/// ```rust,ignore
26/// let _result = pharos_native::binaries::artifact_server::serve_app_request(todo!(), todo!(), "/");
27/// ```
28///
29/// # Related
30///
31/// - [`crate::artifact_server`] for adjacent helpers that share the same runtime surface.
32pub fn serve_app_request(app: &AppRuntime, method: &str, target: &str) -> StaticResponse {
33    let root = app
34        .artifact_dir
35        .as_deref()
36        .filter(|value| !value.is_empty())
37        .map(Path::new)
38        .unwrap_or_else(|| Path::new(""));
39    pharos_runtime_static::serve_artifact_request(root, method, target)
40}
41
42/// Resolves one direct app asset into a bounded streaming response.
43///
44/// # Parameters
45///
46/// - `app`: Application runtime whose packaged artifact tree is being served.
47/// - `method`: HTTP method for the asset request.
48/// - `target`: Request path for the requested asset.
49/// - `range`: Optional byte-range request header.
50/// - `if_range`: Optional conditional range header.
51///
52/// # Returns
53///
54/// Returns a streaming response when the asset exists and is streamable.
55///
56/// # Errors
57///
58/// Returns an error when range parsing or stream preparation fails.
59///
60/// # Examples
61///
62/// ```rust,ignore
63/// let _result = pharos_native::binaries::artifact_server::serve_app_streamable_asset(todo!(), todo!(), "/", todo!(), todo!());
64/// ```
65///
66/// # Related
67///
68/// - [`crate::artifact_server`] for adjacent helpers that share the same runtime surface.
69pub fn serve_app_streamable_asset(
70    app: &AppRuntime,
71    method: &str,
72    target: &str,
73    range: Option<&str>,
74    if_range: Option<&str>,
75) -> Result<Option<StreamResponse>, StreamError> {
76    let root = app
77        .artifact_dir
78        .as_deref()
79        .filter(|value| !value.is_empty())
80        .map(Path::new)
81        .unwrap_or_else(|| Path::new(""));
82    pharos_runtime_static::serve_streamable_asset(root, method, target, range, if_range)
83}
84
85/// Streams one already-authorized app-private asset.
86///
87/// # Parameters
88///
89/// - `app`: Application runtime whose packaged private-asset tree is being served.
90/// - `private_root`: Declared private asset root inside the artifact tree.
91/// - `asset_key`: Authorized asset key to resolve within the private root.
92/// - `method`: HTTP method for the asset request.
93/// - `range`: Optional byte-range request header.
94/// - `if_range`: Optional conditional range header.
95///
96/// # Returns
97///
98/// Returns the prepared private-asset stream response when authorization has already succeeded.
99///
100/// # Errors
101///
102/// Returns an error when the private asset cannot be resolved or streamed.
103///
104/// # Examples
105///
106/// ```rust,ignore
107/// let _result = pharos_native::binaries::artifact_server::serve_app_private_asset(todo!(), todo!(), todo!(), todo!(), todo!(), todo!());
108/// ```
109///
110/// # Related
111///
112/// - [`crate::artifact_server`] for adjacent helpers that share the same runtime surface.
113pub fn serve_app_private_asset(
114    app: &AppRuntime,
115    private_root: &str,
116    asset_key: &str,
117    method: &str,
118    range: Option<&str>,
119    if_range: Option<&str>,
120) -> Result<StreamResponse, StreamError> {
121    let root = app
122        .artifact_dir
123        .as_deref()
124        .filter(|value| !value.is_empty())
125        .map(Path::new)
126        .unwrap_or_else(|| Path::new(""));
127    pharos_runtime_static::serve_private_asset(
128        root,
129        private_root,
130        asset_key,
131        method,
132        range,
133        if_range,
134    )
135}
136
137/// Runs the extracted bounded loopback static server.
138///
139/// # Parameters
140///
141/// - `root`: Artifact root to serve.
142/// - `port`: TCP port to bind for the loopback server.
143/// - `max_requests`: Maximum number of requests to serve before exiting.
144///
145/// # Returns
146///
147/// Returns the process exit code from the bounded static server loop.
148///
149/// # Examples
150///
151/// ```rust,ignore
152/// let _result = pharos_native::binaries::artifact_server::run(std::path::Path::new("."), "8080", todo!());
153/// ```
154///
155/// # Related
156///
157/// - [`crate::artifact_server`] for adjacent helpers that share the same runtime surface.
158pub fn run(root: &Path, port: u16, max_requests: usize) -> i32 {
159    pharos_runtime_static::run(root, port, max_requests)
160}