Overhaul refactor. Still things in progress

This commit is contained in:
2025-04-05 22:42:13 -04:00
parent 310d1eaad8
commit 769762dfa7
133 changed files with 119890 additions and 8784 deletions

2023
api/Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"
authors = ["Ben Sherriff <hello@bensherriff.com>"]
repository = "https://github.com/bensherriff/aviation-weather"
readme = "README.md"
readme = "../README.md"
license = "GPL-3.0-or-later"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -16,12 +16,8 @@ actix-web-httpauth = "0.8.2"
actix-multipart = "0.7.2"
chrono = { version = "0.4.38", features = ["serde"] }
dotenv = "0.15.0"
diesel = { version = "2.2.4", features = ["postgres", "r2d2", "uuid", "chrono", "serde_json"] }
postgis_diesel = { version = "2.4.1", features = ["serde"] }
diesel_migrations = { version = "2.2.0", features = ["postgres"] }
sqlx = { version = "0.8.2", features = ["runtime-tokio", "postgres", "chrono", "uuid"] }
env_logger = "0.11.5"
lazy_static = "1.5.0"
r2d2 = "0.8.10"
reqwest = "0.12.7"
serde = {version = "1.0.209", features = ["derive"]}
serde_json = "1.0.127"
@@ -29,9 +25,11 @@ tokio = { version = "1.40.0", features = ["macros", "rt", "time"] }
uuid = { version = "1.10.0", features = ["serde", "v4"] }
log = "0.4.22"
argon2 = "0.5.3"
redis = { version = "0.26.1", features = ["tokio-comp", "connection-manager", "r2d2"] }
redis = { version = "0.26.1", features = ["tokio-comp", "connection-manager", "r2d2", "json"] }
regex = "1.10.6"
futures-util = "0.3.30"
rust-s3 = "0.35.1"
rand = "0.8.5"
rand_chacha = "0.3.1"
geo-types = "0.7.15"
byteorder = "1.5.0"

View File

@@ -1,7 +0,0 @@
# For documentation on how to configure this file,
# see diesel.rs/guides/configuring-diesel-cli
[print_schema]
file = "src/schema.rs"
custom_type_derives = ["diesel::sql_types::SqlType", "std::fmt::Debug"]
import_types = ["diesel::sql_types::*", "postgis_diesel::sql_types::*"]

View File

@@ -1 +0,0 @@
DROP TABLE airports;

View File

@@ -1,13 +0,0 @@
CREATE EXTENSION IF NOT EXISTS postgis;
CREATE TABLE IF NOT EXISTS airports (
icao TEXT PRIMARY KEY NOT NULL,
category TEXT NOT NULL,
name TEXT NOT NULL,
elevation_ft REAL NOT NULL,
iso_country TEXT NOT NULL,
iso_region TEXT NOT NULL,
municipality TEXT NOT NULL,
has_metar BOOLEAN NOT NULL DEFAULT FALSE,
point GEOMETRY(POINT,4326) NOT NULL,
data JSONB NOT NULL
);

View File

@@ -1 +0,0 @@
DROP TABLE metars;

View File

@@ -1,7 +0,0 @@
CREATE TABLE IF NOT EXISTS metars (
id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
icao TEXT NOT NULL,
observation_time TIMESTAMP NOT NULL,
raw_text TEXT NOT NULL,
data JSONB NOT NULL
);

View File

@@ -1 +0,0 @@
DROP TABLE airport_metar_cache;

View File

