Compare commits

..

1 Commits
main ... v4

Author SHA1 Message Date
FunctionsAPI
8d444b2aa2 Automatic push from FunctionsAPI 2025-03-27 18:46:37 +00:00
3 changed files with 3428 additions and 0 deletions

3269
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

16
Cargo.toml Normal file
View File

@ -0,0 +1,16 @@
[package]
edition = "2021"
name = "web"
version = "0.1.0"
[dependencies]
axum = { version = "0.8" }
openssl = { version = "0.10", features = ["vendored"] }
pipeline-application = { git = "ssh://git@github.com/fathom-io/pipeline-calculations.git", branch = "johnabell/ili-validation-load" }
pipeline-configuration = { git = "ssh://git@github.com/fathom-io/pipeline-calculations.git", branch = "johnabell/ili-validation-load" }
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.138"
tokio = { version = "1.43.0", features = ["macros", "rt-multi-thread"] }
tracing = { version = "0.1" }
tracing-subscriber = { version = "0.3" }
uuid = { version = "1" }

143
src/main.rs Normal file
View File

@ -0,0 +1,143 @@
use axum::http::HeaderMap;
use axum::{routing::post, Router};
use pipeline_application::application::{Application, Env, SegmentationConfiguration};
use pipeline_configuration::facilities::FacilityType;
use std::io;
use std::net::SocketAddr;
use tokio::net::TcpListener;
use uuid::Uuid;
async fn segment(headers: HeaderMap, body: String) -> String {
let env = std::env::vars().collect::<Vec<_>>();
println!("Request headers: {headers:?}, request body: {body}, env: {env:?}");
let context = Context::from_env().unwrap();
let args = Args::from_body(&body).unwrap();
Application::new(context.env, context.org_id, context.project_id)
.unwrap()
.segment_all(args)
.await
.unwrap();
format!("Request headers: {headers:?}, request body: {body}, env: {env:?}")
}
struct Context {
org_id: Uuid,
project_id: String,
env: Env,
}
impl Context {
fn from_env() -> io::Result<Self> {
let org_id = std::env::var("ORG_ID")
.unwrap_or_else(|_| "2cbfe270-d195-48ad-aed1-24145924635c".to_string())
.parse()
.map_err(io::Error::other)?;
let project_id =
std::env::var("PROJECT_ID").unwrap_or_else(|_| "67acbdae31bc521a3b25def4".to_string());
Ok(Self {
org_id,
project_id,
env: Self::get_env(),
})
}
fn get_env() -> Env {
match std::env::var("ENV") {
Ok(env) if env == "stg" || env == "staging" => Env::Staging,
_ => Env::Dev,
}
}
}
#[tokio::main]
async fn main() {
init_logger();
let router = Router::new().route("/", post(segment));
let addr = SocketAddr::from(([0, 0, 0, 0], 8080));
let tcp = TcpListener::bind(&addr).await.unwrap();
println!("Ready and listening on {}", addr);
axum::serve(tcp, router).await.unwrap();
}
fn init_logger() {
use tracing::{level_filters::LevelFilter, Level};
use tracing_subscriber::{fmt, layer::SubscriberExt};
let subscriber = tracing_subscriber::registry()
.with(LevelFilter::from_level(Level::INFO))
//.with(tracing_subscriber::EnvFilter::from_env("LOG_LEVEL"))
.with(
fmt::Layer::default()
.with_target(true)
.with_thread_names(true)
.with_thread_ids(true)
.with_ansi(true)
.with_line_number(true)
.with_file(true),
);
tracing::subscriber::set_global_default(subscriber)
.expect("Unable to set a global logger instance");
}
#[derive(Debug, serde::Deserialize)]
struct Args {
facilities: Vec<FacilityType>,
local_data: Vec<String>,
}
impl From<Args> for SegmentationConfiguration {
fn from(value: Args) -> Self {
let mut config = Self::default_false();
for fac in value.facilities {
config = match fac {
FacilityType::InsulationJoint => config.by_insulation_joints(true),
FacilityType::Repair => config.by_repairs(true),
FacilityType::Valve => config.by_valves(true),
_ => config,
};
}
// TODO: add crossing configuration
for loc in value.local_data {
config = match loc.as_str() {
"material_grade" => config.by_material_grade(true),
"coating_type" => config.by_coating_type(true),
"design_factor" => config.by_design_factor(true),
"high_consequence_area" => config.by_high_consequence_area(true),
"unusual_sensitive_area" => config.by_unusual_sensitive_area(true),
"joint_type" => config.by_joint_type(true),
"soil_type" => config.by_soil_type(true),
"soil_ph" => config.by_soil_ph(true),
_ => config,
}
}
config
}
}
impl Args {
fn from_body(body: &str) -> serde_json::Result<Self> {
let value: String = serde_json::from_str(body)?;
serde_json::from_str(&value)
}
}
#[cfg(test)]
mod tests {
use super::*;
const INPUT: &str = "\"{\\\"facilities\\\": [\\\"repair\\\"], \\\"formData\\\": {\\\"facilities\\\": [\\\"repairs\\\"], \\\"localProperties\\\": [\\\"material_grade\\\", \\\"coating_type\\\", \\\"soil_type\\\", \\\"soil_ph\\\"]}, \\\"input\\\": null, \\\"local_data\\\": [\\\"material_grade\\\", \\\"coating_type\\\", \\\"soil_type\\\", \\\"soil_ph\\\"]}\"";
#[test]
fn test() {
let env = std::env::vars().collect::<Vec<_>>();
println!("body: {}, env: {env:?}", INPUT);
let args = Args::from_body(INPUT);
dbg!(args).unwrap();
}
}