mirror of https://github.com/Revertron/Alfis
Changed web-server implementation from actix-web to tiny-http to save resources.
parent
162962c039
commit
57e202653d
@ -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()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue