1111use std:: task:: { Context , Poll } ;
1212
1313use futures_util:: future;
14- use hyper:: { server:: conn:: AddrIncoming , service:: Service } ;
15- use hyper:: { Body , Request , Response } ;
14+ use hyper:: { server:: conn:: AddrIncoming , service:: Service , Body , Request , Response } ;
1615
17- #[ derive( Debug ) ]
18- struct Svc ;
16+ struct Svc {
17+ // NOTE: Optional because it is possible to have the `analytics` feature flag enabled
18+ // while at the same time opting-out of analytics at run-time.
19+ #[ cfg( feature = "analytics" ) ]
20+ analytics : Option < re_analytics:: Analytics > ,
21+ }
22+
23+ impl Svc {
24+ #[ cfg( feature = "analytics" ) ]
25+ fn new ( ) -> Self {
26+ let analytics = match re_analytics:: Analytics :: new ( std:: time:: Duration :: from_secs ( 2 ) ) {
27+ Ok ( analytics) => Some ( analytics) ,
28+ Err ( err) => {
29+ re_log:: error!( %err, "failed to initialize analytics SDK" ) ;
30+ None
31+ }
32+ } ;
33+ Self { analytics }
34+ }
35+
36+ #[ cfg( not( feature = "analytics" ) ) ]
37+ fn new ( ) -> Self {
38+ Self { }
39+ }
40+
41+ #[ cfg( feature = "analytics" ) ]
42+ fn on_serve_wasm ( & self ) {
43+ if let Some ( analytics) = & self . analytics {
44+ analytics. record ( re_analytics:: Event :: append ( "serve_wasm" . into ( ) ) ) ;
45+ }
46+ }
47+ }
1948
2049impl Service < Request < Body > > for Svc {
2150 type Response = Response < Body > ;
@@ -28,6 +57,10 @@ impl Service<Request<Body>> for Svc {
2857
2958 #[ cfg( feature = "__ci" ) ]
3059 fn call ( & mut self , _req : Request < Body > ) -> Self :: Future {
60+ if false {
61+ self . on_serve_wasm ( ) ; // to silence warning about the function being unused
62+ }
63+
3164 panic ! ( "web_server compiled with '__ci' feature (or `--all-features`). DON'T DO THAT! It's only for the CI!" ) ;
3265 }
3366
@@ -39,8 +72,12 @@ impl Service<Request<Body>> for Svc {
3972 "/" | "/index.html" => & include_bytes ! ( "../web_viewer/index.html" ) [ ..] ,
4073 "/favicon.ico" => & include_bytes ! ( "../web_viewer/favicon.ico" ) [ ..] ,
4174 "/sw.js" => & include_bytes ! ( "../web_viewer/sw.js" ) [ ..] ,
42- "/re_viewer_bg.wasm" => & include_bytes ! ( "../web_viewer/re_viewer_bg.wasm" ) [ ..] ,
4375 "/re_viewer.js" => & include_bytes ! ( "../web_viewer/re_viewer.js" ) [ ..] ,
76+ "/re_viewer_bg.wasm" => {
77+ #[ cfg( feature = "analytics" ) ]
78+ self . on_serve_wasm ( ) ;
79+ & include_bytes ! ( "../web_viewer/re_viewer_bg.wasm" ) [ ..]
80+ }
4481 _ => {
4582 re_log:: warn!( "404 path: {}" , req. uri( ) . path( ) ) ;
4683 let body = Body :: from ( Vec :: new ( ) ) ;
@@ -67,7 +104,7 @@ impl<T> Service<T> for MakeSvc {
67104 }
68105
69106 fn call ( & mut self , _: T ) -> Self :: Future {
70- future:: ok ( Svc )
107+ future:: ok ( Svc :: new ( ) )
71108 }
72109}
73110
0 commit comments