@@ -1,5 +0,0 @@
CREATE TABLE IF NOT EXISTS airport_metar_cache (
icao TEXT PRIMARY KEY NOT NULL,
has_metar BOOLEAN NOT NULL DEFAULT FALSE,
last_checked TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

View File

@@ -1 +0,0 @@
DROP TABLE users;

View File

@@ -1,12 +0,0 @@
CREATE TABLE IF NOT EXISTS users (
email TEXT PRIMARY KEY NOT NULL,
hash TEXT NOT NULL,
role TEXT NOT NULL,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
profile_picture TEXT,
favorites TEXT[] NOT NULL DEFAULT '{}',
verified BOOLEAN NOT NULL DEFAULT FALSE
);

View File

@@ -0,0 +1,35 @@
CREATE EXTENSION IF NOT EXISTS postgis;
CREATE TABLE IF NOT EXISTS airports (
icao TEXT PRIMARY KEY NOT NULL,
iata TEXT,
local TEXT,
name TEXT NOT NULL,
category TEXT NOT NULL,
iso_country TEXT NOT NULL,
iso_region TEXT NOT NULL,
municipality TEXT NOT NULL,
elevation_ft REAL NOT NULL,
longitude REAL NOT NULL,
latitude REAL NOT NULL,
has_tower BOOLEAN DEFAULT false,
has_beacon BOOLEAN DEFAULT false,
public BOOLEAN DEFAULT false
);
CREATE TABLE IF NOT EXISTS metars (
icao TEXT NOT NULL,
observation_time TIMESTAMPTZ NOT NULL,
raw_text TEXT NOT NULL,
data JSONB NOT NULL
);
CREATE TABLE IF NOT EXISTS users (
email TEXT PRIMARY KEY NOT NULL,
password_hash TEXT NOT NULL,
role TEXT NOT NULL,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

233
api/src/airports/delete.rs Normal file
View File

@@ -0,0 +1,233 @@
use std::fmt::Display;
use std::str::FromStr;
use crate::db;
use log::error;
use serde::{Deserialize, Serialize};
use sqlx::postgres::types::PgPoint;
use crate::error::ApiResult;
const TABLE_NAME: &str = "airports";
#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
struct RunwayDb {
pub icao: String,
pub id: String,
pub length_ft: f32,
pub width_ft: f32,
pub surface: String,
}
#[derive(Debug)]
pub struct AirportFilter {
pub icaos: Option<Vec<String>>,
pub name: Option<String>,
// pub bounds: Option<Polygon<Point>>,
pub categories: Option<Vec<AirportCategory>>,
pub has_metar: Option<bool>,
}
impl Default for AirportFilter {
fn default() -> Self {
AirportFilter {
icaos: None,
name: None,
// bounds: None,
categories: None,
has_metar: None,
}
}
}
impl AirportDb {
pub async fn find_all(_filter: &AirportFilter, _limit: i32, _page: i32) -> ApiResult<Vec<Self>> {
let pool = db::pool();
let airports: Vec<Self> = sqlx::query_as::<_, Self>(&format!(
"SELECT * FROM {}",
TABLE_NAME
))
.fetch_all(pool)
.await?;
Ok(airports)
}
pub async fn count(_filter: &AirportFilter) -> ApiResult<i64> {
let pool = db::pool();
let count: i64 = sqlx::query_scalar::<_, i64>(&format!(
"SELECT COUNT(*) FROM {}",
TABLE_NAME
))
.fetch_one(pool)
.await?;
Ok(count)
}
// fn build_query<'a>(
// mut query: QueryBuilder<'a, Postgres>,
// filter: &'a AirportFilter,
// ) -> QueryBuilder<'a, Postgres> {
// if let Some(bounds) = &filter.bounds {
// // convert bounds to a WKT polygon
// if bounds.rings.len() > 1 {
// return Err(ApiError {
// status: 400,
// message: "Only one polygon is allowed".to_string(),
// });
// } else {
// let mut points: Vec<String> = vec![];
// bounds.rings.iter().for_each(|ring| {
// ring.iter().for_each(|point| {
// points.push(format!("{} {}", point.get_x(), point.get_y()));
// });
// });
// let bounds = format!("POLYGON(({}))", points.join(","));
// query.push(format!(
// "ST_Contains(ST_GeomFromText('{}', 4326), point)",
// bounds
// ));
// }
// }
// if let Some(categories) = &filter.categories {
// query.push(format!(
// "({})",
// categories
// .iter()
// .map(|category| format!("category = '{}'", category.to_string()))
// .collect::<Vec<String>>()
// .join(" OR ")
// ));
// }
//
// fn sanitize_icao(icao: &str) -> String {
// // Sanitize search to only allow [a-zA-Z0-9-\\s]
// icao
// .chars()
// .filter(|c| c.is_alphanumeric() || *c == '-' || *c == ' ')
// .collect::<String>()
// }
//
// if &filter.icaos.is_some() == &true && &filter.name.is_some() == &true {
// let icaos = filter.icaos.as_ref().unwrap();
// let name = sanitize_icao(filter.name.as_ref().unwrap());
// let icao_part = format!(
// "({})",
// icaos
// .iter()
// .map(|icao| format!("icao ILIKE '{}'", sanitize_icao(icao)))
// .collect::<Vec<String>>()
// .join(" OR ")
// );
// let name_part = format!("name ILIKE '%{}%'", name);
// parts.push(format!("({} OR {})", icao_part, name_part));
// } else if let Some(icaos) = &filter.icaos {
// parts.push(format!(
// "({})",
// icaos
// .iter()
// .map(|icao| format!("icao ILIKE '{}'", sanitize_icao(icao)))
// .collect::<Vec<String>>()
// .join(" OR ")
// ));
// } else if let Some(name) = &filter.name {
// let search = sanitize_icao(name);
// parts.push(format!("name ILIKE '%{}%'", search));
// }
// if let Some(has_metar) = &filter.has_metar {
// parts.push(format!("has_metar = {}", has_metar));
// }
//
// if parts.len() > 0 {
// query = format!("{} WHERE {}", query, parts.join(" AND "));
// }
//
// return Ok(query);
// }
pub async fn find_by_icao(icao: &str) -> ApiResult<Self> {
let pool = db::pool();
let airport =
sqlx::query_as::<_, Self>(&format!("SELECT * FROM {} WHERE icao = $1", TABLE_NAME))
.bind(icao)
.fetch_one(pool)
.await?;
Ok(airport)
}
pub async fn insert(&self) -> ApiResult<()> {
let pool = db::pool();
sqlx::query(&format!(
"INSERT INTO {} (
icao,
category,
name,
elevation_ft,
iso_country,
iso_region,
municipality,
has_metar,
point,
data
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10
)",
TABLE_NAME
))
.bind(self.icao.clone())
.bind(self.category.clone())
.bind(&self.name)
.bind(self.elevation_ft)
.bind(self.iso_country.clone())
.bind(self.iso_region.clone())
.bind(self.municipality.clone())
.bind(self.has_metar.clone())
// .bind(self.point.clone())
.bind(self.data.clone())
.execute(pool)
.await?;
Ok(())
}
// pub fn insert_vec(airports: Vec<Self>) -> ApiResult<Vec<Self>> {
// let mut conn: r2d2::PooledConnection<diesel::r2d2::ConnectionManager<PgConnection>> =
// db::connection()?;
// let mut inserted_airports: Vec<Self> = vec![];
// for airport in airports {
// let airport = Self::from(airport);
// let airport = diesel::insert_into(airports::table)
// .values(airport)
// .on_conflict_do_nothing()
// .get_result(&mut conn)?;
// inserted_airports.push(airport);
// }
// Ok(inserted_airports)
// }
pub async fn update(&self) -> ApiResult<()> {
// let mut conn = db::pool()?;
// let airport = diesel::update(airports::table)
// .filter(airports::icao.eq(airport.icao.clone()))
// .set(airport)
// .get_result(&mut conn)?;
// Ok(airport)
Ok(())
}
pub async fn delete_all() -> ApiResult<()> {
Ok(())
}
pub async fn delete_by_icao(_icao: &str) -> ApiResult<()> {
// let mut conn = db::pool()?;
// let res = match icao {
// Some(icao) => {
// diesel::delete(airports::table.filter(airports::icao.eq(icao))).execute(&mut conn)?
// }
// None => diesel::delete(airports::table).execute(&mut conn)?,
// };
// Ok(res)
Ok(())
}
}

View File

@@ -1,432 +0,0 @@
use std::fmt::Display;
use std::str::FromStr;
use crate::db;
use crate::error::{ApiError, ApiResult};
use crate::db::schema::airports;
use diesel::prelude::*;
use diesel::sql_query;
use log::error;
use postgis_diesel::types::*;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct Runway {
pub id: String,
pub length_ft: f32,
pub width_ft: f32,
pub surface: String,
}
#[derive(Serialize, Deserialize)]
pub struct Frequency {
pub id: String,
pub frequency_mhz: f32,
}
#[derive(Serialize, Deserialize)]
pub struct Airport {
pub icao: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub iata: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub local: Option<String>,
pub name: String,
pub category: AirportCategory,
pub iso_country: String,
pub iso_region: String,
pub municipality: String,
pub elevation_ft: f32,
pub latitude: f64,
pub longitude: f64,
#[serde(skip_serializing_if = "Option::is_none")]
pub has_tower: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub has_beacon: Option<bool>,
pub runways: Vec<Runway>,
pub frequencies: Vec<Frequency>,
pub public: bool,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct AirportMetarCache {
pub icao: String,
pub has_metar: bool,
pub last_checked: chrono::NaiveDateTime,
}
impl Into<QueryAirport> for Airport {
fn into(self) -> QueryAirport {
return QueryAirport {
icao: self.icao.clone(),
category: self.category.clone().to_string(),
name: self.name.clone(),
elevation_ft: self.elevation_ft,
iso_country: self.iso_country.clone(),
iso_region: self.iso_region.clone(),
municipality: self.municipality.clone(),
has_metar: false,
point: Point::new(self.longitude, self.latitude, Some(4326)),
data: match serde_json::to_value(&self) {
Ok(d) => d,
Err(err) => {
error!("{}", err);
serde_json::Value::Null
}
},
};
}
}
impl From<QueryAirport> for Airport {
fn from(airport: QueryAirport) -> Self {
serde_json::from_value(airport.data).unwrap()
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum AirportCategory {
#[serde(rename = "small_airport")]
Small,
#[serde(rename = "medium_airport")]
Medium,
#[serde(rename = "large_airport")]
Large,
#[serde(rename = "heliport")]
Heliport,
#[serde(rename = "closed")]
Closed,
#[serde(rename = "seaplane_base")]
Seaplane,
#[serde(rename = "balloonport")]
Balloonport,
#[serde(rename = "unknown")]
Unknown,
}
impl FromStr for AirportCategory {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"small_airport" => Ok(AirportCategory::Small),
"medium_airport" => Ok(AirportCategory::Medium),
"large_airport" => Ok(AirportCategory::Large),
"heliport" => Ok(AirportCategory::Heliport),
"closed" => Ok(AirportCategory::Closed),
"seaplane_base" => Ok(AirportCategory::Seaplane),
"balloonport" => Ok(AirportCategory::Balloonport),
_ => Ok(AirportCategory::Unknown),
}
}
}
impl Display for AirportCategory {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AirportCategory::Small => write!(f, "small_airport"),
AirportCategory::Medium => write!(f, "medium_airport"),
AirportCategory::Large => write!(f, "large_airport"),
AirportCategory::Heliport => write!(f, "heliport"),
AirportCategory::Closed => write!(f, "closed"),
AirportCategory::Seaplane => write!(f, "seaplane_base"),
AirportCategory::Balloonport => write!(f, "balloonport"),
AirportCategory::Unknown => write!(f, "unknown"),
}
}
}
#[derive(Serialize, Deserialize, AsChangeset, Insertable, Queryable, QueryableByName)]
#[diesel(table_name = airports)]
pub struct QueryAirport {
pub icao: String,
pub category: String,
pub name: String,
pub elevation_ft: f32,
pub iso_country: String,
pub iso_region: String,
pub municipality: String,
pub has_metar: bool,
pub point: Point,
pub data: serde_json::Value,
}
#[derive(Debug)]
pub struct QueryFilters {
pub icaos: Option<Vec<String>>,
pub name: Option<String>,
pub bounds: Option<Polygon<Point>>,
pub categories: Option<Vec<AirportCategory>>,
pub order_field: Option<QueryOrderField>,
pub order_by: Option<QueryOrderBy>,
pub has_metar: Option<bool>,
}
impl Default for QueryFilters {
fn default() -> Self {
QueryFilters {
icaos: None,
name: None,
bounds: None,
categories: None,
order_field: None,
order_by: None,
has_metar: None,
}
}
}
#[derive(Debug)]
pub enum QueryOrderBy {
Asc,
Desc,
}
impl FromStr for QueryOrderBy {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"asc" => Ok(QueryOrderBy::Asc),
"desc" => Ok(QueryOrderBy::Desc),
_ => Err(()),
}
}
}
#[derive(Debug)]
pub enum QueryOrderField {
Icao,
Name,
Category,
Country,
Region,
Municipality,
}
impl FromStr for QueryOrderField {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"icao" => Ok(QueryOrderField::Icao),
"name" => Ok(QueryOrderField::Name),
"category" => Ok(QueryOrderField::Category),
"iso_country" => Ok(QueryOrderField::Country),
"iso_region" => Ok(QueryOrderField::Region),
"municipality" => Ok(QueryOrderField::Municipality),
_ => Err(()),
}
}
}
impl QueryAirport {
pub fn get_all(filters: &QueryFilters, limit: i32, page: i32) -> ApiResult<Vec<Self>> {
let mut conn = db::connection()?;
let mut query: String = "SELECT * FROM airports".to_string();
query = format!("{} {}", query, QueryAirport::build_filter_query(&filters)?);
query = format!("{} ORDER BY has_metar DESC", query);
if let Some(order_by) = &filters.order_by {
match order_by {
QueryOrderBy::Asc => {
if let Some(order_field) = &filters.order_field {
query = match order_field {
QueryOrderField::Icao => format!("{}, icao ASC", query),
QueryOrderField::Name => format!("{}, name ASC", query),
QueryOrderField::Category => format!("{}, category ASC", query),
QueryOrderField::Country => format!("{}, iso_country ASC", query),
QueryOrderField::Region => format!("{}, iso_region ASC", query),
QueryOrderField::Municipality => format!("{}, municipality ASC", query),
};
};
}
QueryOrderBy::Desc => {
if let Some(order_field) = &filters.order_field {
query = match order_field {
QueryOrderField::Icao => format!("{}, icao DESC", query),
QueryOrderField::Name => format!("{}, name DESC", query),
QueryOrderField::Category => format!("{}, category DESC", query),
QueryOrderField::Country => format!("{}, iso_country DESC", query),
QueryOrderField::Region => format!("{}, iso_region DESC", query),
QueryOrderField::Municipality => format!("{}, municipality DESC", query),
};
};
}
}
}
// Limit query to page and limit
query = format!("{} LIMIT {} OFFSET {}", query, limit, (page - 1) * limit);
let airports: Vec<QueryAirport> = match sql_query(query).load(&mut conn) {
Ok(a) => a,
Err(err) => {
return Err(ApiError {
status: 500,
message: format!("{}", err),
})
}
};
Ok(airports)
}
pub fn get_count(filters: &QueryFilters) -> ApiResult<i64> {
let mut conn = db::connection()?;
let mut query = "SELECT COUNT(*) FROM airports".to_string();
query = format!("{} {}", query, QueryAirport::build_filter_query(&filters)?);
// TODO: Fix this to use get_result() instead of building this table to do the load()
diesel::table! {
airports (count) {
count -> BigInt,
}
}
#[derive(Debug, Queryable, QueryableByName)]
#[diesel(table_name = airports)]
struct Count {
count: i64,
}
let count: Vec<Count> = match sql_query(query).load(&mut conn) {
Ok(a) => a,
Err(err) => {
return Err(ApiError {
status: 500,
message: format!("{}", err),
})
}
};
return Ok(count[0].count);
}
// TODO: Unsafe query, need to sanitize inputs
fn build_filter_query(filters: &QueryFilters) -> ApiResult<String> {
let mut query = "".to_string();
let mut parts: Vec<String> = vec![];
if let Some(bounds) = &filters.bounds {
// convert bounds to a WKT polygon
if bounds.rings.len() > 1 {
return Err(ApiError {
status: 400,
message: "Only one polygon is allowed".to_string(),
});
} else {
let mut points: Vec<String> = vec![];
bounds.rings.iter().for_each(|ring| {
ring.iter().for_each(|point| {
points.push(format!("{} {}", point.get_x(), point.get_y()));
});
});
let bounds = format!("POLYGON(({}))", points.join(","));
parts.push(format!(
"ST_Contains(ST_GeomFromText('{}', 4326), point)",
bounds
));
}
}
if let Some(categories) = &filters.categories {
parts.push(format!(
"({})",
categories
.iter()
.map(|category| format!("category = '{}'", category.to_string()))
.collect::<Vec<String>>()
.join(" OR ")
));
}
fn sanitize_icao(icao: &str) -> String {
// Sanitize search to only allow [a-zA-Z0-9-\\s]
icao
.chars()
.filter(|c| c.is_alphanumeric() || *c == '-' || *c == ' ')
.collect::<String>()
}
if &filters.icaos.is_some() == &true && &filters.name.is_some() == &true {
let icaos = filters.icaos.as_ref().unwrap();
let name = sanitize_icao(filters.name.as_ref().unwrap());
let icao_part = format!(
"({})",
icaos
.iter()
.map(|icao| format!("icao ILIKE '{}'", sanitize_icao(icao)))
.collect::<Vec<String>>()
.join(" OR ")
);
let name_part = format!("name ILIKE '%{}%'", name);
parts.push(format!("({} OR {})", icao_part, name_part));
} else if let Some(icaos) = &filters.icaos {
parts.push(format!(
"({})",
icaos
.iter()
.map(|icao| format!("icao ILIKE '{}'", sanitize_icao(icao)))
.collect::<Vec<String>>()
.join(" OR ")
));
} else if let Some(name) = &filters.name {
let search = sanitize_icao(name);
parts.push(format!("name ILIKE '%{}%'", search));
}
if let Some(has_metar) = &filters.has_metar {
parts.push(format!("has_metar = {}", has_metar));
}
if parts.len() > 0 {
query = format!("{} WHERE {}", query, parts.join(" AND "));
}
return Ok(query);
}
pub fn get(icao: &str) -> ApiResult<Self> {
let mut conn = db::connection()?;
let airport = airports::table
.filter(airports::icao.eq(icao))
.first(&mut conn)?;
Ok(airport)
}
pub fn insert(airport: Self) -> ApiResult<Self> {
let mut conn: r2d2::PooledConnection<diesel::r2d2::ConnectionManager<PgConnection>> =
db::connection()?;
let airport = Self::from(airport);
let airport = diesel::insert_into(airports::table)
.values(airport)
.on_conflict_do_nothing()
.get_result(&mut conn)?;
Ok(airport)
}
pub fn insert_all(airports: Vec<Self>) -> ApiResult<Vec<Self>> {
let mut conn: r2d2::PooledConnection<diesel::r2d2::ConnectionManager<PgConnection>> =
db::connection()?;
let mut inserted_airports: Vec<Self> = vec![];
for airport in airports {
let airport = Self::from(airport);
let airport = diesel::insert_into(airports::table)
.values(airport)
.on_conflict_do_nothing()
.get_result(&mut conn)?;
inserted_airports.push(airport);
}
Ok(inserted_airports)
}
pub fn update(airport: Self) -> ApiResult<Self> {
let mut conn = db::connection()?;
let airport = diesel::update(airports::table)
.filter(airports::icao.eq(airport.icao.clone()))
.set(airport)
.get_result(&mut conn)?;
Ok(airport)
}
pub fn delete(icao: Option<String>) -> ApiResult<usize> {
let mut conn = db::connection()?;
let res = match icao {
Some(icao) => {
diesel::delete(airports::table.filter(airports::icao.eq(icao))).execute(&mut conn)?
}
None => diesel::delete(airports::table).execute(&mut conn)?,
};
Ok(res)
}
}

View File

@@ -0,0 +1,288 @@
use std::str::FromStr;
use actix_web::web::Json;
use serde::{Deserialize, Serialize};
use sqlx::{Postgres, QueryBuilder};
use crate::airports::model::airport_category::AirportCategory;
use crate::airports::{Frequency, Runway, UpdateFrequency, UpdateRunway};
use crate::db;
use crate::error::ApiResult;
const TABLE_NAME: &str = "airports";
#[derive(Debug, Serialize, Deserialize)]
pub struct Airport {
pub icao: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub iata: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub local: Option<String>,
pub name: String,
pub category: AirportCategory,
pub iso_country: String,
pub iso_region: String,
pub municipality: String,
pub elevation_ft: f32,
pub longitude: f32,
pub latitude: f32,
#[serde(skip_serializing_if = "Option::is_none")]
pub has_tower: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub has_beacon: Option<bool>,
pub runways: Vec<Runway>,
pub frequencies: Vec<Frequency>,
pub public: bool,
}
#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
struct AirportRow {
pub icao: String,
pub iata: Option<String>,
pub local: Option<String>,
pub name: String,
pub category: String,
pub iso_country: String,
pub iso_region: String,
pub municipality: String,
pub elevation_ft: f32,
longitude: f32,
latitude: f32,
pub has_tower: Option<bool>,
pub has_beacon: Option<bool>,
pub public: bool,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct UpdateAirport {
#[serde(skip_serializing_if = "Option::is_none")]
pub icao: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub iata: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub local: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub category: Option<AirportCategory>,
#[serde(skip_serializing_if = "Option::is_none")]
pub iso_country: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub iso_region: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub municipality: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub elevation_ft: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub longitude: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub latitude: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub has_tower: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub has_beacon: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub runways: Option<Vec<UpdateRunway>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub frequencies: Option<Vec<UpdateFrequency>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub public: Option<bool>,
}
impl Into<AirportRow> for Airport {
fn into(self) -> AirportRow {
AirportRow {
icao: self.icao.clone(),
iata: self.iata.clone(),
local: self.local.clone(),
name: self.name.clone(),
category: self.category.clone().to_string(),
iso_country: self.iso_country.clone(),
iso_region: self.iso_region.clone(),
municipality: self.municipality.clone(),
elevation_ft: self.elevation_ft,
longitude: self.longitude,
latitude: self.latitude,
has_tower: self.has_tower,
has_beacon: self.has_beacon,
public: self.public,
}
}
}
impl From<AirportRow> for Airport {
fn from(airport: AirportRow) -> Self {
Airport {
icao: airport.icao.clone(),
iata: airport.iata.clone(),
local: airport.local.clone(),
name: airport.name.clone(),
category: match AirportCategory::from_str(&airport.category) {
Ok(c) => c,
Err(_) => {
log::error!("Invalid Airport category: {}", airport.category);
AirportCategory::Unknown
}
},
iso_country: airport.iso_country.clone(),
iso_region: airport.iso_region.clone(),
municipality: airport.municipality.clone(),
elevation_ft: airport.elevation_ft,
longitude: airport.longitude,
latitude: airport.latitude,
has_tower: airport.has_tower,
has_beacon: airport.has_beacon,
runways: vec![],
frequencies: vec![],
public: airport.public,
}
}
}
impl Airport {
pub async fn select(icao: &str) -> Option<Self> {
let pool = db::pool();
let airport: Option<AirportRow> = sqlx::query_as(&format!(
r#"
SELECT * FROM {} WHERE icao = $1
"#,
TABLE_NAME
))
.bind(icao)
.fetch_optional(pool)
.await
.unwrap_or_else(|err| {
log::error!("Unable to find airport '{}'", icao);
None
});
match airport {
Some(a) => Some(a.into()),
None => None,
}
}
pub async fn select_all() -> ApiResult<Vec<Self>> {
let pool = db::pool();
let airports: Vec<AirportRow> = sqlx::query_as(&format!(
r#"
SELECT * FROM {}
"#,
TABLE_NAME
))
.fetch_all(pool)
.await?;
Ok(airports.into_iter().map(From::from).collect())
}
pub async fn insert(&self) -> ApiResult<Self> {
let pool = db::pool();
let airport: AirportRow = sqlx::query_as(&format!(
r#"
INSERT INTO {} (
icao, iata, local, name, category, iso_country, iso_region, municipality,
elevation_ft, longitude, latitude, has_tower, has_beacon, public
)
VALUES (
$1, $2, $3, $4, $5, $6, $7,
$8, $9, $10, $11, $12, $13, $14
)
RETURNING *
"#,
TABLE_NAME,
))
.bind(self.icao.to_string())
.bind(&self.iata)
.bind(&self.local)
.bind(self.name.to_string())
.bind(self.category.to_string())
.bind(self.iso_country.to_string())
.bind(self.iso_region.to_string())
.bind(self.municipality.to_string())
.bind(self.elevation_ft)
.bind(self.longitude)
.bind(self.latitude)
.bind(self.has_tower)
.bind(self.has_beacon)
.bind(self.public)
.fetch_one(pool)
.await?;
Ok(airport.into())
}
pub async fn insert_all(airports: Vec<Self>) -> ApiResult<()> {
let pool = db::pool();
let airport_rows: Vec<AirportRow> = airports.into_iter().map(Into::into).collect();
// Define the maximum size of a single insertion batch.
let chunk_size = 1000;
for chunk in airport_rows.chunks(chunk_size) {
// Build a dynamic query for batch insertion.
let mut query_builder: QueryBuilder<Postgres> = QueryBuilder::new(
"INSERT INTO airports (icao, iata, local, name, category, \
iso_country, iso_region, municipality, elevation_ft, \
longitude, latitude, has_tower, has_beacon, public) ",
);
query_builder.push_values(chunk, |mut b, row| {
b.push_bind(&row.icao)
.push_bind(&row.iata)
.push_bind(&row.local)
.push_bind(&row.name)
.push_bind(&row.category)
.push_bind(&row.iso_country)
.push_bind(&row.iso_region)
.push_bind(&row.municipality)
.push_bind(row.elevation_ft)
.push_bind(row.longitude)
.push_bind(row.latitude)
.push_bind(row.has_tower)
.push_bind(row.has_beacon)
.push_bind(row.public);
});
let query = query_builder.build();
query.execute(pool).await?;
}
Ok(())
}
// TODO
pub async fn update(icao: &str, airport: &UpdateAirport) -> ApiResult<()> {
Ok(())
}
pub async fn delete(icao: &str) -> ApiResult<()> {
let pool = db::pool();
sqlx::query(&format!(
r#"
DELETE FROM {} WHERE icao = $1
"#,
TABLE_NAME
))
.bind(icao.to_string())
.execute(pool)
.await?;
Ok(())
}
pub async fn delete_all() -> ApiResult<()> {
let pool = db::pool();
sqlx::query(&format!(
r#"
DELETE FROM {} WHERE true
"#,
TABLE_NAME
))
.execute(pool)
.await?;
Ok(())
}
}

View File

@@ -0,0 +1,54 @@
use std::fmt::Display;
use std::str::FromStr;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AirportCategory {
#[serde(rename = "small_airport")]
Small,
#[serde(rename = "medium_airport")]
Medium,
#[serde(rename = "large_airport")]
Large,
#[serde(rename = "heliport")]
Heliport,
#[serde(rename = "closed")]
Closed,
#[serde(rename = "seaplane_base")]
Seaplane,
#[serde(rename = "balloon_port")]
BalloonPort,
#[serde(rename = "unknown")]
Unknown,
}
impl FromStr for AirportCategory {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"small_airport" => Ok(AirportCategory::Small),
"medium_airport" => Ok(AirportCategory::Medium),
"large_airport" => Ok(AirportCategory::Large),
"heliport" => Ok(AirportCategory::Heliport),
"closed" => Ok(AirportCategory::Closed),
"seaplane_base" => Ok(AirportCategory::Seaplane),
"balloon_port" => Ok(AirportCategory::BalloonPort),
_ => Ok(AirportCategory::Unknown),
}
}
}
impl Display for AirportCategory {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AirportCategory::Small => write!(f, "small_airport"),
AirportCategory::Medium => write!(f, "medium_airport"),
AirportCategory::Large => write!(f, "large_airport"),
AirportCategory::Heliport => write!(f, "heliport"),
AirportCategory::Closed => write!(f, "closed"),
AirportCategory::Seaplane => write!(f, "seaplane_base"),
AirportCategory::BalloonPort => write!(f, "balloon_port"),
AirportCategory::Unknown => write!(f, "unknown"),
}
}
}

