Skip to content

Commit be7cf09

Browse files
committed
Change web server to serve the web client.
1 parent 3cc7f03 commit be7cf09

File tree

4 files changed

+71
-11
lines changed

4 files changed

+71
-11
lines changed

server/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11

22
/target
33
**/*.rs.bk
4+
/static/client.*

server/build.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
current=$(pwd)
2+
3+
# client build
4+
cd ../client
5+
6+
cargo +nightly web deploy --target=wasm32-unknown-unknown --release
7+
8+
# server build
9+
cd $current
10+
11+
mkdir -p ./static
12+
cp ../client/target/deploy/client.* ./static/
13+
14+
cargo build

server/index.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
<!DOCTYPE html>
3+
<html>
4+
<head>
5+
<meta charset="utf-8" />
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
7+
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=1" name="viewport" />
8+
<script>
9+
var Rust = {
10+
__dirname: 'static',
11+
};
12+
var Module = {};
13+
var __cargo_web = {};
14+
Object.defineProperty( Module, 'canvas', {
15+
get: function() {
16+
if( __cargo_web.canvas ) {
17+
return __cargo_web.canvas;
18+
}
19+
20+
var canvas = document.createElement( 'canvas' );
21+
document.querySelector( 'body' ).appendChild( canvas );
22+
__cargo_web.canvas = canvas;
23+
24+
return canvas;
25+
}
26+
});
27+
</script>
28+
</head>
29+
<body>
30+
<script src="static/client.js"></script>
31+
</body>
32+
</html>

server/src/main.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,25 @@ extern crate protobuf;
33
extern crate protos;
44

55
use 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

1011
use protobuf::Message;
1112

1213
use 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+
1425
fn fetch_timestamp(_req: HttpRequest) -> HttpResponse {
1526
let mut response = TimestampResponse::new();
1627

@@ -26,13 +37,15 @@ fn fetch_timestamp(_req: HttpRequest) -> HttpResponse {
2637

2738
fn 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

Comments
 (0)