Changed web-server implementation from actix-web to tiny-http to save resources.

feature/web_server
Revertron 3 years ago
parent 162962c039
commit 57e202653d

@ -37,9 +37,8 @@ uuid = { version = "0.8.2", features = ["serde", "v4"] }
mio = { version = "0.7", features = ["os-poll", "net"] }
derive_more = "0.99" # for DNS from hermes
lazy_static = "1.4.0"
#tiny_http = "0.8"
tiny_http = "0.8"
tungstenite = "0.13"
actix-web = "3"
# Optional dependencies regulated by features
web-view = { version = "0.7", features = [], optional = true }

@ -45,3 +45,8 @@ pub const MAX_READ_BLOCK_TIME: u128 = 500;
pub const MAX_RECONNECTS: u32 = 5;
pub const MAX_IDLE_SECONDS: u64 = 180;
pub const MAX_NODES: usize = 15;
// Web-server is providing HTML/CSS/JS files to web-ui on this address
pub const WEB_SERVER_ADDR: &str = "[::1]:4280";
// Web-UI is loading its resources from this address
pub const WEB_URL: &str = "http://localhost:4280/";

@ -1,53 +1,54 @@
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};
extern crate tiny_http;
pub const CONTENT_TYPE_HTML: &str = "text/html; charset=utf-8";
pub const CONTENT_TYPE_JS: &str = "application/javascript";
pub const CONTENT_TYPE_CSS: &str = "text/css";
use std::io::Cursor;
#[get("/")]
async fn html() -> impl Responder {
let body = include_str!("webview/index.html");
HttpResponse::Ok().body(body)
}
#[allow(unused_imports)]
use log::{debug, error, info, trace, warn};
use tiny_http::{Header, Response, Server, StatusCode};
#[get("/bulma.css")]
async fn bulma() -> impl Responder {
let body = include_str!("webview/bulma.css");
HttpResponse::Ok().content_type(CONTENT_TYPE_CSS).body(body)
}
use crate::commons::WEB_SERVER_ADDR;
#[get("/styles.css")]
async fn styles() -> impl Responder {
let body = include_str!("webview/styles.css");
HttpResponse::Ok().content_type(CONTENT_TYPE_CSS).body(body)
}
pub const INDEX: &str = include_str!("webview/index.html");
pub const BULMA: &str = include_str!("webview/bulma.css");
pub const STYLES: &str = include_str!("webview/styles.css");
pub const BUSY: &str = include_str!("webview/busy.css");
pub const SCRIPTS: &str = include_str!("webview/scripts.js");
pub fn start_server() {
let server = Server::http(WEB_SERVER_ADDR).unwrap();
for request in server.incoming_requests() {
debug!("Got '{:?}' request for url: {}", request.method(), request.url());
#[get("/busy.css")]
async fn busy() -> impl Responder {
let body = include_str!("webview/busy.css");
HttpResponse::Ok().content_type(CONTENT_TYPE_CSS).body(body)
let response = process_request(&request.url());
if request.respond(response).is_err() {
warn!("Unable to send response!");
}
}
}
#[get("/scripts.js")]
async fn scripts() -> impl Responder {
let body = include_str!("webview/scripts.js");
HttpResponse::Ok().content_type(CONTENT_TYPE_JS).body(body)
fn process_request(url: &str) -> Response<Cursor<Vec<u8>>> {
let headers = vec![get_type(url)];
match url {
"/" => get_response(200, headers, INDEX),
"/bulma.css" => get_response(200, headers, BULMA),
"/styles.css" => get_response(200, headers, STYLES),
"/busy.css" => get_response(200, headers, BUSY),
"/scripts.js" => get_response(200, headers, SCRIPTS),
&_ => get_response(404, vec!(), ""),
}
}
#[post("/api")]
async fn api(req_body: String) -> impl Responder {
HttpResponse::Ok().body("Not implemented")
fn get_response(code: u16, headers: Vec<Header>, body: &str) -> Response<Cursor<Vec<u8>>> {
Response::new(StatusCode(code), headers, Cursor::new(body.as_bytes().to_vec()), Some(body.len()), None)
}
#[actix_web::main]
pub async fn start_server() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(html)
.service(bulma)
.service(styles)
.service(busy)
.service(scripts)
.service(api)
}).bind("127.0.0.1:4280")?.run().await
fn get_type(str: &str) -> Header {
let parts: Vec<&str> = str.rsplitn(2, ".").collect();
match parts[0] {
"/" | "html" | "htm" => Header::from_bytes(&b"Content-Type"[..], &b"text/html; charset=utf-8"[..]).unwrap(),
"css" => Header::from_bytes(&b"Content-Type"[..], &b"text/css"[..]).unwrap(),
"js" => Header::from_bytes(&b"Content-Type"[..], &b"application/javascript"[..]).unwrap(),
&_ => Header::from_bytes(&b"Content-Type"[..], &b"text/plain"[..]).unwrap()
}
}

@ -14,25 +14,25 @@ use log::{debug, error, info, LevelFilter, trace, warn};
use serde::Deserialize;
use alfis::{Block, Bytes, Context, Keystore, Transaction};
use alfis::keystore;
use alfis::blockchain::transaction::DomainData;
use alfis::blockchain::types::MineResult;
use alfis::commons::*;
use alfis::crypto::CryptoBox;
use alfis::dns::protocol::DnsRecord;
use alfis::event::Event;
use alfis::eventbus::{post, register};
use alfis::keystore;
use alfis::miner::Miner;
use Cmd::*;
use self::web_view::{Handle, WebView};
use alfis::crypto::CryptoBox;
use alfis::eventbus::{register, post};
use self::web_view::Content::Url;
pub fn run_interface(context: Arc<Mutex<Context>>, miner: Arc<Mutex<Miner>>) {
let title = format!("ALFIS {}", env!("CARGO_PKG_VERSION"));
let mut interface = web_view::builder()
.title(&title)
.content(Url("http://localhost:4280/"))
.content(Url(WEB_URL))
.size(1023, 720)
.min_size(773, 350)
.resizable(true)

Loading…
Cancel
Save