Rust backend setup

This commit is contained in:
2023-09-04 14:51:59 -04:00
parent 7a7f90809a
commit acfc89feb8
1296 changed files with 1616 additions and 1056 deletions

View File

@@ -7,19 +7,19 @@ use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, AsChangeset, Insertable)]
#[table_name = "airports"]
pub struct Airport {
pub name: String,
pub full_name: String,
pub icao: String,
pub latitude: f32,
pub longitude: f32,
pub latitude: f64,
pub longitude: f64,
}
#[derive(Serialize, Deserialize, Queryable)]
pub struct Airports {
pub id: i32,
pub name: String,
pub full_name: String,
pub icao: String,
pub latitude: f32,
pub longitude: f32,
pub latitude: f64,
pub longitude: f64,
}
impl Airports {
@@ -28,4 +28,34 @@ impl Airports {
let airports = airports::table.load::<Airports>(&conn)?;
Ok(airports)
}
pub fn find(id: i32) -> Result<Self, CustomError> {
let conn = db::connection()?;
let airport = airports::table.filter(airports::id.eq(id)).first(&conn)?;
Ok(airport)
}
pub fn create(airport: Airport) -> Result<Self, CustomError> {
let conn = db::connection()?;
let airport = Airport::from(airport);
let airport = diesel::insert_into(airports::table)
.values(airport)
.get_result(&conn)?;
Ok(airport)
}
pub fn update(id: i32, airport: Airport) -> Result<Self, CustomError> {
let conn = db::connection()?;
let airport = diesel::update(airports::table)
.filter(airports::id.eq(id))
.set(airport)
.get_result(&conn)?;
Ok(airport)
}
pub fn delete(id: i32) -> Result<usize, CustomError> {
let conn = db::connection()?;
let res = diesel::delete(airports::table.filter(airports::id.eq(id))).execute(&conn)?;
Ok(res)
}
}

View File

@@ -9,6 +9,37 @@ async fn find_all() -> Result<HttpResponse, CustomError> {
Ok(HttpResponse::Ok().json(airports))
}
#[get("/airports/{id}")]
async fn find(id: web::Path<i32>) -> Result<HttpResponse, CustomError> {
let airport = Airports::find(id.into_inner())?;
Ok(HttpResponse::Ok().json(airport))
}
#[post("/airports")]
async fn create(airport: web::Json<Airport>) -> Result<HttpResponse, CustomError> {
let airport = Airports::create(airport.into_inner())?;
Ok(HttpResponse::Ok().json(airport))
}
#[put("/airports/{id}")]
async fn update(
id: web::Path<i32>,
airport: web::Json<Airport>,
) -> Result<HttpResponse, CustomError> {
let airport = Airports::update(id.into_inner(), airport.into_inner())?;
Ok(HttpResponse::Ok().json(airport))
}
#[delete("/airports/{id}")]
async fn delete(id: web::Path<i32>) -> Result<HttpResponse, CustomError> {
let deleted_airport = Airports::delete(id.into_inner())?;
Ok(HttpResponse::Ok().json(json!({ "deleted": deleted_airport })))
}
pub fn init_routes(config: &mut web::ServiceConfig) {
config.service(find_all);
config.service(find);
config.service(create);
config.service(update);
config.service(delete);
}

View File

