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

17
.env
View File

@@ -1,23 +1,26 @@
RUST_LOG=warn,api=info RUST_LOG=warn,api=info
DATABASE_CONTAINER=aviation-db POSTGRES_USER=aviation
DATABASE_USER=aviation POSTGRES_PASSWORD=CHANGEME
DATABASE_PASSWORD= POSTGRES_NAME=aviation
DATABASE_NAME=aviation POSTGRES_HOST=localhost
DATABASE_HOST=localhost POSTGRES_PORT=5432
DATABASE_PORT=5432
REDIS_HOST=localhost REDIS_HOST=localhost
REDIS_PORT=6379 REDIS_PORT=6379
MINIO_ROOT_USER=aviation MINIO_ROOT_USER=aviation
MINIO_ROOT_PASSWORD= MINIO_ROOT_PASSWORD=CHANGEME
MINIO_SCHEMA=http
MINIO_HOST=localhost MINIO_HOST=localhost
MINIO_PORT=9000 MINIO_PORT=9000
MINIO_PORT_INTERNAL=9001 MINIO_PORT_INTERNAL=9001
API_HOST=localhost API_HOST=localhost
API_PORT=5000 API_PORT=5000
ADMIN_USERNAME=admin
ADMIN_PASSWORD=CHANGEME
UI_PORT=3000 UI_PORT=3000
NODE_ENV=development NODE_ENV=development

38
.gitignore vendored
View File

@@ -1,40 +1,18 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
.vscode/
venv/
.idea/
# dependencies # dependencies
node_modules
/.pnp
.pnp.js
# testing
/coverage
# next.js
.next/ .next/
/out/ node_modules
target/
dist/
Cargo.lock
# production
/build
# misc
.DS_Store .DS_Store
*.pem
*.pem.pub
keys/
# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*
# local env files # local env files
.env*.local .env*.local
# vercel
.vercel
# typescript
*.tsbuildinfo
target/
dist/

View File

@@ -1,4 +0,0 @@
{
"rust-analyzer.showUnlinkedFileNotification": false,
"editor.tabSize": 2
}

View File

@@ -1,7 +1,8 @@
#!make #!make
SHELL := /bin/bash SHELL := /bin/bash
GIT_HASH ?= $(shell git log --format="%h" -n 1) export API_VERSION = $(shell awk -F ' = ' '$$1 ~ /package.version/ { gsub(/[\"]/, "", $$2); printf("%s",$$2) }' api/Cargo.toml)
export UI_VERSION := $(shell awk -F'"' '/"version"/ { print $$4 }' ui/package.json)
include .env include .env
-include .env.local -include .env.local
@@ -14,10 +15,47 @@ help: ## This info
@cat Makefile | grep -E '^[a-zA-Z\/_-]+:.*?## .*$$' | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' @cat Makefile | grep -E '^[a-zA-Z\/_-]+:.*?## .*$$' | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
@echo @echo
format: ## Format code format: format-api format-ui ## Format code
psql: ## Connect to the PSQL DB
@docker exec -it aviation-postgres psql -U ${POSTGRES_USER} -P pager=off
#################
# API Commands #
#################
format-api: ## Format code
@cd api && cargo fmt @cd api && cargo fmt
build-api: ## Build the project
@cd api && cargo build
run-api: ## Run the API project
@cd api && cargo run -p api
#################
# UI Commands #
#################
lint-ui: ## Run the linter
@cd ui && npm run lint
format-ui: ## Run the formatter
@cd ui && npm run format @cd ui && npm run format
build-ui: ## Build the UI app
@cd ui && npm install && npm run build
clean-ui: ## Remove UI build files
@cd ui && rm -rf node_modules dist
run-ui: ## Run the UI app
@cd ui && npm install && npm run dev
###################
# Docker Commands #
###################
backend-up: ## Start Docker containers backend-up: ## Start Docker containers
@docker compose --profile backend up -d @docker compose --profile backend up -d
@@ -41,12 +79,16 @@ frontend-down: ## Stop Docker containers
down-frontend: frontend-down down-frontend: frontend-down
docker-prune: ## Prune the docker system
@docker system prune -a
docker-clean: ## Stop the docker containers and remove volumes docker-clean: ## Stop the docker containers and remove volumes
@echo "Stopping docker container and removing volumes..."
@docker compose --profile frontend --profile api --profile backend down -v @docker compose --profile frontend --profile api --profile backend down -v
@echo "Docker container stopped and volumes removed"
docker-down: ## Stop the docker container
@docker compose --profile frontend --profile api --profile backend down
docker-up: ## Start the docker container
@docker compose --profile backend --profile api --profile frontend up -d
docker-refresh: docker-clean up-backend ## Refresh the database docker-refresh: docker-clean up-backend ## Refresh the database
psql: ## Connect to the PSQL DB
@docker exec -it ${DATABASE_CONTAINER} psql -U ${DATABASE_USER} -P pager=off

View File

