use crate::data::ApplicationData;
use crate::utils;
use actix_web::{App, HttpResponse, HttpServer, web};
#[cfg(not(feature = "tck"))]
use actix_web::{get, post};
use antex::{ColorMode, StyledText, Text};
#[cfg(not(feature = "tck"))]
use dsntk_common::Jsonify;
#[cfg(not(feature = "tck"))]
use dsntk_feel::FeelScope;
use dsntk_workspace::Workspaces;
#[cfg(not(feature = "tck"))]
use std::borrow::Borrow;
use std::net::IpAddr;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;
use std::{env, io};
const DEFAULT_PORT: u16 = 22022;
const DEFAULT_HOST: &str = "0.0.0.0";
const VARIABLE_HOST: &str = "DSNTK_HOST";
const VARIABLE_PORT: &str = "DSNTK_PORT";
const VARIABLE_DIR: &str = "DSNTK_DIR";
const CONTENT_TYPE: &str = "application/json";
#[cfg(not(feature = "tck"))]
#[get("/evaluate/{path:.*}")]
async fn evaluate_invocable_get(path: web::Path<String>, body: String, data: web::Data<ApplicationData>) -> HttpResponse {
evaluate(path, body, data)
}
#[cfg(not(feature = "tck"))]
#[post("/evaluate/{path:.*}")]
async fn evaluate_invocable_post(path: web::Path<String>, body: String, data: web::Data<ApplicationData>) -> HttpResponse {
evaluate(path, body, data)
}
#[cfg(not(feature = "tck"))]
fn evaluate(path: web::Path<String>, request_body: String, data: web::Data<ApplicationData>) -> HttpResponse {
let workspace: &Workspaces = data.workspaces.borrow();
match dsntk_evaluator::evaluate_context(&FeelScope::default(), &request_body).and_then(|input_data| workspace.evaluate(&path, &input_data)) {
Ok(value) => HttpResponse::Ok().content_type(CONTENT_TYPE).body(format!(r#"{{"data":{}}}"#, value.jsonify())),
Err(reason) => HttpResponse::Ok().content_type(CONTENT_TYPE).body(format!(r#"{{"errors":[{{"detail":"{reason}"}}]}}"#)),
}
}
async fn not_found() -> HttpResponse {
HttpResponse::NotFound().content_type(CONTENT_TYPE).body(r#"{"errors":[{"detail":"endpoint not found"}]}"#)
}
#[cfg(feature = "tck")]
fn config(cfg: &mut web::ServiceConfig) {
cfg.service(crate::tck::evaluate_tck_post);
}
#[cfg(not(feature = "tck"))]
fn config(cfg: &mut web::ServiceConfig) {
cfg.service(evaluate_invocable_get);
cfg.service(evaluate_invocable_post);
}
pub async fn start_server(opt_host: Option<String>, opt_port: Option<String>, dirs: Vec<String>, cm: ColorMode, verbose: bool) -> io::Result<()> {
let application_data = web::Data::new(ApplicationData {
workspaces: Arc::new(Workspaces::new(&resolve_search_paths(dirs), cm, verbose)),
});
let address = get_server_address(opt_host, opt_port);
println!("{}", Text::new(cm).bright_blue().s("dsntk").reset().s(' ').bright_yellow().s(&address).reset());
HttpServer::new(move || {
App::new()
.app_data(application_data.clone())
.app_data(web::PayloadConfig::new(4 * 1024 * 1024))
.configure(config)
.default_service(web::route().to(not_found))
})
.bind(address)?
.run()
.await
}
fn get_server_address(opt_host: Option<String>, opt_port: Option<String>) -> String {
let mut host = DEFAULT_HOST.to_string();
if let Ok(host_ip_address) = env::var(VARIABLE_HOST) {
if is_valid_ip_address(&host_ip_address) {
host = host_ip_address;
} else {
eprintln!("invalid host address specified in environment variable {}: {}", VARIABLE_HOST, host_ip_address);
}
}
if let Some(host_ip_address) = opt_host {
if is_valid_ip_address(&host_ip_address) {
host = host_ip_address;
} else {
eprintln!("invalid host address given as command option: {}", host_ip_address);
}
}
let mut port: u16 = DEFAULT_PORT;
if let Ok(p_str) = env::var(VARIABLE_PORT) {
if let Ok(p) = u16::from_str(&p_str) {
port = p;
} else {
eprintln!("invalid port number specified in environment variable {}: {}", VARIABLE_PORT, p_str);
}
}
if let Some(p_str) = opt_port {
if let Ok(p) = u16::from_str(&p_str) {
port = p;
} else {
eprintln!("invalid port number specified as command option: {}", p_str);
}
}
let server_address = format!("{host}:{port}");
server_address
}
fn is_valid_ip_address(ip: &str) -> bool {
ip == "localhost" || ip.parse::<IpAddr>().is_ok()
}
fn resolve_search_paths(args: Vec<String>) -> Vec<PathBuf> {
let paths = utils::paths_from_variable(VARIABLE_DIR);
if !paths.is_empty() {
return paths;
}
let paths = utils::paths_from_arguments(args);
if !paths.is_empty() {
return paths;
}
utils::current_dir()
}