@@ -2,13 +2,14 @@ use crate::error_handler::CustomError;
use diesel::pg::PgConnection;
use diesel::r2d2::ConnectionManager;
use lazy_static::lazy_static;
use log::{error, info};
use r2d2;
use std::env;
type Pool = r2d2::Pool<ConnectionManager<PgConnection>>;
pub type DbConnection = r2d2::PooledConnection<ConnectionManager<PgConnection>>;
embed_migrations!();
diesel_migrations::embed_migrations!();
lazy_static! {
static ref POOL: Pool = {
@@ -21,7 +22,10 @@ lazy_static! {
pub fn init() {
lazy_static::initialize(&POOL);
let conn = connection().expect("Failed to get db connection");
embedded_migrations::run(&conn).unwrap();
match embedded_migrations::run(&conn) {
Ok(_) => info!("Database initialized"),
Err(err) => error!("Failed to initialize database; {}", err),
};
}
pub fn connection() -> Result<DbConnection, CustomError> {

View File

@@ -1,11 +1,11 @@
use actix_web::http::StatusCode;
use actix_web::{HttpResponse, ResponseError};
use diesel::result::Error as DieselError;
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::fmt;
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Serialize)]
pub struct CustomError {
pub error_status_code: u16,
pub error_message: String,
@@ -31,7 +31,7 @@ impl From<DieselError> for CustomError {
match error {
DieselError::DatabaseError(_, err) => CustomError::new(409, err.message().to_string()),
DieselError::NotFound => {
CustomError::new(404, "The employee record not found".to_string())
CustomError::new(404, "The airport record was not found".to_string())
}
err => CustomError::new(500, format!("Unknown Diesel error: {}", err)),
}

View File

@@ -11,15 +11,20 @@ use std::env;
mod airports;
mod db;
mod error_handler;
mod metars;
mod schema;
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
dotenv().ok();
env_logger::init();
db::init();
let mut listenfd = ListenFd::from_env();
let mut server = HttpServer::new(|| App::new().configure(airports::init_routes));
let mut server = HttpServer::new(|| App::new()
.configure(airports::init_routes)
.configure(metars::init_routes)
);
server = match listenfd.take_tcp_listener(0)? {
Some(listener) => server.listen(listener)?,

View File

@@ -0,0 +1,5 @@
mod model;
mod routes;
pub use model::*;
pub use routes::init_routes;

162
backend/src/metars/model.rs Normal file
View File

@@ -0,0 +1,162 @@
use crate::error_handler::CustomError;
use crate::schema::metars;
use log::warn;
use std::io::BufRead;
use quick_xml::{Reader, events::{Event, BytesStart}, Writer, de::Deserializer};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct QualityControlFlags {
pub auto: Option<bool>,
pub auto_station: Option<bool>
}
#[derive(Serialize, Deserialize, AsChangeset, Insertable)]
#[table_name = "metars"]
pub struct Metar {
pub icao: String,
pub raw_text: String,
pub station_id: String,
pub observation_time: String,
pub latitude: f64,
pub longitude: f64,
pub temp_c: f64,
pub dewpoint_c: f64,
pub wind_dir_degrees: i32,
pub wind_speed_kt: i32,
pub visibility_statute_mi: String,
pub altim_in_hg: f64,
pub sea_level_pressure_mb: Option<f64>,
// pub quality_control_flags: Option<QualityControlFlags>,
pub wx_string: Option<String>,
// pub sky_con dition: Option<Vec<String>>, // TODO work on attributes
pub flight_category: String,
pub three_hr_pressure_tendency_mb: Option<f64>,
pub metar_type: String,
#[serde(rename = "maxT_c")]
pub max_t_c: Option<f64>,
#[serde(rename = "minT_c")]
pub min_t_c: Option<f64>,
pub precip_in: Option<f64>,
pub elevation_m: i32
}
#[derive(Serialize, Deserialize, Queryable)]
pub struct Metars {
// pub id: i32,
// pub icao: String,
pub raw_text: String,
pub station_id: String,
pub observation_time: String,
pub latitude: f64,
pub longitude: f64,
pub temp_c: f64,
pub dewpoint_c: f64,
pub wind_dir_degrees: i32,
pub wind_speed_kt: i32,
pub visibility_statute_mi: String,
pub altim_in_hg: f64,
pub sea_level_pressure_mb: Option<f64>,
pub quality_control_flags: Option<QualityControlFlags>,
pub wx_string: Option<String>,
// pub sky_condition: Option<Vec<String>>, // TODO work on attributes
pub flight_category: String,
pub three_hr_pressure_tendency_mb: Option<f64>,
pub metar_type: String,
#[serde(rename = "maxT_c")]
pub max_t_c: Option<f64>,
#[serde(rename = "minT_c")]
pub min_t_c: Option<f64>,
pub precip_in: Option<f64>,
pub elevation_m: i32
}
impl Metars {
pub async fn get_all(icaos: String) -> Result<Vec<Self>, CustomError> {
// let station_string = station_icaos.join(",");
let url = format!("https://beta.aviationweather.gov/cgi-bin/data/metar.php?ids={}&format=xml", icaos);
let metars: Vec<Metars> = match reqwest::get(url).await {
Ok(r) => match r.text().await {
Ok(r) => {
match Metars::parse(r) {
Ok(m) => m,
Err(err) => {
warn!("{}", err);
vec![]
}
}
},
Err(err) => {
warn!("Unable to parse METAR request: {}", err);
vec![]
}
},
Err(err) => {
warn!("Unable to get METAR request: {}", err);
vec![]
}
};
Ok(metars)
}
pub fn parse(input: String) -> Result<Vec<Self>, CustomError> {
if input.is_empty() {
return Err(CustomError::new(500, "Input is empty".to_string()))
}
let mut reader = Reader::from_str(&input);
let mut buf = Vec::new();
let mut junk_buf: Vec<u8> = Vec::new();
let mut metars: Vec<Self> = vec![];
loop {
match reader.read_event_into(&mut buf) {
Err(e) => panic!("Error at position: {}: {:?}", reader.buffer_position(), e),
Ok(Event::Eof) => break,
Ok(Event::Start(e)) => {
match e.name().as_ref() {
b"METAR" => {
let metar_bytes = Metars::read_to_end_into_buffer(&mut reader, &e, &mut junk_buf).unwrap();
let str = std::str::from_utf8(&metar_bytes).unwrap();
let mut deserializer = Deserializer::from_str(str);
let metar = Metars::deserialize(&mut deserializer).unwrap();
metars.push(metar);
},
_ => ()
}
},
_ => ()
}
}
return Ok(metars)
}
// https://capnfabs.net/posts/parsing-huge-xml-quickxml-rust-serde/
pub fn read_to_end_into_buffer<R: BufRead>(reader: &mut Reader<R>, start_tag: &BytesStart, junk_buf: &mut Vec<u8>) -> Result<Vec<u8>, quick_xml::Error> {
let mut depth = 0;
let mut output_buf: Vec<u8> = Vec::new();
let mut w = Writer::new(&mut output_buf);
let tag_name = start_tag.name();
w.write_event(Event::Start(start_tag.clone()))?;
loop {
junk_buf.clear();
let event = reader.read_event_into(junk_buf)?;
w.write_event(&event)?;
match event {
Event::Start(e) if e.name() == tag_name => depth += 1,
Event::End(e) if e.name() == tag_name => {
if depth == 0 {
return Ok(output_buf);
}
depth -= 1;
}
Event::Eof => {
panic!("EOF")
}
_ => {}
}
}
}
}

View File

@@ -0,0 +1,18 @@
use crate::error_handler::CustomError;
use crate::metars::Metars;
use actix_web::{get, web, HttpResponse};
#[get("metars/{ids}")]
async fn get_all(ids: web::Path<String>) -> Result<HttpResponse, CustomError> {
let airports = web::block(|| Ok::<_, CustomError>(async {Metars::get_all(ids.into_inner()).await}))
.await
.unwrap()
.unwrap()
.await
.unwrap();
Ok(HttpResponse::Ok().json(airports))
}
pub fn init_routes(config: &mut web::ServiceConfig) {
config.service(get_all);
}

View File

@@ -1,9 +1,36 @@
table! {
diesel::table! {
airports (id) {
id -> Int4,
name -> Varchar,
icao -> Varchar,
latitude: Int4,
longitude: Int4
id -> Integer,
full_name -> Text,
icao -> Text,
latitude -> Double,
longitude -> Double,
}
}
}
diesel::table! {
metars (id) {
id -> Integer,
icao -> Text,
raw_text -> Text,
station_id -> Text,
observation_time -> Text,
latitude -> Double,
longitude -> Double,
temp_c -> Double,
dewpoint_c -> Double,
wind_dir_degrees -> Integer,
wind_speed_kt -> Integer,
visibility_statute_mi -> Text,
altim_in_hg -> Double,
sea_level_pressure_mb -> Nullable<Double>,
wx_string -> Nullable<Text>,
flight_category -> Text,
three_hr_pressure_tendency_mb -> Nullable<Double>,
metar_type -> Text,
max_t_c -> Nullable<Double>,
min_t_c -> Nullable<Double>,
precip_in -> Nullable<Double>,
elevation_m -> Integer,
}
}