@@ -106,41 +106,107 @@ impl Drop for WebViewerSink {
106
106
107
107
// ----------------------------------------------------------------------------
108
108
109
- /// Helper to spawn an instance of the [`WebViewerServer`].
110
- /// This serves the HTTP+Wasm+JS files that make up the web-viewer.
111
- ///
112
- /// Optionally opens a browser with the web-viewer and connects to the provided `target_url`.
113
- /// This url could be a hosted RRD file or a `ws://` url to a running [`re_ws_comms::RerunServer`].
114
- ///
115
- /// Note: this does not include the websocket server.
116
- ///
117
- /// - `force_wgpu_backend` is an optional string to force a specific backend, either `webgl` or `webgpu`.
109
+ /// Helper to spawn an instance of the [`WebViewerServer`] and configure a webviewer url.
118
110
#[ cfg( feature = "web_viewer" ) ]
119
- pub fn host_web_viewer (
120
- bind_ip : & str ,
121
- web_port : WebViewerServerPort ,
122
- force_wgpu_backend : Option < String > ,
123
- video_decoder : Option < String > ,
124
- open_browser : bool ,
125
- source_url : & str ,
126
- ) -> anyhow:: Result < WebViewerServer > {
127
- let web_server = WebViewerServer :: new ( bind_ip, web_port) ?;
128
- let http_web_viewer_url = web_server. server_url ( ) ;
129
-
130
- let mut viewer_url = format ! ( "{http_web_viewer_url}?url={source_url}" ) ;
131
- if let Some ( force_graphics) = force_wgpu_backend {
132
- viewer_url = format ! ( "{viewer_url}&renderer={force_graphics}" ) ;
133
- }
134
- if let Some ( video_decoder) = video_decoder {
135
- viewer_url = format ! ( "{viewer_url}&video_decoder={video_decoder}" ) ;
136
- }
111
+ pub struct WebViewerConfig {
112
+ /// Ip to which the http server is bound.
113
+ ///
114
+ /// Defaults to 0.0.0.0
115
+ pub bind_ip : String ,
116
+
117
+ /// The port to which the webviewer should bind.
118
+ ///
119
+ /// Defaults to [`WebViewerServerPort::AUTO`].
120
+ pub web_port : WebViewerServerPort ,
121
+
122
+ /// The url from which a spawned webviewer should source
123
+ ///
124
+ /// This url could be a hosted RRD file or a `ws://` url to a running [`re_ws_comms::RerunServer`].
125
+ /// Has no effect if [`Self::open_browser`] is false.
126
+ pub source_url : Option < String > ,
127
+
128
+ /// If set, adjusts the browser url to force a specific backend, either `webgl` or `webgpu`.
129
+ ///
130
+ /// Has no effect if [`Self::open_browser`] is false.
131
+ pub force_wgpu_backend : Option < String > ,
132
+
133
+ /// If set, adjusts the browser url to set the video decoder setting, either `auto`, `prefer_software` or `prefer_hardware`.
134
+ ///
135
+ /// Has no effect if [`Self::open_browser`] is false.
136
+ pub video_decoder : Option < String > ,
137
+
138
+ /// If set to `true`, opens the default browser after hosting the webviewer.
139
+ ///
140
+ /// Defaults to `true`.
141
+ pub open_browser : bool ,
142
+ }
137
143
138
- re_log:: info!( "Hosting a web-viewer at {viewer_url}" ) ;
139
- if open_browser {
140
- webbrowser:: open ( & viewer_url) . ok ( ) ;
144
+ #[ cfg( feature = "web_viewer" ) ]
145
+ impl Default for WebViewerConfig {
146
+ fn default ( ) -> Self {
147
+ Self {
148
+ bind_ip : "0.0.0.0" . to_owned ( ) ,
149
+ web_port : WebViewerServerPort :: AUTO ,
150
+ source_url : None ,
151
+ force_wgpu_backend : None ,
152
+ video_decoder : None ,
153
+ open_browser : true ,
154
+ }
141
155
}
156
+ }
142
157
143
- Ok ( web_server)
158
+ #[ cfg( feature = "web_viewer" ) ]
159
+ impl WebViewerConfig {
160
+ /// Helper to spawn an instance of the [`WebViewerServer`].
161
+ /// This serves the HTTP+Wasm+JS files that make up the web-viewer.
162
+ ///
163
+ /// The server will immediately start listening for incoming connections
164
+ /// and stop doing so when the returned [`WebViewerServer`] is dropped.
165
+ ///
166
+ /// Note: this does not include the websocket server.
167
+ pub fn host_web_viewer ( self ) -> Result < WebViewerServer , WebViewerServerError > {
168
+ let Self {
169
+ bind_ip,
170
+ source_url,
171
+ web_port,
172
+ force_wgpu_backend,
173
+ video_decoder,
174
+ open_browser,
175
+ } = self ;
176
+
177
+ let web_server = WebViewerServer :: new ( & bind_ip, web_port) ?;
178
+ let http_web_viewer_url = web_server. server_url ( ) ;
179
+
180
+ let mut viewer_url = http_web_viewer_url;
181
+
182
+ let mut first_arg = true ;
183
+ let mut append_argument = |arg| {
184
+ let arg_delimiter = if first_arg {
185
+ first_arg = false ;
186
+ "?"
187
+ } else {
188
+ "&"
189
+ } ;
190
+ viewer_url = format ! ( "{viewer_url}{arg_delimiter}{arg}" ) ;
191
+ } ;
192
+
193
+ if let Some ( source_url) = source_url {
194
+ append_argument ( format ! ( "url={source_url}" ) ) ;
195
+ }
196
+ if let Some ( force_graphics) = force_wgpu_backend {
197
+ append_argument ( format ! ( "renderer={force_graphics}" ) ) ;
198
+ }
199
+ if let Some ( video_decoder) = video_decoder {
200
+ append_argument ( format ! ( "video_decoder={video_decoder}" ) ) ;
201
+ }
202
+
203
+ re_log:: info!( "Hosting a web-viewer at {viewer_url}" ) ;
204
+ if open_browser {
205
+ webbrowser:: open ( & viewer_url) . ok ( ) ;
206
+ }
207
+
208
+ Ok ( web_server)
209
+ }
144
210
}
145
211
146
212
// ----------------------------------------------------------------------------
0 commit comments