-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
281 additions
and
53 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Odd computer</title> | ||
<style> | ||
body { | ||
font-family: 'Arial', sans-serif; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
height: 100vh; | ||
margin: 0; | ||
} | ||
|
||
#upload-container { | ||
text-align: center; | ||
padding: 20px; | ||
border: 2px dashed #ccc; | ||
border-radius: 10px; | ||
cursor: pointer; | ||
} | ||
|
||
#file-input { | ||
display: none; | ||
} | ||
|
||
#upload-text { | ||
font-size: 18px; | ||
color: #555; | ||
} | ||
|
||
#result-container { | ||
margin-top: 20px; | ||
display: none; | ||
} | ||
|
||
#json-output { | ||
white-space: pre-line; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div id="upload-container" onclick="handleClick()"> | ||
<input type="file" id="file-input" accept=".json" onchange="handleFile()"> | ||
<p id="upload-text">Click or drag and drop a JSON file containing the plans of the Empire here</p> | ||
</div> | ||
|
||
<div id="result-container"> | ||
<h2>Result from Server:</h2> | ||
<pre id="server-result"></pre> | ||
</div> | ||
|
||
<script> | ||
function handleClick() { | ||
document.getElementById('file-input').click(); | ||
} | ||
|
||
function handleFile() { | ||
const fileInput = document.getElementById('file-input'); | ||
const resultContainer = document.getElementById('result-container'); | ||
|
||
const file = fileInput.files[0]; | ||
if (file) { | ||
const reader = new FileReader(); | ||
reader.onload = function(e) { | ||
const content = e.target.result; | ||
sendDataToServer(content); | ||
resultContainer.style.display = 'block'; | ||
}; | ||
|
||
reader.readAsText(file); | ||
} | ||
} | ||
|
||
|
||
function sendDataToServer(data) { | ||
const url = '/proba'; | ||
fetch(url, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: data, | ||
}) | ||
.then(response => response.text()) | ||
.then(result => { | ||
const serverResult = document.getElementById('server-result'); | ||
serverResult.textContent = result; | ||
}) | ||
.catch(error => { | ||
console.error('Error sending data to server:', error); | ||
}); | ||
} | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use actix_web::{ | ||
dev::Server, get, post, web, App, HttpResponse, HttpServer, Responder, ResponseError, | ||
}; | ||
use anyhow::Result; | ||
|
||
use crate::{ | ||
application_services::{EmpireData, MillenniumFalconData}, | ||
domain_models::{GalaxyRoutes, PlanetCatalog}, | ||
domain_services::compute_probability_of_success, | ||
}; | ||
|
||
struct AppState { | ||
galaxy_routes: GalaxyRoutes, | ||
planet_catalog: PlanetCatalog, | ||
millennium_falcon_data: MillenniumFalconData, | ||
} | ||
|
||
/// Custom Error type that wrap anyhow::Error and implement actix_web::ResponseError | ||
#[derive(thiserror::Error, Debug)] | ||
pub enum Error { | ||
#[error("an internal error occurred: {0}")] | ||
InternalError(#[from] anyhow::Error), | ||
} | ||
|
||
impl ResponseError for Error {} | ||
|
||
#[get("/health_check")] | ||
async fn health_check() -> impl Responder { | ||
HttpResponse::Ok() | ||
} | ||
|
||
#[post("/proba")] | ||
async fn proba(data: web::Data<AppState>, req_body: String) -> std::result::Result<String, Error> { | ||
let empire_data = EmpireData::parse(&req_body)?; | ||
let hunter_planning = empire_data.to_bounty_hunters_planning(&data.planet_catalog); | ||
let proba = compute_probability_of_success( | ||
&hunter_planning, | ||
&data.galaxy_routes, | ||
&data.planet_catalog, | ||
data.millennium_falcon_data.autonomy, | ||
&data.millennium_falcon_data.departure, | ||
&data.millennium_falcon_data.arrival, | ||
empire_data.countdown, | ||
)? * 100.; | ||
Ok(format!("{proba}%")) | ||
} | ||
|
||
#[get("/")] | ||
async fn index() -> impl Responder { | ||
HttpResponse::Ok().body(include_str!("../../front/index.html")) | ||
} | ||
|
||
pub fn run( | ||
address: &str, | ||
galaxy_routes: GalaxyRoutes, | ||
planet_catalog: PlanetCatalog, | ||
millennium_falcon_data: MillenniumFalconData, | ||
) -> Result<Server> { | ||
let server = HttpServer::new(move || { | ||
App::new() | ||
.app_data(web::Data::new(AppState { | ||
galaxy_routes: galaxy_routes.clone(), | ||
planet_catalog: planet_catalog.clone(), | ||
millennium_falcon_data: millennium_falcon_data.clone(), | ||
})) | ||
.service(health_check) | ||
.service(proba) | ||
.service(index) | ||
}) | ||
.bind(address)? | ||
.run(); | ||
Ok(server) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use itertools::Itertools; | ||
|
||
use anyhow::anyhow; | ||
use anyhow::Result; | ||
use std::env; | ||
|
||
pub fn parse_cli() -> Result<(String, String)> { | ||
if let Some((millennium_data_path, empire_data_path)) = env::args().skip(1).collect_tuple() { | ||
Ok((millennium_data_path, empire_data_path)) | ||
} else { | ||
Err(anyhow!( | ||
"script should have 2 arguments, millennium_data_path and empire_data_path", | ||
)) | ||
} | ||
} | ||
|
||
pub fn parse_webserver() -> Result<String> { | ||
if let Some((millennium_data_path,)) = env::args().skip(1).collect_tuple() { | ||
Ok(millennium_data_path) | ||
} else { | ||
Err(anyhow!( | ||
"script should have 1 argument, millennium_data_path", | ||
)) | ||
} | ||
} |
Oops, something went wrong.