Removed actix

This commit is contained in:
Antoine Gersant 2020-01-16 23:37:36 -08:00
parent 6f642c34e2
commit 0dba7e2e4f
6 changed files with 1 additions and 1017 deletions

800
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -5,17 +5,12 @@ authors = ["Antoine Gersant <antoine.gersant@lesforges.org>"]
edition = "2018"
[features]
default = ["service-actix"]
default = ["service-rocket"]
ui = []
profile-index = ["flame", "flamer"]
service-actix = ["actix-files", "actix-http", "actix-rt", "actix-web"]
service-rocket = ["rocket", "rocket_contrib"]
[dependencies]
actix-files = { version = "0.2", optional = true }
actix-http = { version = "1.0", optional = true }
actix-web = { version = "2.0", optional = true }
actix-rt = { version = "1.0", optional = true}
anyhow = "1.0"
ape = "0.2.0"
app_dirs = "1.1.1"

View file

@ -1,56 +0,0 @@
use actix_http::ResponseBuilder;
use actix_web::{error, get, http::header, http::StatusCode, put, web, HttpResponse};
use anyhow::*;
use crate::config::{self, Config};
use crate::db::DB;
use crate::service::constants::*;
use crate::service::dto;
use crate::service::error::APIError;
use crate::user;
impl error::ResponseError for APIError {
fn error_response(&self) -> HttpResponse {
ResponseBuilder::new(self.status_code())
.set_header(header::CONTENT_TYPE, "text/html; charset=utf-8")
.body(self.to_string())
}
fn status_code(&self) -> StatusCode {
match *self {
APIError::IncorrectCredentials => StatusCode::UNAUTHORIZED,
APIError::Unspecified => StatusCode::INTERNAL_SERVER_ERROR,
}
}
}
#[get("/version")]
pub async fn get_version() -> Result<HttpResponse, APIError> {
let current_version = dto::Version {
major: CURRENT_MAJOR_VERSION,
minor: CURRENT_MINOR_VERSION,
};
Ok(HttpResponse::Ok().json(current_version))
}
#[get("/initial_setup")]
pub async fn get_initial_setup(db: web::Data<DB>) -> Result<HttpResponse, APIError> {
let user_count = web::block(move || user::count(&db))
.await
.map_err(|_| anyhow!("Could not count users"))?;
let initial_setup = dto::InitialSetup {
has_any_users: user_count > 0,
};
Ok(HttpResponse::Ok().json(initial_setup))
}
#[put("/settings")]
pub async fn put_settings(
db: web::Data<DB>,
// _admin_rights: AdminRights, // TODO.important https://docs.rs/actix-web/2.0.0/actix_web/trait.FromRequest.html
config: web::Json<Config>,
) -> Result<HttpResponse, APIError> {
web::block(move || config::amend(&db, &config))
.await
.map_err(|_| anyhow!("Could not amend config"))?;
Ok(HttpResponse::Ok().finish())
}

View file

@ -1,31 +0,0 @@
use actix_files as fs;
use actix_web::web;
use std::path::Path;
use crate::db::DB;
pub mod server;
mod api;
#[cfg(test)]
pub mod test;
fn configure_app(
cfg: &mut web::ServiceConfig,
web_url: &str,
web_dir_path: &Path,
swagger_url: &str,
swagger_dir_path: &Path,
db: &DB,
) {
// TODO logging
cfg.data(db.clone())
.service(
web::scope("/api")
.service(api::get_initial_setup)
.service(api::get_version)
.service(api::put_settings),
)
.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,40 +0,0 @@
use actix_web::{App, HttpServer};
use anyhow::*;
use std::path::PathBuf;
use std::sync::Arc;
use crate::db::DB;
use crate::index::CommandSender;
pub fn run(
port: u16,
auth_secret: &[u8],
api_url: String,
web_url: String,
web_dir_path: PathBuf,
swagger_url: String,
swagger_dir_path: PathBuf,
db: DB,
command_sender: Arc<CommandSender>,
) -> Result<()> {
let mut runtime = tokio::runtime::Runtime::new()?;
let local_set = tokio::task::LocalSet::new();
let _ = actix_rt::System::run_in_tokio("polaris_service_executor", &local_set);
let server = HttpServer::new(move || {
App::new().configure(|cfg| {
super::configure_app(
cfg,
&web_url,
web_dir_path.as_path(),
&swagger_url,
swagger_dir_path.as_path(),
&db,
)
})
})
.bind(format!("0.0.0.0:{}", port))?
.run();
runtime.block_on(server).map_err(Error::new)
}

View file

@ -1,84 +0,0 @@
use actix_http::{Error, Request};
use actix_web::dev::{Body, ResponseBody, Service, ServiceResponse};
use actix_web::test::TestRequest;
use actix_web::{test, App};
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::fs;
use std::path::PathBuf;
use crate::db::DB;
fn configure_test_app(cfg: &mut actix_web::web::ServiceConfig, db_name: &str) {
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");
let mut db_path = PathBuf::new();
db_path.push("test");
db_path.push(format!("{}.sqlite", db_name));
if db_path.exists() {
fs::remove_file(&db_path).unwrap();
}
let db = DB::new(&db_path).unwrap();
super::configure_app(
cfg,
web_url,
web_dir_path.as_path(),
swagger_url,
swagger_dir_path.as_path(),
&db,
);
}
pub type ServiceType = impl Service<Request = Request, Response = ServiceResponse, Error = Error>;
pub async fn make_service(test_name: &str) -> ServiceType {
let app = App::new().configure(|cfg| configure_test_app(cfg, test_name));
let service = test::init_service(app).await;
service
}
pub async fn get(service: &mut ServiceType, url: &str) {
let req = TestRequest::get().uri(url).to_request();
let resp = service.call(req).await.unwrap();
assert!(resp.status().is_success());
}
pub async fn get_json<T: DeserializeOwned>(service: &mut ServiceType, url: &str) -> T {
let req = TestRequest::get().uri(url).to_request();
let resp = service.call(req).await.unwrap();
assert!(resp.status().is_success());
let body = resp.response().body().as_u8();
let response_json: T = serde_json::from_slice(body).unwrap();
response_json
}
pub async fn put_json<T: Serialize>(service: &mut ServiceType, url: &str, payload: &T) {
let req = TestRequest::put().uri(url).set_json(payload).to_request();
let resp = service.call(req).await.unwrap();
assert!(resp.status().is_success());
}
trait BodyToBytes {
fn as_u8(&self) -> &[u8];
}
impl BodyToBytes for ResponseBody<Body> {
fn as_u8(&self) -> &[u8] {
match self {
ResponseBody::Body(ref b) => match b {
Body::Bytes(ref by) => by.as_ref(),
_ => panic!(),
},
ResponseBody::Other(ref b) => match b {
Body::Bytes(ref by) => by.as_ref(),
_ => panic!(),
},
}
}
}