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
// Copyright 2018 The Grin Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Integrated static file server to serve up a pre-compiled web-wallet
//! application locally

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;

/// Future returned from `MainService`.
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(),
		}
	}
}

/// Hyper `Service` implementation that serves all requests.
struct MainService {
	static_: Static,
}

impl MainService {
	fn new() -> MainService {
		// Set up directory relative to executable for the time being
		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))
		}
	}
}

/// Start the webwallet server to serve up static files from the given
/// directory
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);
		});
}