Serve web and swagger static files

This commit is contained in:
Antoine Gersant 2020-01-14 01:33:14 -08:00
parent 0a4d05cdc8
commit e248f3b983
9 changed files with 169 additions and 31 deletions

63
Cargo.lock generated
View file

@ -32,6 +32,25 @@ dependencies = [
"trust-dns-resolver 0.18.0-alpha.2 (registry+https://github.com/rust-lang/crates.io-index)", "trust-dns-resolver 0.18.0-alpha.2 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "actix-files"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"actix-http 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"actix-service 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"actix-web 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"bytes 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"derive_more 0.99.2 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"mime 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
"mime_guess 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"v_htmlescape 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "actix-http" name = "actix-http"
version = "1.0.1" version = "1.0.1"
@ -1720,6 +1739,15 @@ dependencies = [
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "nom"
version = "4.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "notify" name = "notify"
version = "4.0.14" version = "4.0.14"
@ -2017,6 +2045,8 @@ dependencies = [
name = "polaris" name = "polaris"
version = "0.11.0" version = "0.11.0"
dependencies = [ dependencies = [
"actix-files 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"actix-http 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"actix-rt 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "actix-rt 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"actix-web 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "actix-web 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"anyhow 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)", "anyhow 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)",
@ -3229,6 +3259,34 @@ name = "uuid"
version = "0.8.1" version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "v_escape"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"v_escape_derive 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "v_escape_derive"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "v_htmlescape"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"v_escape 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "vcpkg" name = "vcpkg"
version = "0.2.8" version = "0.2.8"
@ -3363,6 +3421,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata] [metadata]
"checksum actix-codec 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "09e55f0a5c2ca15795035d90c46bd0e73a5123b72f68f12596d6ba5282051380" "checksum actix-codec 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "09e55f0a5c2ca15795035d90c46bd0e73a5123b72f68f12596d6ba5282051380"
"checksum actix-connect 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1f2b61480a8d30c94d5c883d79ef026b02ad6809931b0a4bb703f9545cd8c986" "checksum actix-connect 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1f2b61480a8d30c94d5c883d79ef026b02ad6809931b0a4bb703f9545cd8c986"
"checksum actix-files 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "301482841d3d74483a446ead63cb7d362e187d2c8b603f13d91995621ea53c46"
"checksum actix-http 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c16664cc4fdea8030837ad5a845eb231fb93fc3c5c171edfefb52fad92ce9019" "checksum actix-http 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c16664cc4fdea8030837ad5a845eb231fb93fc3c5c171edfefb52fad92ce9019"
"checksum actix-macros 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "21705adc76bbe4bc98434890e73a89cd00c6015e5704a60bb6eea6c3b72316b6" "checksum actix-macros 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "21705adc76bbe4bc98434890e73a89cd00c6015e5704a60bb6eea6c3b72316b6"
"checksum actix-router 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "9d7a10ca4d94e8c8e7a87c5173aba1b97ba9a6563ca02b0e1cd23531093d3ec8" "checksum actix-router 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "9d7a10ca4d94e8c8e7a87c5173aba1b97ba9a6563ca02b0e1cd23531093d3ec8"
@ -3543,6 +3602,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum mp3-duration 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "9b7cc3eebd1d49a86505733254d1dfd53fbefe4ab65d86afd6f84d3245cd604f" "checksum mp3-duration 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "9b7cc3eebd1d49a86505733254d1dfd53fbefe4ab65d86afd6f84d3245cd604f"
"checksum native-tls 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4b2df1a4c22fd44a62147fd8f13dd0f95c9d8ca7b2610299b2a2f9cf8964274e" "checksum native-tls 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4b2df1a4c22fd44a62147fd8f13dd0f95c9d8ca7b2610299b2a2f9cf8964274e"
"checksum net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "42550d9fb7b6684a6d404d9fa7250c2eb2646df731d1c06afc06dcee9e1bcf88" "checksum net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "42550d9fb7b6684a6d404d9fa7250c2eb2646df731d1c06afc06dcee9e1bcf88"
"checksum nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2ad2a91a8e869eeb30b9cb3119ae87773a8f4ae617f41b1eb9c154b2905f7bd6"
"checksum notify 4.0.14 (registry+https://github.com/rust-lang/crates.io-index)" = "199628fc33b21bc767baa057490b00b382ecbae030803a7b36292422d15b778b" "checksum notify 4.0.14 (registry+https://github.com/rust-lang/crates.io-index)" = "199628fc33b21bc767baa057490b00b382ecbae030803a7b36292422d15b778b"
"checksum num 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cf4825417e1e1406b3782a8ce92f4d53f26ec055e3622e1881ca8e9f5f9e08db" "checksum num 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cf4825417e1e1406b3782a8ce92f4d53f26ec055e3622e1881ca8e9f5f9e08db"
"checksum num-bigint 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "f9c3f34cdd24f334cb265d9bf8bfa8a241920d026916785747a92f0e55541a1a" "checksum num-bigint 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "f9c3f34cdd24f334cb265d9bf8bfa8a241920d026916785747a92f0e55541a1a"
@ -3701,6 +3761,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum url 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "75b414f6c464c879d7f9babf951f23bc3743fb7313c081b2e6ca719067ea9d61" "checksum url 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "75b414f6c464c879d7f9babf951f23bc3743fb7313c081b2e6ca719067ea9d61"
"checksum uuid 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)" = "90dbc611eb48397705a6b0f6e917da23ae517e4d127123d2cf7674206627d32a" "checksum uuid 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)" = "90dbc611eb48397705a6b0f6e917da23ae517e4d127123d2cf7674206627d32a"
"checksum uuid 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9fde2f6a4bea1d6e007c4ad38c6839fa71cbb63b6dbf5b595aa38dc9b1093c11" "checksum uuid 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9fde2f6a4bea1d6e007c4ad38c6839fa71cbb63b6dbf5b595aa38dc9b1093c11"
"checksum v_escape 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)" = "660b101c07b5d0863deb9e7fb3138777e858d6d2a79f9e6049a27d1cc77c6da6"
"checksum v_escape_derive 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c2ca2a14bc3fc5b64d188b087a7d3a927df87b152e941ccfbc66672e20c467ae"
"checksum v_htmlescape 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e33e939c0d8cf047514fb6ba7d5aac78bc56677a6938b2ee67000b91f2e97e41"
"checksum vcpkg 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3fc439f2794e98976c88a2a2dafce96b930fe8010b0a256b3c2199a773933168" "checksum vcpkg 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3fc439f2794e98976c88a2a2dafce96b930fe8010b0a256b3c2199a773933168"
"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" "checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"
"checksum version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "078775d0255232fb988e6fccf26ddc9d1ac274299aaedcedce21c6f72cc533ce" "checksum version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "078775d0255232fb988e6fccf26ddc9d1ac274299aaedcedce21c6f72cc533ce"

View file

@ -8,10 +8,12 @@ edition = "2018"
default = ["service-actix"] default = ["service-actix"]
ui = [] ui = []
profile-index = ["flame", "flamer"] profile-index = ["flame", "flamer"]
service-actix = ["actix-rt", "actix-web"] service-actix = ["actix-files", "actix-http", "actix-rt", "actix-web"]
service-rocket = ["rocket", "rocket_contrib"] service-rocket = ["rocket", "rocket_contrib"]
[dependencies] [dependencies]
actix-files = { version = "0.2", optional = true }
actix-http = { version = "1.0", optional = true }
actix-web = { version = "2.0", optional = true } actix-web = { version = "2.0", optional = true }
actix-rt = { version = "1.0", optional = true } actix-rt = { version = "1.0", optional = true }
anyhow = "1.0" anyhow = "1.0"

View file

@ -223,11 +223,11 @@ fn main() -> Result<()> {
service::server::run( service::server::run(
port, port,
Some(auth_secret.as_slice()), Some(auth_secret.as_slice()),
&api_url, api_url,
&web_url, web_url,
&web_dir_path, web_dir_path,
&swagger_url, swagger_url,
&swagger_dir_path, swagger_dir_path,
db.clone(), db.clone(),
command_sender, command_sender,
)?; )?;

View file

@ -1,4 +1,20 @@
use actix_files as fs;
use actix_web::web;
use std::path::Path;
pub mod server; pub mod server;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
fn configure_app(
cfg: &mut web::ServiceConfig,
web_url: &str,
web_dir_path: &Path,
swagger_url: &str,
swagger_dir_path: &Path,
) {
// TODO logging
cfg.service(fs::Files::new(swagger_url, swagger_dir_path).index_file("index.html"))
.service(fs::Files::new(web_url, web_dir_path).index_file("index.html"));
}

View file

@ -1,4 +1,4 @@
use actix_web::{web, App, HttpResponse, HttpServer, Responder}; use actix_web::{App, HttpServer};
use anyhow::*; use anyhow::*;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Arc; use std::sync::Arc;
@ -6,25 +6,29 @@ use std::sync::Arc;
use crate::db::DB; use crate::db::DB;
use crate::index::CommandSender; use crate::index::CommandSender;
async fn index() -> impl Responder {
HttpResponse::Ok().body("hello world!")
}
#[actix_rt::main] #[actix_rt::main]
pub async fn run( pub async fn run(
port: u16, port: u16,
auth_secret: Option<&[u8]>, auth_secret: Option<&[u8]>,
api_url: &str, api_url: String,
web_url: &str, web_url: String,
web_dir_path: &PathBuf, web_dir_path: PathBuf,
swagger_url: &str, swagger_url: String,
swagger_dir_path: &PathBuf, swagger_dir_path: PathBuf,
db: Arc<DB>, db: Arc<DB>,
command_sender: Arc<CommandSender>, command_sender: Arc<CommandSender>,
) -> Result<()> { ) -> Result<()> {
let app = App::new(); HttpServer::new(move || {
App::new().configure(|cfg| {
HttpServer::new(|| App::new().route("/", web::get().to(index))) super::configure_app(
cfg,
&web_url,
web_dir_path.as_path(),
&swagger_url,
swagger_dir_path.as_path(),
)
})
})
.bind(format!("127.0.0.1:{}", port))? .bind(format!("127.0.0.1:{}", port))?
.run(); .run();

View file

@ -1 +1,21 @@
use std::path::PathBuf;
mod swagger;
mod web;
fn configure_test_app(cfg: &mut actix_web::web::ServiceConfig) {
let web_url = "/";
let web_dir_path = PathBuf::from("web");
let swagger_url = "swagger";
let mut swagger_dir_path = PathBuf::from("docs");
swagger_dir_path.push("swagger");
super::configure_app(
cfg,
web_url,
web_dir_path.as_path(),
swagger_url,
swagger_dir_path.as_path(),
);
}

View file

@ -0,0 +1,21 @@
use actix_web::dev::Service;
use actix_web::test::TestRequest;
use actix_web::{test, App};
#[actix_rt::test]
async fn test_index() {
let app = App::new().configure(super::configure_test_app);
let mut service = test::init_service(app).await;
let req = TestRequest::get().uri("/swagger").to_request();
let resp = service.call(req).await.unwrap();
assert!(resp.status().is_success());
}
#[actix_rt::test]
async fn test_index_with_trailing_slash() {
let app = App::new().configure(super::configure_test_app);
let mut service = test::init_service(app).await;
let req = TestRequest::get().uri("/swagger/").to_request();
let resp = service.call(req).await.unwrap();
assert!(resp.status().is_success());
}

View file

@ -0,0 +1,12 @@
use actix_web::dev::Service;
use actix_web::test::TestRequest;
use actix_web::{test, App};
#[actix_rt::test]
async fn test_index() {
let app = App::new().configure(super::configure_test_app);
let mut service = test::init_service(app).await;
let req = TestRequest::get().uri("/").to_request();
let resp = service.call(req).await.unwrap();
assert!(resp.status().is_success());
}

View file

@ -51,22 +51,22 @@ pub fn get_server(
pub fn run( pub fn run(
port: u16, port: u16,
auth_secret: Option<&[u8]>, auth_secret: Option<&[u8]>,
api_url: &str, api_url: String,
web_url: &str, web_url: String,
web_dir_path: &PathBuf, web_dir_path: PathBuf,
swagger_url: &str, swagger_url: String,
swagger_dir_path: &PathBuf, swagger_dir_path: PathBuf,
db: Arc<DB>, db: Arc<DB>,
command_sender: Arc<CommandSender>, command_sender: Arc<CommandSender>,
) -> Result<()> { ) -> Result<()> {
let server = get_server( let server = get_server(
port, port,
auth_secret, auth_secret,
api_url, &api_url,
web_url, &web_url,
web_dir_path, &web_dir_path,
swagger_url, &swagger_url,
swagger_dir_path, &swagger_dir_path,
db, db,
command_sender, command_sender,
)?; )?;