View File

@@ -0,0 +1,15 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct Frequency {
pub id: String,
pub frequency_mhz: f32,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct UpdateFrequency {
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub frequency_mhz: Option<f32>,
}

View File

@@ -0,0 +1,9 @@
mod airport;
mod airport_category;
mod frequency;
mod runway;
pub use airport::*;
pub use airport_category::*;
pub use frequency::*;
pub use runway::*;

View File

@@ -0,0 +1,21 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct Runway {
pub id: String,
pub length_ft: f32,
pub width_ft: f32,
pub surface: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct UpdateRunway {
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub length_ft: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub width_ft: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub surface: Option<String>,
}

View File

@@ -2,15 +2,15 @@ use std::str::FromStr;
use futures_util::stream::StreamExt as _;
use crate::{
airports::{QueryAirport, QueryFilters, QueryOrderField, QueryOrderBy, Airport, AirportCategory},
db::{Response, Metadata},
airports::{Airport, AirportCategory},
db::Paged,
auth::{Auth, verify_role},
};
use actix_multipart::Multipart;
use actix_web::{delete, get, post, put, web, HttpResponse, HttpRequest, ResponseError};
use log::{error, warn};
use postgis_diesel::types::{Polygon, Point};
use serde::{Serialize, Deserialize};
use crate::airports::UpdateAirport;
use crate::users::ADMIN_ROLE;
#[derive(Debug, Serialize, Deserialize)]
struct AirportsQuery {
@@ -27,7 +27,7 @@ struct AirportsQuery {
#[post("/import")]
async fn import_airports(mut payload: Multipart, auth: Auth) -> HttpResponse {
if let Err(err) = verify_role(&auth, "admin") {
if let Err(err) = verify_role(&auth, ADMIN_ROLE) {
return ResponseError::error_response(&err);
};
@@ -43,7 +43,7 @@ async fn import_airports(mut payload: Multipart, auth: Auth) -> HttpResponse {
let data = match chunk {
Ok(data) => data,
Err(err) => {
error!("Failed to get chunk: {}", err);
log::error!("Failed to get chunk: {}", err);
return ResponseError::error_response(&err);
}
};
@@ -54,14 +54,12 @@ async fn import_airports(mut payload: Multipart, auth: Auth) -> HttpResponse {
let airports: Vec<Airport> = match serde_json::from_slice(&bytes) {
Ok(a) => a,
Err(err) => {
error!("Failed to parse JSON: {}", err);
log::error!("Failed to parse JSON: {}", err);
return ResponseError::error_response(&err);
}
};
// Convert Vec<Airport> to Vec<QueryAirport> and insert into database
let query_airports: Vec<QueryAirport> = airports.into_iter().map(|a| a.into()).collect();
match QueryAirport::insert_all(query_airports) {
match Airport::insert_all(airports).await {
Ok(_) => {}
Err(err) => return ResponseError::error_response(&err),
};
@@ -71,220 +69,83 @@ async fn import_airports(mut payload: Multipart, auth: Auth) -> HttpResponse {
#[get("")]
async fn get_airports(req: HttpRequest) -> HttpResponse {
let params = web::Query::<AirportsQuery>::from_query(req.query_string()).unwrap();
let mut filters = QueryFilters::default();
filters.icaos = match &params.icaos {
Some(i) => Some(i.split(",").map(|s| s.to_string()).collect()),
None => None,
};
filters.name = params.name.clone();
filters.categories = match &params.categories {
Some(c) => Some(
c.split(",")
.map(|s| AirportCategory::from_str(s).unwrap())
.collect(),
),
None => None,
};
filters.bounds = match &params.bounds {
Some(b) => {
let bounds: Vec<&str> = b.split(",").collect();
if bounds.len() != 4 {
warn!("Expected 4 bounds, received {}: {}", bounds.len(), b);
return HttpResponse::UnprocessableEntity().body(format!(
"Received {}; expected NE_LAT,NE_LON,SW_LAT,SW_LON",
b
));
}
let ne_lat = match bounds[0].parse::<f64>() {
Ok(b) => b,
Err(err) => {
warn!("{}", err);
return HttpResponse::UnprocessableEntity().body(format!("{}", err));
}
};
let ne_lon = match bounds[1].parse::<f64>() {
Ok(b) => b,
Err(err) => {
warn!("{}", err);
return HttpResponse::UnprocessableEntity().body(format!("{}", err));
}
};
let sw_lat = match bounds[2].parse::<f64>() {
Ok(b) => b,
Err(err) => {
warn!("{}", err);
return HttpResponse::UnprocessableEntity().body(format!("{}", err));
}
};
let sw_lon = match bounds[3].parse::<f64>() {
Ok(b) => b,
Err(err) => {
warn!("{}", err);
return HttpResponse::UnprocessableEntity().body(format!("{}", err));
}
};
let mut polygon: Polygon<Point> = Polygon::new(Some(4326));
polygon.add_point(Point {
x: sw_lon,
y: sw_lat,
srid: Some(4326),
});
polygon.add_point(Point {
x: ne_lon,
y: sw_lat,
srid: Some(4326),
});
polygon.add_point(Point {
x: ne_lon,
y: ne_lat,
srid: Some(4326),
});
polygon.add_point(Point {
x: sw_lon,
y: ne_lat,
srid: Some(4326),
});
polygon.add_point(Point {
x: sw_lon,
y: sw_lat,
srid: Some(4326),
});
Some(polygon)
}
None => None,
};
filters.order_by = match &params.order_by {
Some(o) => Some(QueryOrderBy::from_str(&o).unwrap()),
None => None,
};
filters.order_field = match &params.order_field {
Some(o) => Some(QueryOrderField::from_str(&o).unwrap()),
None => None,
};
filters.has_metar = match &params.has_metar {
Some(h) => Some(h.parse::<bool>().unwrap()),
None => None,
};
let limit = match params.limit {
Some(l) => l,
None => 100,
};
let page = match params.page {
Some(p) => p,
None => 1,
};
let total = match QueryAirport::get_count(&filters) {
Ok(t) => t,
Err(_) => 0,
};
match web::block(move || QueryAirport::get_all(&filters, limit, page))
.await
.unwrap()
{
Ok(a) => {
// Convert Vec<QueryAirport> to Vec<Airport>
let mut airports: Vec<Airport> = vec![];
for airport in a {
airports.push(airport.into());
}
HttpResponse::Ok().json(Response {
data: airports,
meta: Some(Metadata { page, limit, total }),
})
}
match Airport::select_all().await {
Ok(airports) => HttpResponse::Ok().json(airports),
Err(err) => {
error!("{}", err);
err.to_http_response()
log::error!("{}", err);
ResponseError::error_response(&err)
}
}
}
#[get("/{icao}")]
async fn get_airport(icao: web::Path<String>) -> HttpResponse {
match QueryAirport::get(&icao.into_inner()) {
Ok(a) => {
let airport: Airport = a.into();
HttpResponse::Ok().json(airport)
}
Err(err) => {
error!("{}", err);
err.to_http_response()
}
match Airport::select(&icao.into_inner()).await {
Some(airport) => HttpResponse::Ok().json(airport),
None => HttpResponse::NotFound().finish(),
}
}
#[post("")]
async fn create_airport(airport: web::Json<Airport>, auth: Auth) -> HttpResponse {
let _ = match verify_role(&auth, "admin") {
async fn insert_airport(airport: web::Json<Airport>, auth: Auth) -> HttpResponse {
let _ = match verify_role(&auth, ADMIN_ROLE) {
Ok(_) => {}
Err(err) => return ResponseError::error_response(&err),
};
let query_airport: QueryAirport = airport.into_inner().into();
match QueryAirport::insert(query_airport) {
Ok(a) => {
let airport: Airport = a.into();
HttpResponse::Ok().json(airport)
}
match airport.insert().await {
Ok(a) => HttpResponse::Ok().json(a),
Err(err) => {
error!("{}", err);
err.to_http_response()
log::error!("{}", err);
ResponseError::error_response(&err)
}
}
}
#[put("/{icao}")]
async fn update_airport(
_icao: web::Path<String>,
airport: web::Json<Airport>,
icao: web::Path<String>,
airport: web::Json<UpdateAirport>,
auth: Auth,
) -> HttpResponse {
let _ = match verify_role(&auth, "admin") {
let _ = match verify_role(&auth, ADMIN_ROLE) {
Ok(_) => {}
Err(err) => return ResponseError::error_response(&err),
};
let query_airport: QueryAirport = airport.into_inner().into();
match QueryAirport::update(query_airport) {
Ok(a) => {
let airport: Airport = a.into();
HttpResponse::Ok().json(airport)
}
match Airport::update(&icao.into_inner(), &airport.into_inner()).await {
Ok(a) => HttpResponse::Ok().json(a),
Err(err) => {
error!("{}", err);
err.to_http_response()
log::error!("{}", err);
ResponseError::error_response(&err)
}
}
}
#[delete("")]
async fn delete_airports(auth: Auth) -> HttpResponse {
let _ = match verify_role(&auth, "admin") {
let _ = match verify_role(&auth, ADMIN_ROLE) {
Ok(_) => {}
Err(err) => return ResponseError::error_response(&err),
};
match QueryAirport::delete(None) {
match Airport::delete_all().await {
Ok(_) => HttpResponse::NoContent().finish(),
Err(err) => {
error!("{}", err);
err.to_http_response()
log::error!("{}", err);
ResponseError::error_response(&err)
}
}
}
#[delete("/{icao}")]
async fn delete_airport(icao: web::Path<String>, auth: Auth) -> HttpResponse {
let _ = match verify_role(&auth, "admin") {
let _ = match verify_role(&auth, ADMIN_ROLE) {
Ok(_) => {}
Err(err) => return ResponseError::error_response(&err),
};
match QueryAirport::delete(Some(icao.into_inner())) {
match Airport::delete(&icao.into_inner()).await {
Ok(_) => HttpResponse::NoContent().finish(),
Err(err) => {
error!("{}", err);
err.to_http_response()
log::error!("{}", err);
ResponseError::error_response(&err)
}
}
}
@@ -295,7 +156,7 @@ pub fn init_routes(config: &mut web::ServiceConfig) {
.service(import_airports)
.service(get_airports)
.service(get_airport)
.service(create_airport)
.service(insert_airport)
.service(update_airport)
.service(delete_airports)
.service(delete_airport),

View File

@@ -4,6 +4,7 @@ use argon2::{
};
use rand::prelude::*;
use rand_chacha::ChaCha20Rng;
use serde::{Deserialize, Serialize};
mod model;
mod routes;
@@ -13,11 +14,9 @@ pub use model::*;
pub use session::*;
pub use routes::init_routes;
use crate::error::{ApiError, ApiResult};
use crate::error::{Error, ApiResult};
pub const SESSION_COOKIE_NAME: &str = "session";
pub fn csprng_128bit(take: usize) -> String {
pub fn csprng(take: usize) -> String {
// Generate a CSPRNG 128-bit (16 byte) ID using alphanumeric characters (a-z, A-Z, 0-9)
let rng = ChaCha20Rng::from_entropy();
rng
@@ -27,32 +26,51 @@ pub fn csprng_128bit(take: usize) -> String {
.collect()
}
pub fn hash(str: &str) -> ApiResult<String> {
pub fn hash(string: &str) -> ApiResult<String> {
let salt = SaltString::generate(&mut OsRng);
let bytes = str.as_bytes();
let hash = Argon2::default().hash_password(bytes, &salt)?.to_string();
let hash = Argon2::default()
.hash_password(string.as_bytes(), &salt)?
.to_string();
Ok(hash)
}
pub fn verify_hash(str: &str, hash: &str) -> bool {
let bytes = str.as_bytes();
let parsed_hash = match PasswordHash::new(hash) {
pub fn verify_hash(string: &str, hashed_string: &str) -> bool {
let bytes = string.as_bytes();
let parsed_hash = match PasswordHash::new(hashed_string) {
Ok(h) => h,
Err(_) => return false,
Err(err) => {
log::error!(
"Failed to construct PasswordHash from '{}': {}",
hashed_string,
err
);
return false;
}
};
match Argon2::default().verify_password(bytes, &parsed_hash) {
Ok(_) => true,
Err(_) => false,
}
Argon2::default()
.verify_password(bytes, &parsed_hash)
.is_ok()
}
pub fn verify_role(auth: &Auth, role: &str) -> ApiResult<()> {
if auth.user.role == role {
Ok(())
} else {
Err(ApiError {
Err(Error {
status: 403,
message: "User does not have permission to perform this action.".to_string(),
details: "User does not have permission to perform this action.".to_string(),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_hash() {
let password = hash("password").unwrap();
assert!(!verify_hash(&password, "bad_password"));
assert!(verify_hash("password", &password));
}
}

View File

@@ -3,17 +3,14 @@ use std::pin::Pin;
use actix_web::{FromRequest, Error as ActixError, HttpRequest, dev::Payload, http};
use serde::{Serialize, Deserialize};
use crate::{
error::ApiError,
users::{User, UserResponse},
};
use crate::{error::Error, users::User};
use super::{Session, SESSION_COOKIE_NAME};
#[derive(Debug, Serialize, Deserialize)]
pub struct Auth {
pub session_id: Option<String>,
pub user: UserResponse,
pub api_key: Option<String>,
pub user: User,
}
impl FromRequest for Auth {
@@ -21,7 +18,34 @@ impl FromRequest for Auth {
type Future = Pin<Box<dyn Future<Output = Result<Self, Self::Error>>>>;
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
// Get session ID from request
// Check for API key
match req
.headers()
.get(http::header::AUTHORIZATION)
.map(|h| h.to_str().unwrap().split_at(7).1.to_string())
{
Some(key_id) => {
let fut = async move {
// Check if the Session API key exists
let api_key = match Session::get(&key_id).await? {
Some(session) => session,
None => return Err(Error::new(401, "API Key does not exist".to_string()).into()),
};
match User::select(&api_key.email).await {
Some(user) => Ok(Auth {
session_id: None,
api_key: Some(key_id),
user,
}),
None => Err(Error::new(404, format!("User {} not found", api_key.email)).into()),
}
};
return Box::pin(fut);
}
None => {}
};
// Check for session
let session_id = match req
.cookie(SESSION_COOKIE_NAME)
.map(|c| c.value().to_string())
@@ -35,9 +59,9 @@ impl FromRequest for Auth {
None => {
let fut = async {
Err(
ApiError {
Error {
status: 401,
message: "No session ID found in the request".to_string(),
details: "No session ID found in the request".to_string(),
}
.into(),
)
@@ -52,12 +76,13 @@ impl FromRequest for Auth {
// Verify the session
let fut = async move {
match Session::verify(&session_id, &ip_address).await {
Ok(session) => match User::get_by_email(&session.email) {
Ok(user) => Ok(Auth {
Ok(session) => match User::select(&session.email).await {
Some(user) => Ok(Auth {
session_id: Some(session_id),
user: user.into(),
api_key: None,
user,
}),
Err(err) => Err(err.into()),
None => Err(Error::new(404, format!("User {} not found", session.email)).into()),
},
Err(err) => Err(err.into()),
}

View File

@@ -5,11 +5,11 @@ use actix_web::{
};
use crate::{
auth::{verify_hash, Session, SESSION_COOKIE_NAME},
error::ApiError,
error::Error,
users::{LoginRequest, RegisterRequest, User, UserResponse},
};
use crate::auth::Auth;
use crate::auth::{Auth, DEFAULT_SESSION_TTL};
#[post("/register")]
async fn register(user: web::Json<RegisterRequest>) -> HttpResponse {
@@ -18,17 +18,18 @@ async fn register(user: web::Json<RegisterRequest>) -> HttpResponse {
Ok(user) => user,
Err(err) => return ResponseError::error_response(&err),
};
match User::insert(insert_user) {
match insert_user.insert().await {
Ok(user) => {
let response: UserResponse = user.into();
log::trace!("Registered user '{}'", response.email);
HttpResponse::Created().json(response)
},
}
Err(err) => {
// Obfuscate the service error message to prevent leaking database details
if err.status == 409 {
return HttpResponse::Conflict().finish();
HttpResponse::Conflict().finish()
} else {
return ResponseError::error_response(&err);
ResponseError::error_response(&err)
}
}
}
@@ -36,29 +37,27 @@ async fn register(user: web::Json<RegisterRequest>) -> HttpResponse {
#[post("/login")]
async fn login(request: web::Json<LoginRequest>, req: HttpRequest) -> HttpResponse {
let email = request.email.clone();
let email = &request.email;
let ip_address = req.peer_addr().unwrap().ip().to_string();
let query_user = match User::get_by_email(&email) {
Ok(query_user) => query_user,
Err(err) => {
log::error!("{}", err);
return ResponseError::error_response(&err);
}
let query_user = match User::select(&email).await {
Some(query_user) => query_user,
None => return HttpResponse::Unauthorized().finish(),
};
if verify_hash(&query_user.hash, &request.password) {
if verify_hash(&request.password, &query_user.password_hash) {
// Create a session
let session = Session::new(&email, &ip_address);
let session_cookie = session.cookie();
let session = Session::new(64, &email, &ip_address, Some(DEFAULT_SESSION_TTL));
let session_cookie = session.to_cookie();
// Save the session to the database
if let Err(err) = session.store().await {
log::error!("Failed to store session");
return ResponseError::error_response(&ApiError::new(500, err.to_string()));
return ResponseError::error_response(&Error::new(500, err.to_string()));
}
return HttpResponse::Ok().cookie(session_cookie).finish();
HttpResponse::Ok().cookie(session_cookie).finish()
} else {
log::error!("Invalid login attempt for {}", email);
return HttpResponse::Unauthorized().finish();
HttpResponse::Unauthorized().finish()
}
}
@@ -70,11 +69,11 @@ async fn logout(req: HttpRequest, _auth: Auth) -> HttpResponse {
let session_id = cookie.value().to_string();
if let Err(err) = Session::delete(&session_id).await {
log::error!("Failed to delete session");
return ResponseError::error_response(&ApiError::new(500, err.to_string()));
return ResponseError::error_response(&Error::new(500, err.to_string()));
}
}
None => {
return ResponseError::error_response(&ApiError::new(400, "Invalid session".to_string()));
return ResponseError::error_response(&Error::new(400, "Invalid session".to_string()));
}
}
@@ -88,11 +87,21 @@ async fn logout(req: HttpRequest, _auth: Auth) -> HttpResponse {
HttpResponse::Ok().cookie(session_cookie).finish()
}
#[post("/key")]
async fn create_api_key(req: HttpRequest, auth: Auth) -> HttpResponse {
let ip_address = req.peer_addr().unwrap().ip().to_string();
let api_key = Session::new(128, &auth.user.email, &ip_address, None);
// TODO: store api key
HttpResponse::Ok().body(api_key.session_id)
}
pub fn init_routes(config: &mut web::ServiceConfig) {
config.service(
web::scope("auth")
.service(register)
.service(login)
.service(logout),
.service(logout)
.service(create_api_key),
);
}

View File

@@ -5,10 +5,10 @@ use redis::{AsyncCommands, RedisResult};
use crate::{
db::redis_async_connection,
error::{ApiError, ApiResult},
error::{Error, ApiResult},
};
use super::{csprng_128bit, hash, verify_hash};
use super::{csprng, hash, verify_hash};
pub const DEFAULT_SESSION_TTL: i64 = 86400; // (In seconds) 24 hours
pub const SESSION_COOKIE_NAME: &str = "session";
@@ -18,17 +18,21 @@ pub struct Session {
pub session_id: String,
pub email: String,
pub ip_address: String,
pub expires_at: DateTime<Utc>,
#[serde(skip_serializing_if = "Option::is_none")]
pub expires_at: Option<DateTime<Utc>>,
}
impl Session {
pub fn new(email: &str, ip_address: &str) -> Self {
let now = chrono::Utc::now();
pub fn new(take: usize, email: &str, ip_address: &str, ttl: Option<i64>) -> Self {
let now = Utc::now();
Self {
session_id: csprng_128bit(32),
session_id: csprng(take),
email: email.to_string(),
ip_address: hash(&ip_address).unwrap(),
expires_at: now + chrono::Duration::seconds(DEFAULT_SESSION_TTL),
expires_at: match ttl {
Some(ttl) => Some(now + chrono::Duration::seconds(ttl)),
None => None,
},
}
}
@@ -36,7 +40,13 @@ impl Session {
let mut conn = redis_async_connection().await?;
let key = self.session_id.clone();
let value = serde_json::to_string(self)?;
let result: RedisResult<()> = conn.set_ex(key, &value, DEFAULT_SESSION_TTL as u64).await;
let result: RedisResult<()> = match self.expires_at {
Some(expires_at) => {
let ttl = expires_at.timestamp() - Utc::now().timestamp();
conn.set_ex(key, &value, ttl as u64).await
}
None => conn.set(key, value).await,
};
match result {
Ok(_) => Ok(()),
Err(err) => Err(err.into()),
@@ -66,26 +76,29 @@ impl Session {
// Check if the session exists
let session = match Self::get(session_id).await? {
Some(session) => session,
None => return Err(ApiError::new(401, "Session does not exist".to_string())),
None => return Err(Error::new(401, "Session does not exist".to_string())),
};
// Check if the IP Address matches the Session's IP Address
if verify_hash(ip_address, &session.ip_address) {
return Ok(session);
Ok(session)
} else {
return Err(ApiError::new(
401,
"IP Address does not match".to_string(),
));
Err(Error::new(401, "IP Address does not match".to_string()))
}
}
pub fn cookie(&self) -> Cookie {
pub fn to_cookie(&self) -> Cookie {
let expires_at = match self.expires_at {
Some(expires_at) => expires_at.timestamp(),
None => DEFAULT_SESSION_TTL,
};
let ttl = expires_at - Utc::now().timestamp();
Cookie::build(SESSION_COOKIE_NAME, self.session_id.clone())
.path("/")
.max_age(Duration::seconds(DEFAULT_SESSION_TTL))
.secure(true)
.http_only(true)
.max_age(Duration::seconds(ttl))
// TODO: enable secure and http_only
// .secure(true)
// .http_only(true)
.finish()
}
}

View File

@@ -1,112 +1,129 @@
use crate::error::{ApiError, ApiResult};
use diesel::{r2d2::ConnectionManager, PgConnection};
use redis::{Client as RedisClient, aio::MultiplexedConnection as RedisConnection};
use crate::error::ApiResult;
use redis::{Client as RedisClient, aio::MultiplexedConnection as RedisConnection, RedisResult};
use s3::{
Bucket, Region, creds::Credentials, BucketConfiguration, request::ResponseData,
bucket_ops::CreateBucketResponse,
};
use serde::{Deserialize, Serialize};
use crate::diesel_migrations::MigrationHarness;
use lazy_static::lazy_static;
use log::{error, info, warn};
use r2d2;
use std::env;
use std::sync::OnceLock;
use std::time::Duration;
use sqlx::{Pool, Postgres};
use sqlx::postgres::PgPoolOptions;
pub mod schema;
static POOL: OnceLock<Pool<Postgres>> = OnceLock::new();
static REDIS: OnceLock<RedisClient> = OnceLock::new();
static BUCKET: OnceLock<Bucket> = OnceLock::new();
type Pool = r2d2::Pool<ConnectionManager<PgConnection>>;
pub type DbConnection = r2d2::PooledConnection<ConnectionManager<PgConnection>>;
pub async fn initialize() -> ApiResult<()> {
log::info!("Initializing database...");
let db_user = std::env::var("POSTGRES_USER").unwrap_or("siren".to_string());
let db_password = std::env::var("POSTGRES_PASSWORD").expect("POSTGRES_PASSWORD must be set");
let db_host: String = std::env::var("POSTGRES_HOST").expect("POSTGRES_HOST must be set");
let db_port = std::env::var("POSTGRES_PORT").unwrap_or("5432".to_string());
let db_name = std::env::var("POSTGRES_NAME").unwrap_or("siren".to_string());
pub const MIGRATIONS: diesel_migrations::EmbeddedMigrations = embed_migrations!();
lazy_static! {
static ref POOL: Pool = {
let username = env::var("DATABASE_USER").expect("Database username is not set");
let password = env::var("DATABASE_PASSWORD").expect("Database password is not set");
let host = env::var("DATABASE_HOST").unwrap_or("localhost".to_string());
let name = env::var("DATABASE_NAME").expect("Database name is not set");
let port = env::var("DATABASE_PORT").unwrap_or("5432".to_string());
let url = format!(
// Setup Postgres pool connection
let pool = PgPoolOptions::new()
.max_connections(5)
.acquire_timeout(Duration::from_secs(30))
.connect(&format!(
"postgres://{}:{}@{}:{}/{}",
username, password, host, port, name
);
let manager = ConnectionManager::<PgConnection>::new(url);
Pool::builder()
.test_on_check_out(true)
.build(manager)
.expect("Failed to create db pool")
};
static ref REDIS: RedisClient = {
let host = env::var("REDIS_HOST").unwrap_or("localhost".to_string());
let port = env::var("REDIS_PORT").unwrap_or("6379".to_string());
db_user, db_password, db_host, db_port, db_name
))
.await?;
match POOL.set(pool) {
Ok(_) => {}
Err(_) => {
log::warn!("Database pool already initialized");
}
}
// Setup Redis connection
let redis = {
let host = std::env::var("REDIS_HOST").unwrap_or("localhost".to_string());
let port = std::env::var("REDIS_PORT").unwrap_or("6379".to_string());
let url = format!("redis://{}:{}", host, port);
RedisClient::open(url).expect("Failed to create redis client")
};
static ref BUCKET: Bucket = {
let url = env::var("MINIO_HOST").unwrap_or("localhost".to_string());
let port = env::var("MINIO_PORT").unwrap_or("9000".to_string());
let user = env::var("MINIO_ROOT_USER").expect("MINIO_ROOT_USER is not set");
let password = env::var("MINIO_ROOT_PASSWORD").expect("MINIO_ROOT_PASSWORD is not set");
let base_url = format!("http://{}:{}", url, port);
match REDIS.set(redis) {
Ok(_) => {}
Err(_) => {
log::warn!("Redis client already initialized");
}
}
let region = Region::Custom {
region: "".to_string(),
endpoint: base_url,
};
let schema = std::env::var("MINIO_SCHEMA").unwrap_or("http".to_string());
let url = std::env::var("MINIO_HOST").unwrap_or("localhost".to_string());
let port = std::env::var("MINIO_PORT").unwrap_or("9000".to_string());
let user = std::env::var("MINIO_ROOT_USER").expect("MINIO_ROOT_USER is not set");
let password = std::env::var("MINIO_ROOT_PASSWORD").expect("MINIO_ROOT_PASSWORD is not set");
let base_url = format!("{}://{}:{}", schema, url, port);
let credentials = Credentials {
access_key: Some(user),
secret_key: Some(password),
security_token: None,
session_token: None,
expiration: None,
};
*Bucket::new("aviation", region.clone(), credentials.clone())
.expect("Failed to create S3 Bucket")
.with_path_style()
let region = Region::Custom {
region: "".to_string(),
endpoint: base_url,
};
let credentials = Credentials {
access_key: Some(user),
secret_key: Some(password),
security_token: None,
session_token: None,
expiration: None,
};
let bucket = Bucket::new("aviation", region.clone(), credentials.clone())
.expect("Failed to create S3 Bucket")
.with_path_style();
match BUCKET.set(*bucket) {
Ok(_) => {}
Err(_) => {
log::warn!("Bucket client already initialized");
}
}
// Run migrations
match run_migrations().await {
Ok(_) => log::debug!("Successfully ran migrations"),
Err(e) => log::error!("Failed to run migrations: {}", e),
}
log::info!("Database initialized");
Ok(())
}
pub async fn init() {
lazy_static::initialize(&POOL);
lazy_static::initialize(&REDIS);
lazy_static::initialize(&BUCKET);
match create_bucket().await {
Ok(_) => info!("Bucket initialized"),
Err(err) => match err.status {
409 => warn!("Bucket already exists"),
_ => error!("Failed to initialize bucket; {}", err),
},
};
let mut pool: DbConnection = connection().expect("Failed to get db connection");
match pool.run_pending_migrations(MIGRATIONS) {
Ok(_) => info!("Database initialized"),
Err(err) => error!("Failed to initialize database; {}", err),
};
pub fn pool() -> &'static Pool<Postgres> {
POOL.get().unwrap()
}
pub fn connection() -> ApiResult<DbConnection> {
POOL
.get()
.map_err(|e| ApiError::new(500, format!("Failed getting db connection: {}", e)))
fn redis() -> &'static RedisClient {
REDIS.get().unwrap()
}
pub fn redis_connection() -> ApiResult<redis::Connection> {
let conn = REDIS.get_connection()?;
pub fn redis_connection() -> RedisResult<redis::Connection> {
let conn = redis().get_connection()?;
Ok(conn)
}
pub async fn redis_async_connection() -> ApiResult<RedisConnection> {
let conn = REDIS.get_multiplexed_async_connection().await?;
pub async fn redis_async_connection() -> RedisResult<RedisConnection> {
let conn = redis().get_multiplexed_async_connection().await?;
Ok(conn)
}
async fn run_migrations() -> ApiResult<()> {
log::debug!("Running migrations");
let pool = pool();
sqlx::migrate!().run(pool).await?;
Ok(())
}
async fn create_bucket() -> ApiResult<CreateBucketResponse> {
let url = env::var("MINIO_URL").unwrap_or("localhost".to_string());
let port = env::var("MINIO_PORT").unwrap_or("9000".to_string());
let user = env::var("MINIO_ROOT_USER").expect("MINIO_ROOT_USER is not set");
let password = env::var("MINIO_ROOT_PASSWORD").expect("MINIO_ROOT_PASSWORD is not set");
let url = std::env::var("MINIO_URL").unwrap_or("localhost".to_string());
let port = std::env::var("MINIO_PORT").unwrap_or("9000".to_string());
let user = std::env::var("MINIO_ROOT_USER").expect("MINIO_ROOT_USER is not set");
let password = std::env::var("MINIO_ROOT_PASSWORD").expect("MINIO_ROOT_PASSWORD is not set");
let base_url = format!("http://{}:{}", url, port);
let region = Region::Custom {
@@ -133,29 +150,24 @@ async fn create_bucket() -> ApiResult<CreateBucketResponse> {
}
pub async fn upload_file(path: &str, content: &[u8]) -> ApiResult<ResponseData> {
let response = BUCKET.put_object(path, content).await?;
let response = BUCKET.get().unwrap().put_object(path, content).await?;
Ok(response)
}
pub async fn get_file(path: &str) -> ApiResult<Vec<u8>> {
let response = BUCKET.get_object(path).await?;
let response = BUCKET.get().unwrap().get_object(path).await?;
let bytes = response.bytes();
Ok(bytes.to_vec())
}
pub async fn delete_file(path: &str) -> ApiResult<ResponseData> {
let response = BUCKET.delete_object(path).await?;
let response = BUCKET.get().unwrap().delete_object(path).await?;
Ok(response)
}
#[derive(Serialize, Deserialize)]
pub struct Response<T> {
pub struct Paged<T> {
pub data: T,
pub meta: Option<Metadata>,
}
#[derive(Serialize, Deserialize)]
pub struct Metadata {
pub page: i32,
pub limit: i32,
pub total: i64,

View File

@@ -1,41 +0,0 @@
diesel::table! {
use diesel::sql_types::*;
use postgis_diesel::sql_types::*;
airports (icao) {
icao -> Text,
category -> Text,
name -> Text,
elevation_ft -> Float,
iso_country -> Text,
iso_region -> Text,
municipality -> Text,
has_metar -> Bool,
point -> Geometry,
data -> Jsonb
}
}
diesel::table! {
metars (id) {
id -> Integer,
icao -> Text,
observation_time -> Timestamp,
raw_text -> Text,
data -> Jsonb,
}
}
diesel::table! {
users (email) {
email -> Text,
hash -> Text,
role -> Text,
first_name -> Text,
last_name -> Text,
updated_at -> Timestamp,
created_at -> Timestamp,
profile_picture -> Nullable<Text>,
favorites -> Array<Text>,
verified -> Bool,
}
}

View File

@@ -1,50 +1,86 @@
use actix_web::http::StatusCode;
use actix_web::{HttpResponse, ResponseError};
use diesel::result::Error as DieselError;
use log::warn;
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::fmt;
pub type ApiResult<T> = Result<T, ApiError>;
pub type ApiResult<T> = Result<T, Error>;
#[derive(Debug, Deserialize, Serialize)]
pub struct ApiError {
pub struct Error {
pub status: u16,
pub message: String,
pub details: String,
}
impl ApiError {
impl Error {
pub fn new(status: u16, message: String) -> Self {
Self { status, message }
Self {
status,
details: message,
}
}
pub fn to_http_response(&self) -> HttpResponse {
let status = match StatusCode::from_u16(self.status) {
Ok(s) => s,
Err(err) => {
warn!("{}", err);
StatusCode::INTERNAL_SERVER_ERROR
let status = StatusCode::from_u16(self.status).unwrap_or_else(|err| {
warn!("{}", err);
StatusCode::INTERNAL_SERVER_ERROR
});
HttpResponse::build(status).body(self.details.to_string())
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self.details.as_str())
}
}
impl std::error::Error for Error {
fn description(&self) -> &str {
&self.details
}
}
impl ResponseError for Error {
fn error_response(&self) -> HttpResponse {
let status =
StatusCode::from_u16(self.status).unwrap_or_else(|_| StatusCode::INTERNAL_SERVER_ERROR);
let status_code = status.as_u16();
let details = match status_code {
401 => String::from("Unauthorized"),
code if code < 500 => self.details.clone(),
_ => {
log::error!("Internal server error: {}", self.details);
String::from("Internal Server Error")
}
};
HttpResponse::build(status).body(self.message.to_string())
HttpResponse::build(status).json(json!({ "status": status_code, "details": details }))
}
}
impl fmt::Display for ApiError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self.message.as_str())
}
}
impl From<std::io::Error> for ApiError {
impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Self {
Self::new(500, format!("Unknown IO error: {}", error))
}
}
impl From<std::env::VarError> for ApiError {
impl From<chrono::ParseError> for Error {
fn from(error: chrono::ParseError) -> Self {
Self::new(500, format!("Parse error: {}", error))
}
}
impl From<core::num::ParseIntError> for Error {
fn from(error: core::num::ParseIntError) -> Self {
Self::new(500, format!("Parse error: {}", error))
}
}
impl From<std::env::VarError> for Error {
fn from(error: std::env::VarError) -> Self {
Self::new(
500,
@@ -53,47 +89,31 @@ impl From<std::env::VarError> for ApiError {
}
}
impl From<DieselError> for ApiError {
fn from(error: DieselError) -> Self {
match error {
DieselError::DatabaseError(kind, err) => match kind {
diesel::result::DatabaseErrorKind::UniqueViolation => {
Self::new(409, err.message().to_string())
}
_ => Self::new(500, err.message().to_string()),
},
DieselError::NotFound => Self::new(404, "The record was not found".to_string()),
DieselError::SerializationError(err) => Self::new(422, err.to_string()),
err => Self::new(500, format!("Unknown Diesel error: {}", err)),
}
}
}
impl From<reqwest::Error> for ApiError {
impl From<reqwest::Error> for Error {
fn from(error: reqwest::Error) -> Self {
Self::new(500, format!("Unknown reqwest error: {}", error))
}
}
impl From<serde_json::Error> for ApiError {
impl From<serde_json::Error> for Error {
fn from(error: serde_json::Error) -> Self {
Self::new(500, format!("Unknown serde_json error: {}", error))
}
}
impl From<argon2::password_hash::Error> for ApiError {
impl From<argon2::password_hash::Error> for Error {
fn from(error: argon2::password_hash::Error) -> Self {
Self::new(500, format!("Unknown argon2 error: {}", error))
}
}
impl From<redis::RedisError> for ApiError {
impl From<redis::RedisError> for Error {
fn from(error: redis::RedisError) -> Self {
Self::new(500, format!("Unknown redis error: {}", error))
}
}
impl From<s3::error::S3Error> for ApiError {
impl From<s3::error::S3Error> for Error {
fn from(error: s3::error::S3Error) -> Self {
match error {
s3::error::S3Error::Credentials(err) => {
@@ -102,9 +122,7 @@ impl From<s3::error::S3Error> for ApiError {
s3::error::S3Error::FromUtf8(err) => {
Self::new(500, format!("Unknown s3 from utf8 error: {}", err))
}
s3::error::S3Error::FmtError(err) => {
Self::new(500, format!("Unknown s3 fmt error: {}", err))
}
s3::error::S3Error::FmtError(err) => Self::new(500, format!("Unknown s3 fmt error: {}", err)),
s3::error::S3Error::HeaderToStr(err) => {
Self::new(500, format!("Unknown s3 header to str error: {}", err))
}
@@ -112,9 +130,7 @@ impl From<s3::error::S3Error> for ApiError {
500,
format!("Unknown s3 hmac invalid length error: {}", err),
),
s3::error::S3Error::Http(error) => {
Self::new(error.status_code().as_u16(), error.to_string())
}
s3::error::S3Error::Http(error) => Self::new(error.status_code().as_u16(), error.to_string()),
_ => {
let re = Regex::new(r"HTTP (\d{3})").unwrap();
// Apply the regex to the input string
@@ -131,18 +147,43 @@ impl From<s3::error::S3Error> for ApiError {
}
}
impl ResponseError for ApiError {
fn error_response(&self) -> HttpResponse {
let status = match StatusCode::from_u16(self.status) {
Ok(status) => status,
Err(_) => StatusCode::INTERNAL_SERVER_ERROR,
};
let message = match status.as_u16() < 500 {
true => self.message.clone(),
false => "Internal server error".to_string(),
};
HttpResponse::build(status).json(json!({ "status": status.as_u16(), "message": message }))
impl From<sqlx::Error> for Error {
fn from(error: sqlx::Error) -> Self {
match error {
sqlx::Error::RowNotFound => Error::new(404, "Not found".to_string()),
sqlx::Error::ColumnIndexOutOfBounds { .. } => Error::new(422, error.to_string()),
sqlx::Error::ColumnNotFound { .. } => Error::new(422, error.to_string()),
sqlx::Error::ColumnDecode { .. } => Error::new(422, error.to_string()),
sqlx::Error::Decode(_) => Error::new(422, error.to_string()),
sqlx::Error::PoolTimedOut => Error::new(503, error.to_string()),
sqlx::Error::PoolClosed => Error::new(503, error.to_string()),
sqlx::Error::Tls(_) => Error::new(500, error.to_string()),
sqlx::Error::Io(_) => Error::new(500, error.to_string()),
sqlx::Error::Protocol(_) => Error::new(500, error.to_string()),
sqlx::Error::Configuration(_) => Error::new(500, error.to_string()),
sqlx::Error::AnyDriverError(_) => Error::new(500, error.to_string()),
sqlx::Error::Database(err) => {
if let Some(code) = err.code() {
match code.trim() {
// Unique violation
"23505" => return Error::new(409, err.to_string()),
_ => (),
}
}
Error::new(500, err.to_string())
}
sqlx::Error::Migrate(_) => Error::new(500, error.to_string()),
sqlx::Error::TypeNotFound { type_name } => {
Error::new(500, format!("Type not found: {}", type_name))
}
sqlx::Error::WorkerCrashed => Error::new(500, error.to_string()),
_ => Error::new(500, error.to_string()),
}
}
}
impl From<sqlx::migrate::MigrateError> for Error {
fn from(error: sqlx::migrate::MigrateError) -> Self {
Error::new(500, error.to_string())
}
}

View File

@@ -1,12 +1,10 @@
extern crate diesel;
#[macro_use]
extern crate diesel_migrations;
use std::env;
use actix_cors::Cors;
use actix_web::{App, HttpServer, middleware::Logger};
use dotenv::dotenv;
use dotenv::from_filename;
use crate::auth::hash;
use crate::users::{User, ADMIN_ROLE};
mod airports;
mod auth;
@@ -17,15 +15,41 @@ mod scheduler;
mod users;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
dotenv().ok();
env_logger::init_from_env(env_logger::Env::default().filter_or("RUST_LOG", "warn,api=info"));
db::init().await;
async fn main() -> Result<(), Box<dyn std::error::Error>> {
initialize_environment()?;
db::initialize().await?;
// scheduler::update_airports();
let host = env::var("API_HOST").unwrap_or("localhost".to_string());
let port = env::var("API_PORT").unwrap_or("5000".to_string());
// Initialize admin user
let admin_username = env::var("ADMIN_USERNAME");
let admin_password = env::var("ADMIN_PASSWORD");
if admin_username.is_ok() && admin_password.is_ok() {
let username = admin_username.unwrap();
if User::select(&username).await.is_none() {
log::debug!("Creating default administrator");
let password = admin_password.unwrap();
let password_hash = hash(&password)?;
let admin_user = User {
email: username,
password_hash,
role: ADMIN_ROLE.to_string(),
first_name: "Admin".to_string(),
last_name: "".to_string(),
updated_at: Default::default(),
created_at: Default::default(),
};
match admin_user.insert().await {
Ok(_) => log::debug!("Default administrator was successfully created"),
Err(err) => {
log::warn!("{}", err);
}
};
}
}
let server = match HttpServer::new(move || {
let cors = Cors::default()
.allow_any_origin()
@@ -49,9 +73,35 @@ async fn main() -> std::io::Result<()> {
}
Err(err) => {
log::error!("Could not bind server: {}", err);
return Err(err);
return Err(err.into());
}
};
server.run().await
if let Err(err) = server.run().await {
return Err(err.into());
}
Ok(())
}
fn initialize_environment() -> std::io::Result<()> {
// Iterate over files in the current directory
for entry in std::fs::read_dir(".")? {
let entry = entry?;
let path = entry.path();
// Check if the file name starts with ".env" and is a file
if let Some(file_name) = path.file_name().and_then(|n| n.to_str()) {
if file_name.starts_with(".env") && path.is_file() {
// Try to load the file
if let Err(err) = from_filename(&file_name) {
eprintln!("Failed to load {}: {}", file_name, err);
} else {
println!("Loaded: {}", file_name);
}
}
}
}
env_logger::init_from_env(env_logger::Env::default().filter_or("RUST_LOG", "warn,api=info"));
Ok(())
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,45 +1,34 @@
use crate::{error::ApiError, db::Metadata};
use crate::error::Error;
use crate::metars::Metar;
use actix_web::{get, web, HttpResponse, HttpRequest};
use log::error;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct MetarsResponse {
pub data: Vec<Metar>,
pub meta: Metadata,
}
#[derive(Debug, Serialize, Deserialize)]
struct GetAllParameters {
struct FindAllParameters {
icaos: Option<String>,
}
#[get("metars")]
async fn get_all(req: HttpRequest) -> HttpResponse {
let params = web::Query::<GetAllParameters>::from_query(req.query_string()).unwrap();
let icao_option = params.icaos.clone();
async fn find_all(req: HttpRequest) -> HttpResponse {
let parameters = web::Query::<FindAllParameters>::from_query(req.query_string()).unwrap();
let icao_option = &parameters.icaos;
let icao_string = match icao_option {
Some(i) => i,
None => return HttpResponse::UnprocessableEntity().body("Missing icaos parameter"),
};
let icaos: Vec<&str> = icao_string.split(',').collect();
let metars =
match web::block(|| Ok::<_, ApiError>(async { Metar::get_all(icao_string).await }))
.await
.unwrap()
.unwrap()
.await
{
Ok(a) => a,
Err(err) => {
error!("{}", err);
return err.to_http_response();
}
};
let metars = match Metar::find_all(&icaos).await {
Ok(a) => a,
Err(err) => {
error!("{}", err);
return err.to_http_response();
}
};
HttpResponse::Ok().json(metars)
}
pub fn init_routes(config: &mut web::ServiceConfig) {
config.service(get_all);
config.service(find_all);
}

View File

@@ -1,74 +1,74 @@
use tokio::time::{sleep, Duration};
use crate::airports::{QueryAirport, QueryFilters};
// use crate::airports::{AirportDb, AirportFilter};
use crate::metars::Metar;
pub fn update_airports() {
tokio::spawn(async {
let mut airports: Vec<QueryAirport> = vec![];
let limit = 100;
loop {
log::debug!("METAR update start");
let total = match QueryAirport::get_count(&QueryFilters::default()) {
Ok(t) => t,
Err(err) => {
log::warn!("{}", err);
break;
}
};
if total != airports.len() as i64 {
log::debug!("{} cached airports, expected {}", airports.len(), total);
airports = vec![];
let pages = ((total as f32) / (if limit <= 0 { 1 } else { limit } as f32)).ceil() as i32;
for page in 1..(pages + 1) {
match QueryAirport::get_all(&QueryFilters::default(), limit, page) {
Ok(mut a) => airports.append(&mut a),
Err(err) => {
log::warn!("{}", err);
break;
}
}
}
}
log::debug!("Updating {} airport METARS", airports.len());
let airport_icaos: Vec<String> = airports.iter().map(|a| a.icao.to_string()).collect();
let mut peekable = airport_icaos.into_iter().peekable();
let mut observation_time = chrono::Utc::now().timestamp();
if peekable.peek().is_none() {
log::debug!("No airports to update, sleeping for 1 hour");
sleep(Duration::from_secs(3600)).await;
continue;
}
while peekable.peek().is_some() {
let chunk: Vec<String> = peekable.by_ref().take(limit as usize).collect();
let icao_string = chunk.join(",");
log::trace!("Updating METARS for: {}", icao_string);
match Metar::get_all(icao_string).await {
Ok(metars) => {
// Find the oldest observation time
for metar in metars {
if metar.observation_time.and_utc().timestamp() < observation_time {
observation_time = metar.observation_time.and_utc().timestamp();
}
}
}
Err(err) => {
log::warn!("{}", err);
}
}
// Sleep for 100ms between chunks to avoid rate limiting
sleep(Duration::from_millis(100)).await;
}
log::debug!("METAR update complete");
// Sleep until the earliest observation time is 1 hour old
// Bounded by 1 and 3600 seconds
let now = chrono::Utc::now().timestamp();
let sleep_time = std::cmp::min(std::cmp::max(1, now - (observation_time + 3600)), 3600);
log::debug!("Next update in {} seconds", sleep_time);
sleep(Duration::from_secs(sleep_time as u64)).await;
}
});
// tokio::spawn(async {
// let mut airports: Vec<AirportDb> = vec![];
// let limit = 100;
// loop {
// log::debug!("METAR update start");
// let total = match AirportDb::count(&AirportFilter::default()).await {
// Ok(t) => t,
// Err(err) => {
// log::warn!("{}", err);
// break;
// }
// };
// if total != airports.len() as i64 {
// log::debug!("{} cached airports, expected {}", airports.len(), total);
// airports = vec![];
// let pages = ((total as f32) / (if limit <= 0 { 1 } else { limit } as f32)).ceil() as i32;
// for page in 1..(pages + 1) {
// match AirportDb::find_all(&AirportFilter::default(), limit, page).await {
// Ok(mut a) => airports.append(&mut a),
// Err(err) => {
// log::warn!("{}", err);
// break;
// }
// }
// }
// }
// log::debug!("Updating {} airport METARS", airports.len());
//
// let airport_icaos: Vec<String> = airports.iter().map(|a| a.icao.to_string()).collect();
// let mut peekable = airport_icaos.into_iter().peekable();
// let mut observation_time = chrono::Utc::now().timestamp();
//
// if peekable.peek().is_none() {
// log::debug!("No airports to update, sleeping for 1 hour");
// sleep(Duration::from_secs(3600)).await;
// continue;
// }
//
// while peekable.peek().is_some() {
// let chunk: Vec<String> = peekable.by_ref().take(limit as usize).collect();
// let icao_string = chunk.join(",");
// log::warn!("Updating METARS for: {}", &icao_string); // TODO: back to trace after
// match Metar::find_all(&[&icao_string]).await {
// Ok(metars) => {
// // Find the oldest observation time
// for metar in metars {
// if metar.observation_time.timestamp() < observation_time {
// observation_time = metar.observation_time.timestamp();
// }
// }
// }
// Err(err) => {
// log::warn!("{}", err);
// }
// }
// // Sleep for 100ms between chunks to avoid rate limiting
// sleep(Duration::from_millis(100)).await;
// }
// log::debug!("METAR update complete");
// // Sleep until the earliest observation time is 1 hour old
// // Bounded by 1 and 3600 seconds
// let now = chrono::Utc::now().timestamp();
// let sleep_time = std::cmp::min(std::cmp::max(1, now - (observation_time + 3600)), 3600);
// log::debug!("Next update in {} seconds", sleep_time);
// sleep(Duration::from_secs(sleep_time as u64)).await;
// }
// });
}

View File

@@ -1,11 +1,12 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use diesel::prelude::*;
use crate::{
auth::hash,
db::{connection, schema::users},
error::ApiResult,
};
use crate::{auth::hash, error::ApiResult};
use crate::db;
pub const ADMIN_ROLE: &str = "ADMIN";
pub const USER_ROLE: &str = "USER";
const TABLE_NAME: &str = "users";
/**
* RegisterRequest
@@ -20,18 +21,15 @@ pub struct RegisterRequest {
impl RegisterRequest {
pub fn to_user(self) -> ApiResult<User> {
let hash = hash(&self.password)?;
let password_hash = hash(&self.password)?;
Ok(User {
email: self.email.to_lowercase(),
hash,
role: "user".to_string(),
password_hash,
role: USER_ROLE.to_string(),
first_name: self.first_name,
last_name: self.last_name,
updated_at: chrono::Utc::now().naive_utc(),
created_at: chrono::Utc::now().naive_utc(),
profile_picture: None,
favorites: vec![],
verified: false,
updated_at: Utc::now(),
created_at: Utc::now(),
})
}
}
@@ -54,7 +52,6 @@ pub struct UserResponse {
pub role: String,
pub first_name: String,
pub last_name: String,
pub profile_picture: Option<String>,
}
impl From<User> for UserResponse {
@@ -64,45 +61,69 @@ impl From<User> for UserResponse {
role: user.role,
first_name: user.first_name,
last_name: user.last_name,
profile_picture: user.profile_picture,
}
}
}
/**
* User
*/
#[derive(Debug, Insertable, AsChangeset, Queryable, QueryableByName, Serialize, Deserialize)]
#[diesel(table_name = users)]
#[derive(Serialize, Deserialize, sqlx::FromRow, Debug)]
pub struct User {
pub email: String,
pub hash: String,
pub password_hash: String,
pub role: String,
pub first_name: String,
pub last_name: String,
pub updated_at: chrono::NaiveDateTime,
pub created_at: chrono::NaiveDateTime,
pub profile_picture: Option<String>,
pub favorites: Vec<String>,
pub verified: bool,
pub updated_at: DateTime<Utc>,
pub created_at: DateTime<Utc>,
}
impl User {
pub fn get_by_email(email: &str) -> ApiResult<User> {
let mut conn = connection()?;
// Check if the user exists by email, case insensitive
pub async fn select(email: &str) -> Option<Self> {
let pool = db::pool();
let user: Option<Self> = sqlx::query_as::<_, Self>(&format!(
r#"
SELECT * FROM {} WHERE email = LOWER($1)
"#,
TABLE_NAME
))
.bind(email)
.fetch_optional(pool)
.await
.unwrap_or_else(|err| {
log::error!("Unable to find user '{}': {}", email, err);
None
});
let user = users::table
.filter(users::email.eq(email.to_lowercase()))
.first(&mut conn)?;
Ok(user)
user
}
pub fn insert(user: Self) -> ApiResult<User> {
let mut conn = connection()?;
let user = diesel::insert_into(users::table)
.values(user)
.get_result(&mut conn)?;
pub async fn insert(&self) -> ApiResult<User> {
let pool = db::pool();
let user: User = sqlx::query_as::<_, Self>(&format!(
r#"
INSERT INTO {} (
email,
password_hash,
role,
first_name,
last_name,
created_at,
updated_at
)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING *
"#,
TABLE_NAME,
))
.bind(&self.email)
.bind(&self.password_hash)
.bind(&self.role)
.bind(&self.first_name)
.bind(&self.last_name)
.bind(self.created_at)
.bind(self.updated_at)
.fetch_one(pool)
.await?;
Ok(user)
}
}