1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use futures::{future, Async::*, Future, Poll};
use http::response::Builder as ResponseBuilder;
use http::{header, Request, Response, StatusCode};
use hyper::service::Service;
use hyper::{rt, Body, Server};
use hyper_staticfile::{Static, StaticFuture};
use std::env;
use std::io::Error;
use std::thread;
enum MainFuture {
Root,
Static(StaticFuture<Body>),
}
impl Future for MainFuture {
type Item = Response<Body>;
type Error = Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
match *self {
MainFuture::Root => {
let res = ResponseBuilder::new()
.status(StatusCode::MOVED_PERMANENTLY)
.header(header::LOCATION, "/index.html")
.body(Body::empty())
.expect("unable to build response");
Ok(Ready(res))
}
MainFuture::Static(ref mut future) => future.poll(),
}
}
}
struct MainService {
static_: Static,
}
impl MainService {
fn new() -> MainService {
let mut exe_path = env::current_exe().unwrap();
exe_path.pop();
exe_path.push("grin-wallet");
MainService {
static_: Static::new(exe_path),
}
}
}
impl Service for MainService {
type ReqBody = Body;
type ResBody = Body;
type Error = Error;
type Future = MainFuture;
fn call(&mut self, req: Request<Body>) -> MainFuture {
if req.uri().path() == "/" {
MainFuture::Root
} else {
MainFuture::Static(self.static_.serve(req))
}
}
}
pub fn start_webwallet_server() {
let _ = thread::Builder::new()
.name("webwallet_server".to_string())
.spawn(move || {
let addr = ([127, 0, 0, 1], 13421).into();
let server = Server::bind(&addr)
.serve(|| future::ok::<_, Error>(MainService::new()))
.map_err(|e| eprintln!("server error: {}", e));
warn!("Grin Web-Wallet Application is running at http://{}/", addr);
rt::run(server);
});
}