@@ -3,14 +3,25 @@ extern crate protobuf;
33extern crate protos;
44
55use std:: time:: { SystemTime , UNIX_EPOCH } ;
6+ use std:: path:: PathBuf ;
67
7- use actix_web:: middleware :: cors :: Cors ;
8- use actix_web:: { server , App , HttpRequest , HttpResponse } ;
8+ use actix_web:: { server , App , HttpRequest , HttpResponse , fs , http :: Method } ;
9+ use actix_web:: Result as ActixResult ;
910
1011use protobuf:: Message ;
1112
1213use protos:: timestamp:: TimestampResponse ;
1314
15+ fn index ( _req : HttpRequest ) -> ActixResult < fs:: NamedFile > {
16+ let path: PathBuf = "./index.html" . into ( ) ;
17+ Ok ( fs:: NamedFile :: open ( path) ?)
18+ }
19+
20+ fn wasm ( _req : HttpRequest ) -> ActixResult < fs:: NamedFile > {
21+ let path: PathBuf = "./static/client.wasm" . into ( ) ;
22+ Ok ( fs:: NamedFile :: open ( path) ?)
23+ }
24+
1425fn fetch_timestamp ( _req : HttpRequest ) -> HttpResponse {
1526 let mut response = TimestampResponse :: new ( ) ;
1627
@@ -26,13 +37,15 @@ fn fetch_timestamp(_req: HttpRequest) -> HttpResponse {
2637
2738fn main ( ) {
2839 server:: new ( || {
29- App :: new ( ) . configure ( |app| {
30- Cors :: for_app ( app)
31- . allowed_origin ( "http://127.0.0.1:8000" )
32- . resource ( "/api/timestamp" , |r| r. f ( fetch_timestamp) )
33- . register ( )
34- } )
35- } ) . bind ( "127.0.0.1:8001" )
36- . expect ( "Can not bind to port 8001" )
37- . run ( ) ;
40+ App :: new ( )
41+ . handler (
42+ "/static" ,
43+ fs:: StaticFiles :: new ( "./static" ) )
44+ . resource ( "/api/timestamp" , |r| r. f ( fetch_timestamp) )
45+ . resource ( "/client.wasm" , |r| r. f ( wasm) )
46+ . default_resource ( |r| r. method ( Method :: GET ) . f ( index) )
47+ } )
48+ . bind ( "127.0.0.1:8001" )
49+ . expect ( "Can not bind to port 8001" )
50+ . run ( ) ;
3851}
0 commit comments