@@ -5,18 +5,30 @@
## Setup ## Setup
1. Copy `.env.TEMPLATE` to `.env` 1. Override any environment variables in `.env.local`
2. Generate JWT RS256 (RSA Signature with SHA-256) Private/Public keys with `make generate` 2. Build the api and ui images with `make build`
3. Build the api and ui images with `make build` 3. Run the application with `make up`
4. Run the application with `make up`
## Decoding METARS ## Data Sources
### Airport Data
Potential Data sources
- https://adip.faa.gov/agis/public/#/airportSearch/advanced
- https://www.icao.int/Aviation-API-Data-Service/Pages/default.aspx
- https://ourairports.com/data/
- [mborsetti/airportsdata](https://github.com/mborsetti/airportsdata)
- https://www.iata.org/en/publications/directories/code-search/
- [openstreet](https://www.openstreetmap.org/#map=13/38.95223/-77.47417)
### Metar Data
Metar data is collected from aviationweather.gov.
#### Decoding METARS
The following resources were used to help decode METARS. The following resources were used to help decode METARS.
- [Metar Decode Key PDF](https://www.weather.gov/media/wrh/mesowest/metar_decode_key.pdf) - [Metar Decode Key PDF](https://www.weather.gov/media/wrh/mesowest/metar_decode_key.pdf)
- [Metar Decode (NPS EDU)](https://met.nps.edu/~bcreasey/mr3222/files/helpful/DecodeMETAR-TAF.html) - [Metar Decode (NPS EDU)](https://met.nps.edu/~bcreasey/mr3222/files/helpful/DecodeMETAR-TAF.html)
- [Weather Phenomena](http://www.moratech.com/aviation/metar-class/metar-pg9-ww.html) - [Weather Phenomena](http://www.moratech.com/aviation/metar-class/metar-pg9-ww.html)
- Airport dataset is based on [mborsetti/airportsdata](https://github.com/mborsetti/airportsdata) ### OpenMapTiles
## OpenMapTiles
[Generate Vector Tiles](https://openmaptiles.org/docs/generate/generate-openmaptiles/) [Generate Vector Tiles](https://openmaptiles.org/docs/generate/generate-openmaptiles/)

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" edition = "2021"
authors = ["Ben Sherriff <hello@bensherriff.com>"] authors = ["Ben Sherriff <hello@bensherriff.com>"]
repository = "https://github.com/bensherriff/aviation-weather" repository = "https://github.com/bensherriff/aviation-weather"
readme = "README.md" readme = "../README.md"
license = "GPL-3.0-or-later" license = "GPL-3.0-or-later"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # 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" actix-multipart = "0.7.2"
chrono = { version = "0.4.38", features = ["serde"] } chrono = { version = "0.4.38", features = ["serde"] }
dotenv = "0.15.0" dotenv = "0.15.0"
diesel = { version = "2.2.4", features = ["postgres", "r2d2", "uuid", "chrono", "serde_json"] } sqlx = { version = "0.8.2", features = ["runtime-tokio", "postgres", "chrono", "uuid"] }
postgis_diesel = { version = "2.4.1", features = ["serde"] }
diesel_migrations = { version = "2.2.0", features = ["postgres"] }
env_logger = "0.11.5" env_logger = "0.11.5"
lazy_static = "1.5.0"
r2d2 = "0.8.10"
reqwest = "0.12.7" reqwest = "0.12.7"
serde = {version = "1.0.209", features = ["derive"]} serde = {version = "1.0.209", features = ["derive"]}
serde_json = "1.0.127" 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"] } uuid = { version = "1.10.0", features = ["serde", "v4"] }
log = "0.4.22" log = "0.4.22"
argon2 = "0.5.3" 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" regex = "1.10.6"
futures-util = "0.3.30" futures-util = "0.3.30"
rust-s3 = "0.35.1" rust-s3 = "0.35.1"
rand = "0.8.5" rand = "0.8.5"
rand_chacha = "0.3.1" 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 futures_util::stream::StreamExt as _;
use crate::{ use crate::{
airports::{QueryAirport, QueryFilters, QueryOrderField, QueryOrderBy, Airport, AirportCategory}, airports::{Airport, AirportCategory},
db::{Response, Metadata}, db::Paged,
auth::{Auth, verify_role}, auth::{Auth, verify_role},
}; };
use actix_multipart::Multipart; use actix_multipart::Multipart;
use actix_web::{delete, get, post, put, web, HttpResponse, HttpRequest, ResponseError}; 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 serde::{Serialize, Deserialize};
use crate::airports::UpdateAirport;
use crate::users::ADMIN_ROLE;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
struct AirportsQuery { struct AirportsQuery {
@@ -27,7 +27,7 @@ struct AirportsQuery {
#[post("/import")] #[post("/import")]
async fn import_airports(mut payload: Multipart, auth: Auth) -> HttpResponse { 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); return ResponseError::error_response(&err);
}; };
@@ -43,7 +43,7 @@ async fn import_airports(mut payload: Multipart, auth: Auth) -> HttpResponse {
let data = match chunk { let data = match chunk {
Ok(data) => data, Ok(data) => data,
Err(err) => { Err(err) => {
error!("Failed to get chunk: {}", err); log::error!("Failed to get chunk: {}", err);
return ResponseError::error_response(&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) { let airports: Vec<Airport> = match serde_json::from_slice(&bytes) {
Ok(a) => a, Ok(a) => a,
Err(err) => { Err(err) => {
error!("Failed to parse JSON: {}", err); log::error!("Failed to parse JSON: {}", err);
return ResponseError::error_response(&err); return ResponseError::error_response(&err);
} }
}; };
// Convert Vec<Airport> to Vec<QueryAirport> and insert into database match Airport::insert_all(airports).await {
let query_airports: Vec<QueryAirport> = airports.into_iter().map(|a| a.into()).collect();
match QueryAirport::insert_all(query_airports) {
Ok(_) => {} Ok(_) => {}
Err(err) => return ResponseError::error_response(&err), Err(err) => return ResponseError::error_response(&err),
}; };
@@ -71,220 +69,83 @@ async fn import_airports(mut payload: Multipart, auth: Auth) -> HttpResponse {
#[get("")] #[get("")]
async fn get_airports(req: HttpRequest) -> HttpResponse { async fn get_airports(req: HttpRequest) -> HttpResponse {
let params = web::Query::<AirportsQuery>::from_query(req.query_string()).unwrap(); match Airport::select_all().await {
let mut filters = QueryFilters::default(); Ok(airports) => HttpResponse::Ok().json(airports),
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 }),
})
}
Err(err) => { Err(err) => {
error!("{}", err); log::error!("{}", err);
err.to_http_response() ResponseError::error_response(&err)
} }
} }
} }
#[get("/{icao}")] #[get("/{icao}")]
async fn get_airport(icao: web::Path<String>) -> HttpResponse { async fn get_airport(icao: web::Path<String>) -> HttpResponse {
match QueryAirport::get(&icao.into_inner()) { match Airport::select(&icao.into_inner()).await {
Ok(a) => { Some(airport) => HttpResponse::Ok().json(airport),
let airport: Airport = a.into(); None => HttpResponse::NotFound().finish(),
HttpResponse::Ok().json(airport)
}
Err(err) => {
error!("{}", err);
err.to_http_response()
}
} }
} }
#[post("")] #[post("")]
async fn create_airport(airport: web::Json<Airport>, auth: Auth) -> HttpResponse { async fn insert_airport(airport: web::Json<Airport>, auth: Auth) -> HttpResponse {
let _ = match verify_role(&auth, "admin") { let _ = match verify_role(&auth, ADMIN_ROLE) {
Ok(_) => {} Ok(_) => {}
Err(err) => return ResponseError::error_response(&err), Err(err) => return ResponseError::error_response(&err),
}; };
let query_airport: QueryAirport = airport.into_inner().into(); match airport.insert().await {
match QueryAirport::insert(query_airport) { Ok(a) => HttpResponse::Ok().json(a),
Ok(a) => {
let airport: Airport = a.into();
HttpResponse::Ok().json(airport)
}
Err(err) => { Err(err) => {
error!("{}", err); log::error!("{}", err);
err.to_http_response() ResponseError::error_response(&err)
} }
} }
} }
#[put("/{icao}")] #[put("/{icao}")]
async fn update_airport( async fn update_airport(
_icao: web::Path<String>, icao: web::Path<String>,
airport: web::Json<Airport>, airport: web::Json<UpdateAirport>,
auth: Auth, auth: Auth,
) -> HttpResponse { ) -> HttpResponse {
let _ = match verify_role(&auth, "admin") { let _ = match verify_role(&auth, ADMIN_ROLE) {
Ok(_) => {} Ok(_) => {}
Err(err) => return ResponseError::error_response(&err), Err(err) => return ResponseError::error_response(&err),
}; };
let query_airport: QueryAirport = airport.into_inner().into(); match Airport::update(&icao.into_inner(), &airport.into_inner()).await {
match QueryAirport::update(query_airport) { Ok(a) => HttpResponse::Ok().json(a),
Ok(a) => {
let airport: Airport = a.into();
HttpResponse::Ok().json(airport)
}
Err(err) => { Err(err) => {
error!("{}", err); log::error!("{}", err);
err.to_http_response() ResponseError::error_response(&err)
} }
} }
} }
#[delete("")] #[delete("")]
async fn delete_airports(auth: Auth) -> HttpResponse { async fn delete_airports(auth: Auth) -> HttpResponse {
let _ = match verify_role(&auth, "admin") { let _ = match verify_role(&auth, ADMIN_ROLE) {
Ok(_) => {} Ok(_) => {}
Err(err) => return ResponseError::error_response(&err), Err(err) => return ResponseError::error_response(&err),
}; };
match QueryAirport::delete(None) { match Airport::delete_all().await {
Ok(_) => HttpResponse::NoContent().finish(), Ok(_) => HttpResponse::NoContent().finish(),
Err(err) => { Err(err) => {
error!("{}", err); log::error!("{}", err);
err.to_http_response() ResponseError::error_response(&err)
} }
} }
} }
#[delete("/{icao}")] #[delete("/{icao}")]
async fn delete_airport(icao: web::Path<String>, auth: Auth) -> HttpResponse { 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(_) => {} Ok(_) => {}
Err(err) => return ResponseError::error_response(&err), 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(), Ok(_) => HttpResponse::NoContent().finish(),
Err(err) => { Err(err) => {
error!("{}", err); log::error!("{}", err);
err.to_http_response() ResponseError::error_response(&err)
} }
} }
} }
@@ -295,7 +156,7 @@ pub fn init_routes(config: &mut web::ServiceConfig) {
.service(import_airports) .service(import_airports)
.service(get_airports) .service(get_airports)
.service(get_airport) .service(get_airport)
.service(create_airport) .service(insert_airport)
.service(update_airport) .service(update_airport)
.service(delete_airports) .service(delete_airports)
.service(delete_airport), .service(delete_airport),

View File

@@ -4,6 +4,7 @@ use argon2::{
}; };
use rand::prelude::*; use rand::prelude::*;
use rand_chacha::ChaCha20Rng; use rand_chacha::ChaCha20Rng;
use serde::{Deserialize, Serialize};
mod model; mod model;
mod routes; mod routes;
@@ -13,11 +14,9 @@ pub use model::*;
pub use session::*; pub use session::*;
pub use routes::init_routes; 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(take: usize) -> String {
pub fn csprng_128bit(take: usize) -> String {
// Generate a CSPRNG 128-bit (16 byte) ID using alphanumeric characters (a-z, A-Z, 0-9) // Generate a CSPRNG 128-bit (16 byte) ID using alphanumeric characters (a-z, A-Z, 0-9)
let rng = ChaCha20Rng::from_entropy(); let rng = ChaCha20Rng::from_entropy();
rng rng
@@ -27,32 +26,51 @@ pub fn csprng_128bit(take: usize) -> String {
.collect() .collect()
} }
pub fn hash(str: &str) -> ApiResult<String> { pub fn hash(string: &str) -> ApiResult<String> {
let salt = SaltString::generate(&mut OsRng); let salt = SaltString::generate(&mut OsRng);
let bytes = str.as_bytes(); let hash = Argon2::default()
let hash = Argon2::default().hash_password(bytes, &salt)?.to_string(); .hash_password(string.as_bytes(), &salt)?
.to_string();
Ok(hash) Ok(hash)
} }
pub fn verify_hash(str: &str, hash: &str) -> bool { pub fn verify_hash(string: &str, hashed_string: &str) -> bool {
let bytes = str.as_bytes(); let bytes = string.as_bytes();
let parsed_hash = match PasswordHash::new(hash) { let parsed_hash = match PasswordHash::new(hashed_string) {
Ok(h) => h, 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) { Argon2::default()
Ok(_) => true, .verify_password(bytes, &parsed_hash)
Err(_) => false, .is_ok()
}
} }
pub fn verify_role(auth: &Auth, role: &str) -> ApiResult<()> { pub fn verify_role(auth: &Auth, role: &str) -> ApiResult<()> {
if auth.user.role == role { if auth.user.role == role {
Ok(()) Ok(())
} else { } else {
Err(ApiError { Err(Error {
status: 403, 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 actix_web::{FromRequest, Error as ActixError, HttpRequest, dev::Payload, http};
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use crate::{ use crate::{error::Error, users::User};
error::ApiError,
users::{User, UserResponse},
};
use super::{Session, SESSION_COOKIE_NAME}; use super::{Session, SESSION_COOKIE_NAME};
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Auth { pub struct Auth {
pub session_id: Option<String>, pub session_id: Option<String>,
pub user: UserResponse, pub api_key: Option<String>,
pub user: User,
} }
impl FromRequest for Auth { impl FromRequest for Auth {
@@ -21,7 +18,34 @@ impl FromRequest for Auth {
type Future = Pin<Box<dyn Future<Output = Result<Self, Self::Error>>>>; type Future = Pin<Box<dyn Future<Output = Result<Self, Self::Error>>>>;
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future { 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 let session_id = match req
.cookie(SESSION_COOKIE_NAME) .cookie(SESSION_COOKIE_NAME)
.map(|c| c.value().to_string()) .map(|c| c.value().to_string())
@@ -35,9 +59,9 @@ impl FromRequest for Auth {
None => { None => {
let fut = async { let fut = async {
Err( Err(
ApiError { Error {
status: 401, status: 401,
message: "No session ID found in the request".to_string(), details: "No session ID found in the request".to_string(),
} }
.into(), .into(),
) )
@@ -52,12 +76,13 @@ impl FromRequest for Auth {
// Verify the session // Verify the session
let fut = async move { let fut = async move {
match Session::verify(&session_id, &ip_address).await { match Session::verify(&session_id, &ip_address).await {
Ok(session) => match User::get_by_email(&session.email) { Ok(session) => match User::select(&session.email).await {
Ok(user) => Ok(Auth { Some(user) => Ok(Auth {
session_id: Some(session_id), 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()), Err(err) => Err(err.into()),
} }

View File

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

View File

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

View File

@@ -1,112 +1,129 @@
use crate::error::{ApiError, ApiResult}; use crate::error::ApiResult;
use diesel::{r2d2::ConnectionManager, PgConnection}; use redis::{Client as RedisClient, aio::MultiplexedConnection as RedisConnection, RedisResult};
use redis::{Client as RedisClient, aio::MultiplexedConnection as RedisConnection};
use s3::{ use s3::{
Bucket, Region, creds::Credentials, BucketConfiguration, request::ResponseData, Bucket, Region, creds::Credentials, BucketConfiguration, request::ResponseData,
bucket_ops::CreateBucketResponse, bucket_ops::CreateBucketResponse,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::diesel_migrations::MigrationHarness; use std::sync::OnceLock;
use lazy_static::lazy_static; use std::time::Duration;
use log::{error, info, warn}; use sqlx::{Pool, Postgres};
use r2d2; use sqlx::postgres::PgPoolOptions;
use std::env;
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 async fn initialize() -> ApiResult<()> {
pub type DbConnection = r2d2::PooledConnection<ConnectionManager<PgConnection>>; 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!(); // Setup Postgres pool connection
let pool = PgPoolOptions::new()
lazy_static! { .max_connections(5)
static ref POOL: Pool = { .acquire_timeout(Duration::from_secs(30))
let username = env::var("DATABASE_USER").expect("Database username is not set"); .connect(&format!(
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!(
"postgres://{}:{}@{}:{}/{}", "postgres://{}:{}@{}:{}/{}",
username, password, host, port, name db_user, db_password, db_host, db_port, db_name
); ))
let manager = ConnectionManager::<PgConnection>::new(url); .await?;
Pool::builder() match POOL.set(pool) {
.test_on_check_out(true) Ok(_) => {}
.build(manager) Err(_) => {
.expect("Failed to create db pool") log::warn!("Database pool already initialized");
}; }
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()); // 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); let url = format!("redis://{}:{}", host, port);
RedisClient::open(url).expect("Failed to create redis client") RedisClient::open(url).expect("Failed to create redis client")
}; };
static ref BUCKET: Bucket = { match REDIS.set(redis) {
let url = env::var("MINIO_HOST").unwrap_or("localhost".to_string()); Ok(_) => {}
let port = env::var("MINIO_PORT").unwrap_or("9000".to_string()); Err(_) => {
let user = env::var("MINIO_ROOT_USER").expect("MINIO_ROOT_USER is not set"); log::warn!("Redis client already initialized");
let password = env::var("MINIO_ROOT_PASSWORD").expect("MINIO_ROOT_PASSWORD is not set"); }
let base_url = format!("http://{}:{}", url, port); }
let region = Region::Custom { let schema = std::env::var("MINIO_SCHEMA").unwrap_or("http".to_string());
region: "".to_string(), let url = std::env::var("MINIO_HOST").unwrap_or("localhost".to_string());
endpoint: base_url, 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 { let region = Region::Custom {
access_key: Some(user), region: "".to_string(),
secret_key: Some(password), endpoint: base_url,
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 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() { pub fn pool() -> &'static Pool<Postgres> {
lazy_static::initialize(&POOL); POOL.get().unwrap()
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 connection() -> ApiResult<DbConnection> { fn redis() -> &'static RedisClient {
POOL REDIS.get().unwrap()
.get()
.map_err(|e| ApiError::new(500, format!("Failed getting db connection: {}", e)))
} }
pub fn redis_connection() -> ApiResult<redis::Connection> { pub fn redis_connection() -> RedisResult<redis::Connection> {
let conn = REDIS.get_connection()?; let conn = redis().get_connection()?;
Ok(conn) Ok(conn)
} }
pub async fn redis_async_connection() -> ApiResult<RedisConnection> { pub async fn redis_async_connection() -> RedisResult<RedisConnection> {
let conn = REDIS.get_multiplexed_async_connection().await?; let conn = redis().get_multiplexed_async_connection().await?;
Ok(conn) 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> { async fn create_bucket() -> ApiResult<CreateBucketResponse> {
let url = env::var("MINIO_URL").unwrap_or("localhost".to_string()); let url = std::env::var("MINIO_URL").unwrap_or("localhost".to_string());
let port = env::var("MINIO_PORT").unwrap_or("9000".to_string()); let port = std::env::var("MINIO_PORT").unwrap_or("9000".to_string());
let user = env::var("MINIO_ROOT_USER").expect("MINIO_ROOT_USER is not set"); let user = std::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 password = std::env::var("MINIO_ROOT_PASSWORD").expect("MINIO_ROOT_PASSWORD is not set");
let base_url = format!("http://{}:{}", url, port); let base_url = format!("http://{}:{}", url, port);
let region = Region::Custom { 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> { 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) Ok(response)
} }
pub async fn get_file(path: &str) -> ApiResult<Vec<u8>> { 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(); let bytes = response.bytes();
Ok(bytes.to_vec()) Ok(bytes.to_vec())
} }
pub async fn delete_file(path: &str) -> ApiResult<ResponseData> { 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) Ok(response)
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct Response<T> { pub struct Paged<T> {
pub data: T, pub data: T,
pub meta: Option<Metadata>,
}
#[derive(Serialize, Deserialize)]
pub struct Metadata {
pub page: i32, pub page: i32,
pub limit: i32, pub limit: i32,
pub total: i64, 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::http::StatusCode;
use actix_web::{HttpResponse, ResponseError}; use actix_web::{HttpResponse, ResponseError};
use diesel::result::Error as DieselError;
use log::warn; use log::warn;
use regex::Regex; use regex::Regex;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::json; use serde_json::json;
use std::fmt; use std::fmt;
pub type ApiResult<T> = Result<T, ApiError>; pub type ApiResult<T> = Result<T, Error>;
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
pub struct ApiError { pub struct Error {
pub status: u16, pub status: u16,
pub message: String, pub details: String,
} }
impl ApiError { impl Error {
pub fn new(status: u16, message: String) -> Self { pub fn new(status: u16, message: String) -> Self {
Self { status, message } Self {
status,
details: message,
}
} }
pub fn to_http_response(&self) -> HttpResponse { pub fn to_http_response(&self) -> HttpResponse {
let status = match StatusCode::from_u16(self.status) { let status = StatusCode::from_u16(self.status).unwrap_or_else(|err| {
Ok(s) => s, warn!("{}", err);
Err(err) => { StatusCode::INTERNAL_SERVER_ERROR
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 { impl From<std::io::Error> for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self.message.as_str())
}
}
impl From<std::io::Error> for ApiError {
fn from(error: std::io::Error) -> Self { fn from(error: std::io::Error) -> Self {
Self::new(500, format!("Unknown IO error: {}", error)) 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 { fn from(error: std::env::VarError) -> Self {
Self::new( Self::new(
500, 500,
@@ -53,47 +89,31 @@ impl From<std::env::VarError> for ApiError {
} }
} }
impl From<DieselError> for ApiError { impl From<reqwest::Error> for Error {
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 {
fn from(error: reqwest::Error) -> Self { fn from(error: reqwest::Error) -> Self {
Self::new(500, format!("Unknown reqwest error: {}", error)) 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 { fn from(error: serde_json::Error) -> Self {
Self::new(500, format!("Unknown serde_json error: {}", error)) 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 { fn from(error: argon2::password_hash::Error) -> Self {
Self::new(500, format!("Unknown argon2 error: {}", error)) 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 { fn from(error: redis::RedisError) -> Self {
Self::new(500, format!("Unknown redis error: {}", error)) 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 { fn from(error: s3::error::S3Error) -> Self {
match error { match error {
s3::error::S3Error::Credentials(err) => { s3::error::S3Error::Credentials(err) => {
@@ -102,9 +122,7 @@ impl From<s3::error::S3Error> for ApiError {
s3::error::S3Error::FromUtf8(err) => { s3::error::S3Error::FromUtf8(err) => {
Self::new(500, format!("Unknown s3 from utf8 error: {}", err)) Self::new(500, format!("Unknown s3 from utf8 error: {}", err))
} }
s3::error::S3Error::FmtError(err) => { s3::error::S3Error::FmtError(err) => Self::new(500, format!("Unknown s3 fmt error: {}", err)),
Self::new(500, format!("Unknown s3 fmt error: {}", err))
}
s3::error::S3Error::HeaderToStr(err) => { s3::error::S3Error::HeaderToStr(err) => {
Self::new(500, format!("Unknown s3 header to str error: {}", err)) Self::new(500, format!("Unknown s3 header to str error: {}", err))
} }
@@ -112,9 +130,7 @@ impl From<s3::error::S3Error> for ApiError {
500, 500,
format!("Unknown s3 hmac invalid length error: {}", err), format!("Unknown s3 hmac invalid length error: {}", err),
), ),
s3::error::S3Error::Http(error) => { s3::error::S3Error::Http(error) => Self::new(error.status_code().as_u16(), error.to_string()),
Self::new(error.status_code().as_u16(), error.to_string())
}
_ => { _ => {
let re = Regex::new(r"HTTP (\d{3})").unwrap(); let re = Regex::new(r"HTTP (\d{3})").unwrap();
// Apply the regex to the input string // Apply the regex to the input string
@@ -131,18 +147,43 @@ impl From<s3::error::S3Error> for ApiError {
} }
} }
impl ResponseError for ApiError { impl From<sqlx::Error> for Error {
fn error_response(&self) -> HttpResponse { fn from(error: sqlx::Error) -> Self {
let status = match StatusCode::from_u16(self.status) { match error {
Ok(status) => status, sqlx::Error::RowNotFound => Error::new(404, "Not found".to_string()),
Err(_) => StatusCode::INTERNAL_SERVER_ERROR, 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()),
let message = match status.as_u16() < 500 { sqlx::Error::Decode(_) => Error::new(422, error.to_string()),
true => self.message.clone(), sqlx::Error::PoolTimedOut => Error::new(503, error.to_string()),
false => "Internal server 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()),
HttpResponse::build(status).json(json!({ "status": status.as_u16(), "message": message })) 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 std::env;
use actix_cors::Cors; use actix_cors::Cors;
use actix_web::{App, HttpServer, middleware::Logger}; 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 airports;
mod auth; mod auth;
@@ -17,15 +15,41 @@ mod scheduler;
mod users; mod users;
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenv().ok(); initialize_environment()?;
env_logger::init_from_env(env_logger::Env::default().filter_or("RUST_LOG", "warn,api=info")); db::initialize().await?;
db::init().await;
// scheduler::update_airports(); // scheduler::update_airports();
let host = env::var("API_HOST").unwrap_or("localhost".to_string()); let host = env::var("API_HOST").unwrap_or("localhost".to_string());
let port = env::var("API_PORT").unwrap_or("5000".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 server = match HttpServer::new(move || {
let cors = Cors::default() let cors = Cors::default()
.allow_any_origin() .allow_any_origin()
@@ -49,9 +73,35 @@ async fn main() -> std::io::Result<()> {
} }
Err(err) => { Err(err) => {
log::error!("Could not bind server: {}", 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 crate::metars::Metar;
use actix_web::{get, web, HttpResponse, HttpRequest}; use actix_web::{get, web, HttpResponse, HttpRequest};
use log::error; use log::error;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct MetarsResponse {
pub data: Vec<Metar>,
pub meta: Metadata,
}
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
struct GetAllParameters { struct FindAllParameters {
icaos: Option<String>, icaos: Option<String>,
} }
#[get("metars")] #[get("metars")]
async fn get_all(req: HttpRequest) -> HttpResponse { async fn find_all(req: HttpRequest) -> HttpResponse {
let params = web::Query::<GetAllParameters>::from_query(req.query_string()).unwrap(); let parameters = web::Query::<FindAllParameters>::from_query(req.query_string()).unwrap();
let icao_option = params.icaos.clone(); let icao_option = &parameters.icaos;
let icao_string = match icao_option { let icao_string = match icao_option {
Some(i) => i, Some(i) => i,
None => return HttpResponse::UnprocessableEntity().body("Missing icaos parameter"), None => return HttpResponse::UnprocessableEntity().body("Missing icaos parameter"),
}; };
let icaos: Vec<&str> = icao_string.split(',').collect();
let metars = let metars = match Metar::find_all(&icaos).await {
match web::block(|| Ok::<_, ApiError>(async { Metar::get_all(icao_string).await })) Ok(a) => a,
.await Err(err) => {
.unwrap() error!("{}", err);
.unwrap() return err.to_http_response();
.await }
{ };
Ok(a) => a,
Err(err) => {
error!("{}", err);
return err.to_http_response();
}
};
HttpResponse::Ok().json(metars) HttpResponse::Ok().json(metars)
} }
pub fn init_routes(config: &mut web::ServiceConfig) { 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 tokio::time::{sleep, Duration};
use crate::airports::{QueryAirport, QueryFilters}; // use crate::airports::{AirportDb, AirportFilter};
use crate::metars::Metar; use crate::metars::Metar;
pub fn update_airports() { pub fn update_airports() {
tokio::spawn(async { // tokio::spawn(async {
let mut airports: Vec<QueryAirport> = vec![]; // let mut airports: Vec<AirportDb> = vec![];
let limit = 100; // let limit = 100;
loop { // loop {
log::debug!("METAR update start"); // log::debug!("METAR update start");
let total = match QueryAirport::get_count(&QueryFilters::default()) { // let total = match AirportDb::count(&AirportFilter::default()).await {
Ok(t) => t, // Ok(t) => t,
Err(err) => { // Err(err) => {
log::warn!("{}", err); // log::warn!("{}", err);
break; // break;
} // }
}; // };
if total != airports.len() as i64 { // if total != airports.len() as i64 {
log::debug!("{} cached airports, expected {}", airports.len(), total); // log::debug!("{} cached airports, expected {}", airports.len(), total);
airports = vec![]; // airports = vec![];
let pages = ((total as f32) / (if limit <= 0 { 1 } else { limit } as f32)).ceil() as i32; // let pages = ((total as f32) / (if limit <= 0 { 1 } else { limit } as f32)).ceil() as i32;
for page in 1..(pages + 1) { // for page in 1..(pages + 1) {
match QueryAirport::get_all(&QueryFilters::default(), limit, page) { // match AirportDb::find_all(&AirportFilter::default(), limit, page).await {
Ok(mut a) => airports.append(&mut a), // Ok(mut a) => airports.append(&mut a),
Err(err) => { // Err(err) => {
log::warn!("{}", err); // log::warn!("{}", err);
break; // break;
} // }
} // }
} // }
} // }
log::debug!("Updating {} airport METARS", airports.len()); // log::debug!("Updating {} airport METARS", airports.len());
//
let airport_icaos: Vec<String> = airports.iter().map(|a| a.icao.to_string()).collect(); // let airport_icaos: Vec<String> = airports.iter().map(|a| a.icao.to_string()).collect();
let mut peekable = airport_icaos.into_iter().peekable(); // let mut peekable = airport_icaos.into_iter().peekable();
let mut observation_time = chrono::Utc::now().timestamp(); // let mut observation_time = chrono::Utc::now().timestamp();
//
if peekable.peek().is_none() { // if peekable.peek().is_none() {
log::debug!("No airports to update, sleeping for 1 hour"); // log::debug!("No airports to update, sleeping for 1 hour");
sleep(Duration::from_secs(3600)).await; // sleep(Duration::from_secs(3600)).await;
continue; // continue;
} // }
//
while peekable.peek().is_some() { // while peekable.peek().is_some() {
let chunk: Vec<String> = peekable.by_ref().take(limit as usize).collect(); // let chunk: Vec<String> = peekable.by_ref().take(limit as usize).collect();
let icao_string = chunk.join(","); // let icao_string = chunk.join(",");
log::trace!("Updating METARS for: {}", icao_string); // log::warn!("Updating METARS for: {}", &icao_string); // TODO: back to trace after
match Metar::get_all(icao_string).await { // match Metar::find_all(&[&icao_string]).await {
Ok(metars) => { // Ok(metars) => {
// Find the oldest observation time // // Find the oldest observation time
for metar in metars { // for metar in metars {
if metar.observation_time.and_utc().timestamp() < observation_time { // if metar.observation_time.timestamp() < observation_time {
observation_time = metar.observation_time.and_utc().timestamp(); // observation_time = metar.observation_time.timestamp();
} // }
} // }
} // }
Err(err) => { // Err(err) => {
log::warn!("{}", err); // log::warn!("{}", err);
} // }
} // }
// Sleep for 100ms between chunks to avoid rate limiting // // Sleep for 100ms between chunks to avoid rate limiting
sleep(Duration::from_millis(100)).await; // sleep(Duration::from_millis(100)).await;
} // }
log::debug!("METAR update complete"); // log::debug!("METAR update complete");
// Sleep until the earliest observation time is 1 hour old // // Sleep until the earliest observation time is 1 hour old
// Bounded by 1 and 3600 seconds // // Bounded by 1 and 3600 seconds
let now = chrono::Utc::now().timestamp(); // let now = chrono::Utc::now().timestamp();
let sleep_time = std::cmp::min(std::cmp::max(1, now - (observation_time + 3600)), 3600); // let sleep_time = std::cmp::min(std::cmp::max(1, now - (observation_time + 3600)), 3600);
log::debug!("Next update in {} seconds", sleep_time); // log::debug!("Next update in {} seconds", sleep_time);
sleep(Duration::from_secs(sleep_time as u64)).await; // sleep(Duration::from_secs(sleep_time as u64)).await;
} // }
}); // });
} }

View File

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

View File

@@ -0,0 +1,11 @@
meta {
name: Delete Airport
type: http
seq: 4
}
delete {
url: {{BASE_URL}}/airports/TEST
body: none
auth: none
}

View File

@@ -0,0 +1,11 @@
meta {
name: Delete All Airports
type: http
seq: 5
}
delete {
url: {{BASE_URL}}/airports
body: none
auth: none
}

View File

@@ -0,0 +1,11 @@
meta {
name: Get Airport
type: http
seq: 2
}
get {
url: {{BASE_URL}}/airports/TEST
body: none
auth: none
}

View File

@@ -0,0 +1,11 @@
meta {
name: Get All Airports
type: http
seq: 3
}
get {
url: {{BASE_URL}}/airports
body: none
auth: none
}

View File

@@ -0,0 +1,28 @@
meta {
name: Insert Airport
type: http
seq: 1
}
post {
url: {{BASE_URL}}/airports
body: json
auth: none
}
body:json {
{
"icao": "TEST",
"name": "Test Airport",
"category": "unknown",
"iso_country": "",
"iso_region": "",
"municipality": "",
"elevation_ft": 0,
"latitude": 0,
"longitude": 0,
"runways": [],
"frequencies": [],
"public": true
}
}

View File

@@ -0,0 +1,15 @@
meta {
name: Find Metars
type: http
seq: 1
}
get {
url: {{BASE_URL}}/metars?icaos=KHEF,KJYO,KLNS,KRMN,KIAD,KSFO,KPBI,KJFK,KORD,KDAL,KSAN,KGFK
body: none
auth: none
}
params:query {
icaos: KHEF,KJYO,KLNS,KRMN,KIAD,KSFO,KPBI,KJFK,KORD,KDAL,KSAN,KGFK
}

View File

@@ -0,0 +1,23 @@
meta {
name: Create API Key
type: http
seq: 4
}
post {
url: {{BASE_URL}}/auth/key
body: none
auth: none
}
body:json {
{
"email": "john.doe@gmail.com",
"password": "fake_password123"
}
}
script:post-response {
const apiKey = res.body
bru.setVar("bearer",apiKey)
}

18
bruno/Users/Login.bru Normal file
View File

@@ -0,0 +1,18 @@
meta {
name: Login
type: http
seq: 2
}
post {
url: {{BASE_URL}}/auth/login
body: json
auth: none
}
body:json {
{
"email": "admin",
"password": "CHANGEME"
}
}

18
bruno/Users/Logout.bru Normal file
View File

@@ -0,0 +1,18 @@
meta {
name: Logout
type: http
seq: 3
}
post {
url: {{BASE_URL}}/auth/logout
body: none
auth: none
}
body:json {
{
"email": "john.doe@gmail.com",
"password": "fake_password123"
}
}

20
bruno/Users/Register.bru Normal file
View File

@@ -0,0 +1,20 @@
meta {
name: Register
type: http
seq: 1
}
post {
url: {{BASE_URL}}/auth/register
body: json
auth: none
}
body:json {
{
"email": "john.doe@gmail.com",
"password": "fake_password123",
"first_name": "John",
"last_name": "Doe"
}
}

9
bruno/bruno.json Normal file
View File

@@ -0,0 +1,9 @@
{
"version": "1",
"name": "Aviation",
"type": "collection",
"ignore": [
"node_modules",
".git"
]
}

3
bruno/collection.bru Normal file
View File

@@ -0,0 +1,3 @@
vars:pre-request {
BASE_URL: http://localhost:5000
}

104106
data/airports_2024-09-04.json Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -6,19 +6,19 @@ x-env_file: &env
name: aviation name: aviation
services: services:
db: postgres:
image: postgis/postgis:latest image: postgis/postgis:17-3.5
container_name: aviation-db container_name: aviation-postgres
env_file: *env env_file: *env
environment: environment:
POSTGRES_USER: ${DATABASE_USER} POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${DATABASE_PASSWORD} POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${DATABASE_NAME} POSTGRES_DB: ${POSTGRES_NAME}
volumes: volumes:
- db:/var/lib/postgresql/data - postgres:/var/lib/postgresql/data
- db_logs:/var/log - postgres_logs:/var/log
ports: ports:
- "${DATABASE_PORT:-5432}:5432" - "${POSTGRES_PORT:-5432}:5432"
networks: networks:
- backend - backend
profiles: profiles:
@@ -26,7 +26,7 @@ services:
restart: unless-stopped restart: unless-stopped
redis: redis:
image: redis:latest image: redis:8.0-M03 # Replace with valkey?
container_name: aviation-redis container_name: aviation-redis
volumes: volumes:
- redis:/data - redis:/data
@@ -39,7 +39,7 @@ services:
restart: unless-stopped restart: unless-stopped
minio: minio:
image: minio/minio image: minio/minio:RELEASE.2025-02-28T09-55-16Z
container_name: aviation-minio container_name: aviation-minio
environment: environment:
MINIO_ROOT_USER: ${MINIO_ROOT_USER} MINIO_ROOT_USER: ${MINIO_ROOT_USER}
@@ -64,7 +64,7 @@ services:
build: build:
context: api context: api
depends_on: depends_on:
- db - postgres
- redis - redis
- minio - minio
networks: networks:
@@ -96,8 +96,8 @@ services:
restart: unless-stopped restart: unless-stopped
volumes: volumes:
db: postgres:
db_logs: postgres_logs:
redis: redis:
minio: minio:

8
ui-old/.prettierrc.json Normal file
View File

@@ -0,0 +1,8 @@
{
"trailingComma": "none",
"tabWidth": 2,
"semi": true,
"singleQuote": true,
"jsxSingleQuote": true,
"printWidth": 120
}

39
ui-old/Dockerfile Normal file
View File

@@ -0,0 +1,39 @@
# Base
FROM node:21-alpine AS base
# Dependencies
FROM base as deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
# Dev
FROM base AS dev
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Builder
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build
# Runner
FROM base AS runner
WORKDIR /app
RUN addgroup --system --gid 1001 nodejs && adduser --system --uid 1001 nextjs
COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/package.json ./package.json
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 3000
ENV PORT 3000
ENV NEXT_TELEMETRY_DISABLED 1
CMD ["node", "server.js"]

5445
ui-old/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

47
ui-old/package.json Normal file
View File

@@ -0,0 +1,47 @@
{
"name": "aviation-weather",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@mantine/core": "^7.3.2",
"@mantine/form": "^7.3.2",
"@mantine/hooks": "^7.3.2",
"@mantine/modals": "^7.3.2",
"@mantine/notifications": "^7.3.2",
"js-cookie": "^3.0.5",
"leaflet": "^1.9.4",
"next": "^14.0.4",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-icons": "^4.11.0",
"react-leaflet": "^4.2.1",
"recharts": "^2.10.3",
"recoil": "^0.7.7"
},
"devDependencies": {
"@types/js-cookie": "^3.0.6",
"@types/leaflet": "^1.9.8",
"@types/node": "20.10.5",
"@types/react": "18.2.45",
"@types/react-dom": "18.2.18",
"@typescript-eslint/eslint-plugin": "^6.15.0",
"@typescript-eslint/parser": "^6.15.0",
"autoprefixer": "^10.4.16",
"eslint": "8.56.0",
"eslint-config-next": "14.0.4",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.0",
"postcss": "^8.4.32",
"postcss-import": "^15.1.0",
"postcss-preset-mantine": "^1.12.1",
"postcss-simple-vars": "^7.0.1",
"prettier": "^3.1.1",
"typescript": "5.3.3"
}
}

View File

Before

Width:  |  Height:  |  Size: 25 KiB

After

Width:  |  Height:  |  Size: 25 KiB

View File

Before

Width:  |  Height:  |  Size: 871 B

After

Width:  |  Height:  |  Size: 871 B

View File

Before

Width:  |  Height:  |  Size: 874 B

After

Width:  |  Height:  |  Size: 874 B

View File

Before

Width:  |  Height:  |  Size: 902 B

After

Width:  |  Height:  |  Size: 902 B

View File

Before

Width:  |  Height:  |  Size: 624 B

After

Width:  |  Height:  |  Size: 624 B

View File

Before

Width:  |  Height:  |  Size: 904 B

After

Width:  |  Height:  |  Size: 904 B

View File

Before

Width:  |  Height:  |  Size: 695 B

After

Width:  |  Height:  |  Size: 695 B

View File

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

Before

Width:  |  Height:  |  Size: 696 B

After

Width:  |  Height:  |  Size: 696 B

View File

Before

Width:  |  Height:  |  Size: 2.5 KiB

After

Width:  |  Height:  |  Size: 2.5 KiB

View File

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

Before

Width:  |  Height:  |  Size: 618 B

After

Width:  |  Height:  |  Size: 618 B

View File

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@@ -24,10 +24,8 @@ export default function RootLayout({ children }: { children: React.ReactNode })
<Notifications /> <Notifications />
<ModalsProvider> <ModalsProvider>
<RecoilRootWrapper> <RecoilRootWrapper>
<React.Suspense fallback={<Skeleton/>}> <React.Suspense fallback={<Skeleton />}>
<Loader> <Loader>{children}</Loader>
{children}
</Loader>
</React.Suspense> </React.Suspense>
</RecoilRootWrapper> </RecoilRootWrapper>
</ModalsProvider> </ModalsProvider>

View File

@@ -0,0 +1,111 @@
'use client';
import Link from 'next/link';
import { useState } from 'react';
import { getAirport, getAirports } from '@/api/airport';
import { Autocomplete, Button, Group, UnstyledButton } from '@mantine/core';
import { SetterOrUpdater, useRecoilState } from 'recoil';
import { useToggle } from '@mantine/hooks';
import { HeaderModal } from './HeaderModal';
import { coordinatesState } from '@/state/map';
import { User } from '@/api/auth.types';
import { usePathname, useRouter } from 'next/navigation';
import { FaMoon } from "react-icons/fa6";
import { FaSun } from "react-icons/fa6";
import UserMenu from './UserMenu';
import './styles.css';
interface HeaderProps {
user: User | undefined;
profilePicture: File | undefined;
setProfilePicture: SetterOrUpdater<File | undefined>;
login: ({ email, password }: { email: string, password: string }) => Promise<boolean>;
logout: () => Promise<void>;
register: ({ firstName, lastName, email, password }: { firstName: string, lastName: string, email: string, password: string }) => Promise<boolean>;
}
export default function Header({ user, profilePicture, setProfilePicture, login, logout, register }: HeaderProps) {
const [searchValue, setSearchValue] = useState('');
const [airports, setAirports] = useState<{ key: string; value: string; label: string }[]>([]);
const [modalType, toggle] = useToggle([undefined, 'login', 'register', 'reset']);
const [_, setCoordinates] = useRecoilState(coordinatesState);
const pathname = usePathname();
const router = useRouter();
async function onChange(value: string) {
setSearchValue(value);
const airportData = await getAirports({ icaos: [value], name: value });
setAirports(
airportData.data.map((airport) => ({
key: airport.icao,
value: airport.icao,
label: `${airport.icao} - ${airport.name}`
}))
);
}
async function onClick(value: string) {
setSearchValue('');
// Get current path
if (pathname == '/') {
const airport = await getAirport({ icao: value });
if (airport) {
setCoordinates({ lat: airport.data.latitude, lon: airport.data.longitude });
}
} else {
router.push(`/airport/${value}`)
}
}
return (
<>
<nav className='navbar'>
<div className='left'>
<Link href={'/'} className='title'>
<span>Aviation Weather</span>
</Link>
<div className='search'>
<Autocomplete
radius='xl'
placeholder='Search Airports...'
data={airports}
limit={10}
value={searchValue}
onChange={onChange}
onOptionSubmit={onClick}
onBlur={() => setSearchValue('')}
/>
</div>
</div>
<div className='right'>
<UnstyledButton style={{ paddingRight: '1em', margin: 'auto' }}>
<FaMoon />
{/* <FaSun /> */}
</UnstyledButton>
{user ? (
<UserMenu
user={user}
profilePicture={profilePicture}
setProfilePicture={setProfilePicture}
toggle={toggle}
logout={logout}
/>
) : (
<Group className='user'>
<Button onClick={() => toggle('login')}>Login</Button>
<Button variant='outline' onClick={() => toggle('register')}>
Sign up
</Button>
</Group>
)}
</div>
</nav>
<HeaderModal
type={modalType}
toggle={toggle}
login={login}
register={register}
/>
</>
);
}

Some files were not shown because too many files have changed in this diff Show More