Overhaul refactor. Still things in progress
17
.env
@@ -1,23 +1,26 @@
|
||||
RUST_LOG=warn,api=info
|
||||
|
||||
DATABASE_CONTAINER=aviation-db
|
||||
DATABASE_USER=aviation
|
||||
DATABASE_PASSWORD=
|
||||
DATABASE_NAME=aviation
|
||||
DATABASE_HOST=localhost
|
||||
DATABASE_PORT=5432
|
||||
POSTGRES_USER=aviation
|
||||
POSTGRES_PASSWORD=CHANGEME
|
||||
POSTGRES_NAME=aviation
|
||||
POSTGRES_HOST=localhost
|
||||
POSTGRES_PORT=5432
|
||||
|
||||
REDIS_HOST=localhost
|
||||
REDIS_PORT=6379
|
||||
|
||||
MINIO_ROOT_USER=aviation
|
||||
MINIO_ROOT_PASSWORD=
|
||||
MINIO_ROOT_PASSWORD=CHANGEME
|
||||
MINIO_SCHEMA=http
|
||||
MINIO_HOST=localhost
|
||||
MINIO_PORT=9000
|
||||
MINIO_PORT_INTERNAL=9001
|
||||
|
||||
API_HOST=localhost
|
||||
API_PORT=5000
|
||||
ADMIN_USERNAME=admin
|
||||
ADMIN_PASSWORD=CHANGEME
|
||||
|
||||
UI_PORT=3000
|
||||
NODE_ENV=development
|
||||
|
||||
|
||||
38
.gitignore
vendored
@@ -1,40 +1,18 @@
|
||||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||
|
||||
.vscode/
|
||||
venv/
|
||||
.idea/
|
||||
|
||||
# dependencies
|
||||
node_modules
|
||||
/.pnp
|
||||
.pnp.js
|
||||
|
||||
# testing
|
||||
/coverage
|
||||
|
||||
# next.js
|
||||
.next/
|
||||
/out/
|
||||
node_modules
|
||||
target/
|
||||
dist/
|
||||
Cargo.lock
|
||||
|
||||
# production
|
||||
/build
|
||||
|
||||
# misc
|
||||
.DS_Store
|
||||
*.pem
|
||||
*.pem.pub
|
||||
keys/
|
||||
|
||||
# debug
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
.pnpm-debug.log*
|
||||
|
||||
# local env files
|
||||
.env*.local
|
||||
|
||||
# vercel
|
||||
.vercel
|
||||
|
||||
# typescript
|
||||
*.tsbuildinfo
|
||||
|
||||
target/
|
||||
dist/
|
||||
|
||||
4
.vscode/settings.json
vendored
@@ -1,4 +0,0 @@
|
||||
{
|
||||
"rust-analyzer.showUnlinkedFileNotification": false,
|
||||
"editor.tabSize": 2
|
||||
}
|
||||
56
Makefile
@@ -1,7 +1,8 @@
|
||||
#!make
|
||||
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.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}'
|
||||
@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
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
@docker compose --profile backend up -d
|
||||
|
||||
@@ -41,12 +79,16 @@ frontend-down: ## Stop Docker containers
|
||||
|
||||
down-frontend: frontend-down
|
||||
|
||||
docker-prune: ## Prune the docker system
|
||||
@docker system prune -a
|
||||
|
||||
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
|
||||
@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
|
||||
|
||||
psql: ## Connect to the PSQL DB
|
||||
@docker exec -it ${DATABASE_CONTAINER} psql -U ${DATABASE_USER} -P pager=off
|
||||
28
README.md
@@ -5,18 +5,30 @@
|
||||
|
||||
## Setup
|
||||
|
||||
1. Copy `.env.TEMPLATE` to `.env`
|
||||
2. Generate JWT RS256 (RSA Signature with SHA-256) Private/Public keys with `make generate`
|
||||
3. Build the api and ui images with `make build`
|
||||
4. Run the application with `make up`
|
||||
1. Override any environment variables in `.env.local`
|
||||
2. Build the api and ui images with `make build`
|
||||
3. 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.
|
||||
- [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)
|
||||
- [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/)
|
||||
2023
api/Cargo.lock
generated
@@ -4,7 +4,7 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
authors = ["Ben Sherriff <hello@bensherriff.com>"]
|
||||
repository = "https://github.com/bensherriff/aviation-weather"
|
||||
readme = "README.md"
|
||||
readme = "../README.md"
|
||||
license = "GPL-3.0-or-later"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
@@ -16,12 +16,8 @@ actix-web-httpauth = "0.8.2"
|
||||
actix-multipart = "0.7.2"
|
||||
chrono = { version = "0.4.38", features = ["serde"] }
|
||||
dotenv = "0.15.0"
|
||||
diesel = { version = "2.2.4", features = ["postgres", "r2d2", "uuid", "chrono", "serde_json"] }
|
||||
postgis_diesel = { version = "2.4.1", features = ["serde"] }
|
||||
diesel_migrations = { version = "2.2.0", features = ["postgres"] }
|
||||
sqlx = { version = "0.8.2", features = ["runtime-tokio", "postgres", "chrono", "uuid"] }
|
||||
env_logger = "0.11.5"
|
||||
lazy_static = "1.5.0"
|
||||
r2d2 = "0.8.10"
|
||||
reqwest = "0.12.7"
|
||||
serde = {version = "1.0.209", features = ["derive"]}
|
||||
serde_json = "1.0.127"
|
||||
@@ -29,9 +25,11 @@ tokio = { version = "1.40.0", features = ["macros", "rt", "time"] }
|
||||
uuid = { version = "1.10.0", features = ["serde", "v4"] }
|
||||
log = "0.4.22"
|
||||
argon2 = "0.5.3"
|
||||
redis = { version = "0.26.1", features = ["tokio-comp", "connection-manager", "r2d2"] }
|
||||
redis = { version = "0.26.1", features = ["tokio-comp", "connection-manager", "r2d2", "json"] }
|
||||
regex = "1.10.6"
|
||||
futures-util = "0.3.30"
|
||||
rust-s3 = "0.35.1"
|
||||
rand = "0.8.5"
|
||||
rand_chacha = "0.3.1"
|
||||
geo-types = "0.7.15"
|
||||
byteorder = "1.5.0"
|
||||
|
||||
@@ -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::*"]
|
||||
@@ -1 +0,0 @@
|
||||
DROP TABLE airports;
|
||||
@@ -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
|
||||
);
|
||||
@@ -1 +0,0 @@
|
||||
DROP TABLE metars;
|
||||
@@ -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
|
||||
);
|
||||
@@ -1 +0,0 @@
|
||||
DROP TABLE airport_metar_cache;
|
||||
@@ -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
|
||||
);
|
||||
@@ -1 +0,0 @@
|
||||
DROP TABLE users;
|
||||
@@ -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
|
||||
);
|
||||
35
api/migrations/10232024_initial.sql
Normal 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
@@ -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(())
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
288
api/src/airports/model/airport.rs
Normal 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(())
|
||||
}
|
||||
}
|
||||
54
api/src/airports/model/airport_category.rs
Normal 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"),
|
||||
}
|
||||
}
|
||||
}
|
||||
15
api/src/airports/model/frequency.rs
Normal 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>,
|
||||
}
|
||||
9
api/src/airports/model/mod.rs
Normal 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::*;
|
||||
21
api/src/airports/model/runway.rs
Normal 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>,
|
||||
}
|
||||
@@ -2,15 +2,15 @@ use std::str::FromStr;
|
||||
use futures_util::stream::StreamExt as _;
|
||||
|
||||
use crate::{
|
||||
airports::{QueryAirport, QueryFilters, QueryOrderField, QueryOrderBy, Airport, AirportCategory},
|
||||
db::{Response, Metadata},
|
||||
airports::{Airport, AirportCategory},
|
||||
db::Paged,
|
||||
auth::{Auth, verify_role},
|
||||
};
|
||||
use actix_multipart::Multipart;
|
||||
use actix_web::{delete, get, post, put, web, HttpResponse, HttpRequest, ResponseError};
|
||||
use log::{error, warn};
|
||||
use postgis_diesel::types::{Polygon, Point};
|
||||
use serde::{Serialize, Deserialize};
|
||||
use crate::airports::UpdateAirport;
|
||||
use crate::users::ADMIN_ROLE;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct AirportsQuery {
|
||||
@@ -27,7 +27,7 @@ struct AirportsQuery {
|
||||
|
||||
#[post("/import")]
|
||||
async fn import_airports(mut payload: Multipart, auth: Auth) -> HttpResponse {
|
||||
if let Err(err) = verify_role(&auth, "admin") {
|
||||
if let Err(err) = verify_role(&auth, ADMIN_ROLE) {
|
||||
return ResponseError::error_response(&err);
|
||||
};
|
||||
|
||||
@@ -43,7 +43,7 @@ async fn import_airports(mut payload: Multipart, auth: Auth) -> HttpResponse {
|
||||
let data = match chunk {
|
||||
Ok(data) => data,
|
||||
Err(err) => {
|
||||
error!("Failed to get chunk: {}", err);
|
||||
log::error!("Failed to get chunk: {}", err);
|
||||
return ResponseError::error_response(&err);
|
||||
}
|
||||
};
|
||||
@@ -54,14 +54,12 @@ async fn import_airports(mut payload: Multipart, auth: Auth) -> HttpResponse {
|
||||
let airports: Vec<Airport> = match serde_json::from_slice(&bytes) {
|
||||
Ok(a) => a,
|
||||
Err(err) => {
|
||||
error!("Failed to parse JSON: {}", err);
|
||||
log::error!("Failed to parse JSON: {}", err);
|
||||
return ResponseError::error_response(&err);
|
||||
}
|
||||
};
|
||||
|
||||
// Convert Vec<Airport> to Vec<QueryAirport> and insert into database
|
||||
let query_airports: Vec<QueryAirport> = airports.into_iter().map(|a| a.into()).collect();
|
||||
match QueryAirport::insert_all(query_airports) {
|
||||
match Airport::insert_all(airports).await {
|
||||
Ok(_) => {}
|
||||
Err(err) => return ResponseError::error_response(&err),
|
||||
};
|
||||
@@ -71,220 +69,83 @@ async fn import_airports(mut payload: Multipart, auth: Auth) -> HttpResponse {
|
||||
|
||||
#[get("")]
|
||||
async fn get_airports(req: HttpRequest) -> HttpResponse {
|
||||
let params = web::Query::<AirportsQuery>::from_query(req.query_string()).unwrap();
|
||||
let mut filters = QueryFilters::default();
|
||||
filters.icaos = match ¶ms.icaos {
|
||||
Some(i) => Some(i.split(",").map(|s| s.to_string()).collect()),
|
||||
None => None,
|
||||
};
|
||||
filters.name = params.name.clone();
|
||||
filters.categories = match ¶ms.categories {
|
||||
Some(c) => Some(
|
||||
c.split(",")
|
||||
.map(|s| AirportCategory::from_str(s).unwrap())
|
||||
.collect(),
|
||||
),
|
||||
None => None,
|
||||
};
|
||||
filters.bounds = match ¶ms.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,
|
||||
match Airport::select_all().await {
|
||||
Ok(airports) => HttpResponse::Ok().json(airports),
|
||||
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 ¶ms.order_by {
|
||||
Some(o) => Some(QueryOrderBy::from_str(&o).unwrap()),
|
||||
None => None,
|
||||
};
|
||||
filters.order_field = match ¶ms.order_field {
|
||||
Some(o) => Some(QueryOrderField::from_str(&o).unwrap()),
|
||||
None => None,
|
||||
};
|
||||
filters.has_metar = match ¶ms.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) => {
|
||||
error!("{}", err);
|
||||
err.to_http_response()
|
||||
log::error!("{}", err);
|
||||
ResponseError::error_response(&err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/{icao}")]
|
||||
async fn get_airport(icao: web::Path<String>) -> HttpResponse {
|
||||
match QueryAirport::get(&icao.into_inner()) {
|
||||
Ok(a) => {
|
||||
let airport: Airport = a.into();
|
||||
HttpResponse::Ok().json(airport)
|
||||
}
|
||||
Err(err) => {
|
||||
error!("{}", err);
|
||||
err.to_http_response()
|
||||
}
|
||||
match Airport::select(&icao.into_inner()).await {
|
||||
Some(airport) => HttpResponse::Ok().json(airport),
|
||||
None => HttpResponse::NotFound().finish(),
|
||||
}
|
||||
}
|
||||
|
||||
#[post("")]
|
||||
async fn create_airport(airport: web::Json<Airport>, auth: Auth) -> HttpResponse {
|
||||
let _ = match verify_role(&auth, "admin") {
|
||||
async fn insert_airport(airport: web::Json<Airport>, auth: Auth) -> HttpResponse {
|
||||
let _ = match verify_role(&auth, ADMIN_ROLE) {
|
||||
Ok(_) => {}
|
||||
Err(err) => return ResponseError::error_response(&err),
|
||||
};
|
||||
let query_airport: QueryAirport = airport.into_inner().into();
|
||||
match QueryAirport::insert(query_airport) {
|
||||
Ok(a) => {
|
||||
let airport: Airport = a.into();
|
||||
HttpResponse::Ok().json(airport)
|
||||
}
|
||||
match airport.insert().await {
|
||||
Ok(a) => HttpResponse::Ok().json(a),
|
||||
Err(err) => {
|
||||
error!("{}", err);
|
||||
err.to_http_response()
|
||||
log::error!("{}", err);
|
||||
ResponseError::error_response(&err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[put("/{icao}")]
|
||||
async fn update_airport(
|
||||
_icao: web::Path<String>,
|
||||
airport: web::Json<Airport>,
|
||||
icao: web::Path<String>,
|
||||
airport: web::Json<UpdateAirport>,
|
||||
auth: Auth,
|
||||
) -> HttpResponse {
|
||||
let _ = match verify_role(&auth, "admin") {
|
||||
let _ = match verify_role(&auth, ADMIN_ROLE) {
|
||||
Ok(_) => {}
|
||||
Err(err) => return ResponseError::error_response(&err),
|
||||
};
|
||||
let query_airport: QueryAirport = airport.into_inner().into();
|
||||
match QueryAirport::update(query_airport) {
|
||||
Ok(a) => {
|
||||
let airport: Airport = a.into();
|
||||
HttpResponse::Ok().json(airport)
|
||||
}
|
||||
match Airport::update(&icao.into_inner(), &airport.into_inner()).await {
|
||||
Ok(a) => HttpResponse::Ok().json(a),
|
||||
Err(err) => {
|
||||
error!("{}", err);
|
||||
err.to_http_response()
|
||||
log::error!("{}", err);
|
||||
ResponseError::error_response(&err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[delete("")]
|
||||
async fn delete_airports(auth: Auth) -> HttpResponse {
|
||||
let _ = match verify_role(&auth, "admin") {
|
||||
let _ = match verify_role(&auth, ADMIN_ROLE) {
|
||||
Ok(_) => {}
|
||||
Err(err) => return ResponseError::error_response(&err),
|
||||
};
|
||||
match QueryAirport::delete(None) {
|
||||
match Airport::delete_all().await {
|
||||
Ok(_) => HttpResponse::NoContent().finish(),
|
||||
Err(err) => {
|
||||
error!("{}", err);
|
||||
err.to_http_response()
|
||||
log::error!("{}", err);
|
||||
ResponseError::error_response(&err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[delete("/{icao}")]
|
||||
async fn delete_airport(icao: web::Path<String>, auth: Auth) -> HttpResponse {
|
||||
let _ = match verify_role(&auth, "admin") {
|
||||
let _ = match verify_role(&auth, ADMIN_ROLE) {
|
||||
Ok(_) => {}
|
||||
Err(err) => return ResponseError::error_response(&err),
|
||||
};
|
||||
match QueryAirport::delete(Some(icao.into_inner())) {
|
||||
match Airport::delete(&icao.into_inner()).await {
|
||||
Ok(_) => HttpResponse::NoContent().finish(),
|
||||
Err(err) => {
|
||||
error!("{}", err);
|
||||
err.to_http_response()
|
||||
log::error!("{}", err);
|
||||
ResponseError::error_response(&err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -295,7 +156,7 @@ pub fn init_routes(config: &mut web::ServiceConfig) {
|
||||
.service(import_airports)
|
||||
.service(get_airports)
|
||||
.service(get_airport)
|
||||
.service(create_airport)
|
||||
.service(insert_airport)
|
||||
.service(update_airport)
|
||||
.service(delete_airports)
|
||||
.service(delete_airport),
|
||||
|
||||
@@ -4,6 +4,7 @@ use argon2::{
|
||||
};
|
||||
use rand::prelude::*;
|
||||
use rand_chacha::ChaCha20Rng;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
mod model;
|
||||
mod routes;
|
||||
@@ -13,11 +14,9 @@ pub use model::*;
|
||||
pub use session::*;
|
||||
pub use routes::init_routes;
|
||||
|
||||
use crate::error::{ApiError, ApiResult};
|
||||
use crate::error::{Error, ApiResult};
|
||||
|
||||
pub const SESSION_COOKIE_NAME: &str = "session";
|
||||
|
||||
pub fn csprng_128bit(take: usize) -> String {
|
||||
pub fn csprng(take: usize) -> String {
|
||||
// Generate a CSPRNG 128-bit (16 byte) ID using alphanumeric characters (a-z, A-Z, 0-9)
|
||||
let rng = ChaCha20Rng::from_entropy();
|
||||
rng
|
||||
@@ -27,32 +26,51 @@ pub fn csprng_128bit(take: usize) -> String {
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn hash(str: &str) -> ApiResult<String> {
|
||||
pub fn hash(string: &str) -> ApiResult<String> {
|
||||
let salt = SaltString::generate(&mut OsRng);
|
||||
let bytes = str.as_bytes();
|
||||
let hash = Argon2::default().hash_password(bytes, &salt)?.to_string();
|
||||
let hash = Argon2::default()
|
||||
.hash_password(string.as_bytes(), &salt)?
|
||||
.to_string();
|
||||
Ok(hash)
|
||||
}
|
||||
|
||||
pub fn verify_hash(str: &str, hash: &str) -> bool {
|
||||
let bytes = str.as_bytes();
|
||||
let parsed_hash = match PasswordHash::new(hash) {
|
||||
pub fn verify_hash(string: &str, hashed_string: &str) -> bool {
|
||||
let bytes = string.as_bytes();
|
||||
let parsed_hash = match PasswordHash::new(hashed_string) {
|
||||
Ok(h) => h,
|
||||
Err(_) => return false,
|
||||
};
|
||||
match Argon2::default().verify_password(bytes, &parsed_hash) {
|
||||
Ok(_) => true,
|
||||
Err(_) => false,
|
||||
Err(err) => {
|
||||
log::error!(
|
||||
"Failed to construct PasswordHash from '{}': {}",
|
||||
hashed_string,
|
||||
err
|
||||
);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
Argon2::default()
|
||||
.verify_password(bytes, &parsed_hash)
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
pub fn verify_role(auth: &Auth, role: &str) -> ApiResult<()> {
|
||||
if auth.user.role == role {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(ApiError {
|
||||
Err(Error {
|
||||
status: 403,
|
||||
message: "User does not have permission to perform this action.".to_string(),
|
||||
details: "User does not have permission to perform this action.".to_string(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_hash() {
|
||||
let password = hash("password").unwrap();
|
||||
assert!(!verify_hash(&password, "bad_password"));
|
||||
assert!(verify_hash("password", &password));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,17 +3,14 @@ use std::pin::Pin;
|
||||
|
||||
use actix_web::{FromRequest, Error as ActixError, HttpRequest, dev::Payload, http};
|
||||
use serde::{Serialize, Deserialize};
|
||||
use crate::{
|
||||
error::ApiError,
|
||||
users::{User, UserResponse},
|
||||
};
|
||||
|
||||
use crate::{error::Error, users::User};
|
||||
use super::{Session, SESSION_COOKIE_NAME};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Auth {
|
||||
pub session_id: Option<String>,
|
||||
pub user: UserResponse,
|
||||
pub api_key: Option<String>,
|
||||
pub user: User,
|
||||
}
|
||||
|
||||
impl FromRequest for Auth {
|
||||
@@ -21,7 +18,34 @@ impl FromRequest for Auth {
|
||||
type Future = Pin<Box<dyn Future<Output = Result<Self, Self::Error>>>>;
|
||||
|
||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||
// Get session ID from request
|
||||
// Check for API key
|
||||
match req
|
||||
.headers()
|
||||
.get(http::header::AUTHORIZATION)
|
||||
.map(|h| h.to_str().unwrap().split_at(7).1.to_string())
|
||||
{
|
||||
Some(key_id) => {
|
||||
let fut = async move {
|
||||
// Check if the Session API key exists
|
||||
let api_key = match Session::get(&key_id).await? {
|
||||
Some(session) => session,
|
||||
None => return Err(Error::new(401, "API Key does not exist".to_string()).into()),
|
||||
};
|
||||
match User::select(&api_key.email).await {
|
||||
Some(user) => Ok(Auth {
|
||||
session_id: None,
|
||||
api_key: Some(key_id),
|
||||
user,
|
||||
}),
|
||||
None => Err(Error::new(404, format!("User {} not found", api_key.email)).into()),
|
||||
}
|
||||
};
|
||||
return Box::pin(fut);
|
||||
}
|
||||
None => {}
|
||||
};
|
||||
|
||||
// Check for session
|
||||
let session_id = match req
|
||||
.cookie(SESSION_COOKIE_NAME)
|
||||
.map(|c| c.value().to_string())
|
||||
@@ -35,9 +59,9 @@ impl FromRequest for Auth {
|
||||
None => {
|
||||
let fut = async {
|
||||
Err(
|
||||
ApiError {
|
||||
Error {
|
||||
status: 401,
|
||||
message: "No session ID found in the request".to_string(),
|
||||
details: "No session ID found in the request".to_string(),
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
@@ -52,12 +76,13 @@ impl FromRequest for Auth {
|
||||
// Verify the session
|
||||
let fut = async move {
|
||||
match Session::verify(&session_id, &ip_address).await {
|
||||
Ok(session) => match User::get_by_email(&session.email) {
|
||||
Ok(user) => Ok(Auth {
|
||||
Ok(session) => match User::select(&session.email).await {
|
||||
Some(user) => Ok(Auth {
|
||||
session_id: Some(session_id),
|
||||
user: user.into(),
|
||||
api_key: None,
|
||||
user,
|
||||
}),
|
||||
Err(err) => Err(err.into()),
|
||||
None => Err(Error::new(404, format!("User {} not found", session.email)).into()),
|
||||
},
|
||||
Err(err) => Err(err.into()),
|
||||
}
|
||||
|
||||
@@ -5,11 +5,11 @@ use actix_web::{
|
||||
};
|
||||
use crate::{
|
||||
auth::{verify_hash, Session, SESSION_COOKIE_NAME},
|
||||
error::ApiError,
|
||||
error::Error,
|
||||
users::{LoginRequest, RegisterRequest, User, UserResponse},
|
||||
};
|
||||
|
||||
use crate::auth::Auth;
|
||||
use crate::auth::{Auth, DEFAULT_SESSION_TTL};
|
||||
|
||||
#[post("/register")]
|
||||
async fn register(user: web::Json<RegisterRequest>) -> HttpResponse {
|
||||
@@ -18,17 +18,18 @@ async fn register(user: web::Json<RegisterRequest>) -> HttpResponse {
|
||||
Ok(user) => user,
|
||||
Err(err) => return ResponseError::error_response(&err),
|
||||
};
|
||||
match User::insert(insert_user) {
|
||||
match insert_user.insert().await {
|
||||
Ok(user) => {
|
||||
let response: UserResponse = user.into();
|
||||
log::trace!("Registered user '{}'", response.email);
|
||||
HttpResponse::Created().json(response)
|
||||
},
|
||||
}
|
||||
Err(err) => {
|
||||
// Obfuscate the service error message to prevent leaking database details
|
||||
if err.status == 409 {
|
||||
return HttpResponse::Conflict().finish();
|
||||
HttpResponse::Conflict().finish()
|
||||
} else {
|
||||
return ResponseError::error_response(&err);
|
||||
ResponseError::error_response(&err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,29 +37,27 @@ async fn register(user: web::Json<RegisterRequest>) -> HttpResponse {
|
||||
|
||||
#[post("/login")]
|
||||
async fn login(request: web::Json<LoginRequest>, req: HttpRequest) -> HttpResponse {
|
||||
let email = request.email.clone();
|
||||
let email = &request.email;
|
||||
let ip_address = req.peer_addr().unwrap().ip().to_string();
|
||||
|
||||
let query_user = match User::get_by_email(&email) {
|
||||
Ok(query_user) => query_user,
|
||||
Err(err) => {
|
||||
log::error!("{}", err);
|
||||
return ResponseError::error_response(&err);
|
||||
}
|
||||
let query_user = match User::select(&email).await {
|
||||
Some(query_user) => query_user,
|
||||
None => return HttpResponse::Unauthorized().finish(),
|
||||
};
|
||||
if verify_hash(&query_user.hash, &request.password) {
|
||||
|
||||
if verify_hash(&request.password, &query_user.password_hash) {
|
||||
// Create a session
|
||||
let session = Session::new(&email, &ip_address);
|
||||
let session_cookie = session.cookie();
|
||||
let session = Session::new(64, &email, &ip_address, Some(DEFAULT_SESSION_TTL));
|
||||
let session_cookie = session.to_cookie();
|
||||
// Save the session to the database
|
||||
if let Err(err) = session.store().await {
|
||||
log::error!("Failed to store session");
|
||||
return ResponseError::error_response(&ApiError::new(500, err.to_string()));
|
||||
return ResponseError::error_response(&Error::new(500, err.to_string()));
|
||||
}
|
||||
return HttpResponse::Ok().cookie(session_cookie).finish();
|
||||
HttpResponse::Ok().cookie(session_cookie).finish()
|
||||
} else {
|
||||
log::error!("Invalid login attempt for {}", email);
|
||||
return HttpResponse::Unauthorized().finish();
|
||||
HttpResponse::Unauthorized().finish()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,11 +69,11 @@ async fn logout(req: HttpRequest, _auth: Auth) -> HttpResponse {
|
||||
let session_id = cookie.value().to_string();
|
||||
if let Err(err) = Session::delete(&session_id).await {
|
||||
log::error!("Failed to delete session");
|
||||
return ResponseError::error_response(&ApiError::new(500, err.to_string()));
|
||||
return ResponseError::error_response(&Error::new(500, err.to_string()));
|
||||
}
|
||||
}
|
||||
None => {
|
||||
return ResponseError::error_response(&ApiError::new(400, "Invalid session".to_string()));
|
||||
return ResponseError::error_response(&Error::new(400, "Invalid session".to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,11 +87,21 @@ async fn logout(req: HttpRequest, _auth: Auth) -> HttpResponse {
|
||||
HttpResponse::Ok().cookie(session_cookie).finish()
|
||||
}
|
||||
|
||||
#[post("/key")]
|
||||
async fn create_api_key(req: HttpRequest, auth: Auth) -> HttpResponse {
|
||||
let ip_address = req.peer_addr().unwrap().ip().to_string();
|
||||
let api_key = Session::new(128, &auth.user.email, &ip_address, None);
|
||||
|
||||
// TODO: store api key
|
||||
HttpResponse::Ok().body(api_key.session_id)
|
||||
}
|
||||
|
||||
pub fn init_routes(config: &mut web::ServiceConfig) {
|
||||
config.service(
|
||||
web::scope("auth")
|
||||
.service(register)
|
||||
.service(login)
|
||||
.service(logout),
|
||||
.service(logout)
|
||||
.service(create_api_key),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -5,10 +5,10 @@ use redis::{AsyncCommands, RedisResult};
|
||||
|
||||
use crate::{
|
||||
db::redis_async_connection,
|
||||
error::{ApiError, ApiResult},
|
||||
error::{Error, ApiResult},
|
||||
};
|
||||
|
||||
use super::{csprng_128bit, hash, verify_hash};
|
||||
use super::{csprng, hash, verify_hash};
|
||||
|
||||
pub const DEFAULT_SESSION_TTL: i64 = 86400; // (In seconds) 24 hours
|
||||
pub const SESSION_COOKIE_NAME: &str = "session";
|
||||
@@ -18,17 +18,21 @@ pub struct Session {
|
||||
pub session_id: String,
|
||||
pub email: String,
|
||||
pub ip_address: String,
|
||||
pub expires_at: DateTime<Utc>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub expires_at: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
impl Session {
|
||||
pub fn new(email: &str, ip_address: &str) -> Self {
|
||||
let now = chrono::Utc::now();
|
||||
pub fn new(take: usize, email: &str, ip_address: &str, ttl: Option<i64>) -> Self {
|
||||
let now = Utc::now();
|
||||
Self {
|
||||
session_id: csprng_128bit(32),
|
||||
session_id: csprng(take),
|
||||
email: email.to_string(),
|
||||
ip_address: hash(&ip_address).unwrap(),
|
||||
expires_at: now + chrono::Duration::seconds(DEFAULT_SESSION_TTL),
|
||||
expires_at: match ttl {
|
||||
Some(ttl) => Some(now + chrono::Duration::seconds(ttl)),
|
||||
None => None,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +40,13 @@ impl Session {
|
||||
let mut conn = redis_async_connection().await?;
|
||||
let key = self.session_id.clone();
|
||||
let value = serde_json::to_string(self)?;
|
||||
let result: RedisResult<()> = conn.set_ex(key, &value, DEFAULT_SESSION_TTL as u64).await;
|
||||
let result: RedisResult<()> = match self.expires_at {
|
||||
Some(expires_at) => {
|
||||
let ttl = expires_at.timestamp() - Utc::now().timestamp();
|
||||
conn.set_ex(key, &value, ttl as u64).await
|
||||
}
|
||||
None => conn.set(key, value).await,
|
||||
};
|
||||
match result {
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => Err(err.into()),
|
||||
@@ -66,26 +76,29 @@ impl Session {
|
||||
// Check if the session exists
|
||||
let session = match Self::get(session_id).await? {
|
||||
Some(session) => session,
|
||||
None => return Err(ApiError::new(401, "Session does not exist".to_string())),
|
||||
None => return Err(Error::new(401, "Session does not exist".to_string())),
|
||||
};
|
||||
|
||||
// Check if the IP Address matches the Session's IP Address
|
||||
if verify_hash(ip_address, &session.ip_address) {
|
||||
return Ok(session);
|
||||
Ok(session)
|
||||
} else {
|
||||
return Err(ApiError::new(
|
||||
401,
|
||||
"IP Address does not match".to_string(),
|
||||
));
|
||||
Err(Error::new(401, "IP Address does not match".to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cookie(&self) -> Cookie {
|
||||
pub fn to_cookie(&self) -> Cookie {
|
||||
let expires_at = match self.expires_at {
|
||||
Some(expires_at) => expires_at.timestamp(),
|
||||
None => DEFAULT_SESSION_TTL,
|
||||
};
|
||||
let ttl = expires_at - Utc::now().timestamp();
|
||||
Cookie::build(SESSION_COOKIE_NAME, self.session_id.clone())
|
||||
.path("/")
|
||||
.max_age(Duration::seconds(DEFAULT_SESSION_TTL))
|
||||
.secure(true)
|
||||
.http_only(true)
|
||||
.max_age(Duration::seconds(ttl))
|
||||
// TODO: enable secure and http_only
|
||||
// .secure(true)
|
||||
// .http_only(true)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,53 +1,63 @@
|
||||
use crate::error::{ApiError, ApiResult};
|
||||
use diesel::{r2d2::ConnectionManager, PgConnection};
|
||||
use redis::{Client as RedisClient, aio::MultiplexedConnection as RedisConnection};
|
||||
use crate::error::ApiResult;
|
||||
use redis::{Client as RedisClient, aio::MultiplexedConnection as RedisConnection, RedisResult};
|
||||
use s3::{
|
||||
Bucket, Region, creds::Credentials, BucketConfiguration, request::ResponseData,
|
||||
bucket_ops::CreateBucketResponse,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::diesel_migrations::MigrationHarness;
|
||||
use lazy_static::lazy_static;
|
||||
use log::{error, info, warn};
|
||||
use r2d2;
|
||||
use std::env;
|
||||
use std::sync::OnceLock;
|
||||
use std::time::Duration;
|
||||
use sqlx::{Pool, Postgres};
|
||||
use sqlx::postgres::PgPoolOptions;
|
||||
|
||||
pub mod schema;
|
||||
static POOL: OnceLock<Pool<Postgres>> = OnceLock::new();
|
||||
static REDIS: OnceLock<RedisClient> = OnceLock::new();
|
||||
static BUCKET: OnceLock<Bucket> = OnceLock::new();
|
||||
|
||||
type Pool = r2d2::Pool<ConnectionManager<PgConnection>>;
|
||||
pub type DbConnection = r2d2::PooledConnection<ConnectionManager<PgConnection>>;
|
||||
pub async fn initialize() -> ApiResult<()> {
|
||||
log::info!("Initializing database...");
|
||||
let db_user = std::env::var("POSTGRES_USER").unwrap_or("siren".to_string());
|
||||
let db_password = std::env::var("POSTGRES_PASSWORD").expect("POSTGRES_PASSWORD must be set");
|
||||
let db_host: String = std::env::var("POSTGRES_HOST").expect("POSTGRES_HOST must be set");
|
||||
let db_port = std::env::var("POSTGRES_PORT").unwrap_or("5432".to_string());
|
||||
let db_name = std::env::var("POSTGRES_NAME").unwrap_or("siren".to_string());
|
||||
|
||||
pub const MIGRATIONS: diesel_migrations::EmbeddedMigrations = embed_migrations!();
|
||||
|
||||
lazy_static! {
|
||||
static ref POOL: Pool = {
|
||||
let username = env::var("DATABASE_USER").expect("Database username is not set");
|
||||
let password = env::var("DATABASE_PASSWORD").expect("Database password is not set");
|
||||
let host = env::var("DATABASE_HOST").unwrap_or("localhost".to_string());
|
||||
let name = env::var("DATABASE_NAME").expect("Database name is not set");
|
||||
let port = env::var("DATABASE_PORT").unwrap_or("5432".to_string());
|
||||
let url = format!(
|
||||
// Setup Postgres pool connection
|
||||
let pool = PgPoolOptions::new()
|
||||
.max_connections(5)
|
||||
.acquire_timeout(Duration::from_secs(30))
|
||||
.connect(&format!(
|
||||
"postgres://{}:{}@{}:{}/{}",
|
||||
username, password, host, port, name
|
||||
);
|
||||
let manager = ConnectionManager::<PgConnection>::new(url);
|
||||
Pool::builder()
|
||||
.test_on_check_out(true)
|
||||
.build(manager)
|
||||
.expect("Failed to create db pool")
|
||||
};
|
||||
static ref REDIS: RedisClient = {
|
||||
let host = env::var("REDIS_HOST").unwrap_or("localhost".to_string());
|
||||
let port = env::var("REDIS_PORT").unwrap_or("6379".to_string());
|
||||
db_user, db_password, db_host, db_port, db_name
|
||||
))
|
||||
.await?;
|
||||
match POOL.set(pool) {
|
||||
Ok(_) => {}
|
||||
Err(_) => {
|
||||
log::warn!("Database pool already initialized");
|
||||
}
|
||||
}
|
||||
|
||||
// Setup Redis connection
|
||||
let redis = {
|
||||
let host = std::env::var("REDIS_HOST").unwrap_or("localhost".to_string());
|
||||
let port = std::env::var("REDIS_PORT").unwrap_or("6379".to_string());
|
||||
let url = format!("redis://{}:{}", host, port);
|
||||
RedisClient::open(url).expect("Failed to create redis client")
|
||||
};
|
||||
static ref BUCKET: Bucket = {
|
||||
let url = env::var("MINIO_HOST").unwrap_or("localhost".to_string());
|
||||
let port = env::var("MINIO_PORT").unwrap_or("9000".to_string());
|
||||
let user = env::var("MINIO_ROOT_USER").expect("MINIO_ROOT_USER is not set");
|
||||
let password = env::var("MINIO_ROOT_PASSWORD").expect("MINIO_ROOT_PASSWORD is not set");
|
||||
let base_url = format!("http://{}:{}", url, port);
|
||||
match REDIS.set(redis) {
|
||||
Ok(_) => {}
|
||||
Err(_) => {
|
||||
log::warn!("Redis client already initialized");
|
||||
}
|
||||
}
|
||||
|
||||
let schema = std::env::var("MINIO_SCHEMA").unwrap_or("http".to_string());
|
||||
let url = std::env::var("MINIO_HOST").unwrap_or("localhost".to_string());
|
||||
let port = std::env::var("MINIO_PORT").unwrap_or("9000".to_string());
|
||||
let user = std::env::var("MINIO_ROOT_USER").expect("MINIO_ROOT_USER is not set");
|
||||
let password = std::env::var("MINIO_ROOT_PASSWORD").expect("MINIO_ROOT_PASSWORD is not set");
|
||||
let base_url = format!("{}://{}:{}", schema, url, port);
|
||||
|
||||
let region = Region::Custom {
|
||||
region: "".to_string(),
|
||||
@@ -62,51 +72,58 @@ lazy_static! {
|
||||
expiration: None,
|
||||
};
|
||||
|
||||
*Bucket::new("aviation", region.clone(), credentials.clone())
|
||||
let bucket = Bucket::new("aviation", region.clone(), credentials.clone())
|
||||
.expect("Failed to create S3 Bucket")
|
||||
.with_path_style()
|
||||
};
|
||||
.with_path_style();
|
||||
|
||||
match BUCKET.set(*bucket) {
|
||||
Ok(_) => {}
|
||||
Err(_) => {
|
||||
log::warn!("Bucket client already initialized");
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn init() {
|
||||
lazy_static::initialize(&POOL);
|
||||
lazy_static::initialize(&REDIS);
|
||||
lazy_static::initialize(&BUCKET);
|
||||
match create_bucket().await {
|
||||
Ok(_) => info!("Bucket initialized"),
|
||||
Err(err) => match err.status {
|
||||
409 => warn!("Bucket already exists"),
|
||||
_ => error!("Failed to initialize bucket; {}", err),
|
||||
},
|
||||
};
|
||||
let mut pool: DbConnection = connection().expect("Failed to get db connection");
|
||||
match pool.run_pending_migrations(MIGRATIONS) {
|
||||
Ok(_) => info!("Database initialized"),
|
||||
Err(err) => error!("Failed to initialize database; {}", err),
|
||||
};
|
||||
// Run migrations
|
||||
match run_migrations().await {
|
||||
Ok(_) => log::debug!("Successfully ran migrations"),
|
||||
Err(e) => log::error!("Failed to run migrations: {}", e),
|
||||
}
|
||||
|
||||
pub fn connection() -> ApiResult<DbConnection> {
|
||||
POOL
|
||||
.get()
|
||||
.map_err(|e| ApiError::new(500, format!("Failed getting db connection: {}", e)))
|
||||
log::info!("Database initialized");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn redis_connection() -> ApiResult<redis::Connection> {
|
||||
let conn = REDIS.get_connection()?;
|
||||
pub fn pool() -> &'static Pool<Postgres> {
|
||||
POOL.get().unwrap()
|
||||
}
|
||||
|
||||
fn redis() -> &'static RedisClient {
|
||||
REDIS.get().unwrap()
|
||||
}
|
||||
|
||||
pub fn redis_connection() -> RedisResult<redis::Connection> {
|
||||
let conn = redis().get_connection()?;
|
||||
Ok(conn)
|
||||
}
|
||||
|
||||
pub async fn redis_async_connection() -> ApiResult<RedisConnection> {
|
||||
let conn = REDIS.get_multiplexed_async_connection().await?;
|
||||
pub async fn redis_async_connection() -> RedisResult<RedisConnection> {
|
||||
let conn = redis().get_multiplexed_async_connection().await?;
|
||||
Ok(conn)
|
||||
}
|
||||
|
||||
async fn run_migrations() -> ApiResult<()> {
|
||||
log::debug!("Running migrations");
|
||||
let pool = pool();
|
||||
sqlx::migrate!().run(pool).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn create_bucket() -> ApiResult<CreateBucketResponse> {
|
||||
let url = env::var("MINIO_URL").unwrap_or("localhost".to_string());
|
||||
let port = env::var("MINIO_PORT").unwrap_or("9000".to_string());
|
||||
let user = env::var("MINIO_ROOT_USER").expect("MINIO_ROOT_USER is not set");
|
||||
let password = env::var("MINIO_ROOT_PASSWORD").expect("MINIO_ROOT_PASSWORD is not set");
|
||||
let url = std::env::var("MINIO_URL").unwrap_or("localhost".to_string());
|
||||
let port = std::env::var("MINIO_PORT").unwrap_or("9000".to_string());
|
||||
let user = std::env::var("MINIO_ROOT_USER").expect("MINIO_ROOT_USER is not set");
|
||||
let password = std::env::var("MINIO_ROOT_PASSWORD").expect("MINIO_ROOT_PASSWORD is not set");
|
||||
let base_url = format!("http://{}:{}", url, port);
|
||||
|
||||
let region = Region::Custom {
|
||||
@@ -133,29 +150,24 @@ async fn create_bucket() -> ApiResult<CreateBucketResponse> {
|
||||
}
|
||||
|
||||
pub async fn upload_file(path: &str, content: &[u8]) -> ApiResult<ResponseData> {
|
||||
let response = BUCKET.put_object(path, content).await?;
|
||||
let response = BUCKET.get().unwrap().put_object(path, content).await?;
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
pub async fn get_file(path: &str) -> ApiResult<Vec<u8>> {
|
||||
let response = BUCKET.get_object(path).await?;
|
||||
let response = BUCKET.get().unwrap().get_object(path).await?;
|
||||
let bytes = response.bytes();
|
||||
Ok(bytes.to_vec())
|
||||
}
|
||||
|
||||
pub async fn delete_file(path: &str) -> ApiResult<ResponseData> {
|
||||
let response = BUCKET.delete_object(path).await?;
|
||||
let response = BUCKET.get().unwrap().delete_object(path).await?;
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Response<T> {
|
||||
pub struct Paged<T> {
|
||||
pub data: T,
|
||||
pub meta: Option<Metadata>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Metadata {
|
||||
pub page: i32,
|
||||
pub limit: i32,
|
||||
pub total: i64,
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
157
api/src/error.rs
@@ -1,50 +1,86 @@
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::{HttpResponse, ResponseError};
|
||||
use diesel::result::Error as DieselError;
|
||||
use log::warn;
|
||||
use regex::Regex;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
use std::fmt;
|
||||
|
||||
pub type ApiResult<T> = Result<T, ApiError>;
|
||||
pub type ApiResult<T> = Result<T, Error>;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct ApiError {
|
||||
pub struct Error {
|
||||
pub status: u16,
|
||||
pub message: String,
|
||||
pub details: String,
|
||||
}
|
||||
|
||||
impl ApiError {
|
||||
impl Error {
|
||||
pub fn new(status: u16, message: String) -> Self {
|
||||
Self { status, message }
|
||||
Self {
|
||||
status,
|
||||
details: message,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_http_response(&self) -> HttpResponse {
|
||||
let status = match StatusCode::from_u16(self.status) {
|
||||
Ok(s) => s,
|
||||
Err(err) => {
|
||||
let status = StatusCode::from_u16(self.status).unwrap_or_else(|err| {
|
||||
warn!("{}", err);
|
||||
StatusCode::INTERNAL_SERVER_ERROR
|
||||
});
|
||||
HttpResponse::build(status).body(self.details.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.write_str(self.details.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {
|
||||
fn description(&self) -> &str {
|
||||
&self.details
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseError for Error {
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
let status =
|
||||
StatusCode::from_u16(self.status).unwrap_or_else(|_| StatusCode::INTERNAL_SERVER_ERROR);
|
||||
|
||||
let status_code = status.as_u16();
|
||||
let details = match status_code {
|
||||
401 => String::from("Unauthorized"),
|
||||
code if code < 500 => self.details.clone(),
|
||||
_ => {
|
||||
log::error!("Internal server error: {}", self.details);
|
||||
String::from("Internal Server Error")
|
||||
}
|
||||
};
|
||||
HttpResponse::build(status).body(self.message.to_string())
|
||||
|
||||
HttpResponse::build(status).json(json!({ "status": status_code, "details": details }))
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ApiError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.write_str(self.message.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for ApiError {
|
||||
impl From<std::io::Error> for Error {
|
||||
fn from(error: std::io::Error) -> Self {
|
||||
Self::new(500, format!("Unknown IO error: {}", error))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::env::VarError> for ApiError {
|
||||
impl From<chrono::ParseError> for Error {
|
||||
fn from(error: chrono::ParseError) -> Self {
|
||||
Self::new(500, format!("Parse error: {}", error))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<core::num::ParseIntError> for Error {
|
||||
fn from(error: core::num::ParseIntError) -> Self {
|
||||
Self::new(500, format!("Parse error: {}", error))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::env::VarError> for Error {
|
||||
fn from(error: std::env::VarError) -> Self {
|
||||
Self::new(
|
||||
500,
|
||||
@@ -53,47 +89,31 @@ impl From<std::env::VarError> for ApiError {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DieselError> for ApiError {
|
||||
fn from(error: DieselError) -> Self {
|
||||
match error {
|
||||
DieselError::DatabaseError(kind, err) => match kind {
|
||||
diesel::result::DatabaseErrorKind::UniqueViolation => {
|
||||
Self::new(409, err.message().to_string())
|
||||
}
|
||||
_ => Self::new(500, err.message().to_string()),
|
||||
},
|
||||
DieselError::NotFound => Self::new(404, "The record was not found".to_string()),
|
||||
DieselError::SerializationError(err) => Self::new(422, err.to_string()),
|
||||
err => Self::new(500, format!("Unknown Diesel error: {}", err)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<reqwest::Error> for ApiError {
|
||||
impl From<reqwest::Error> for Error {
|
||||
fn from(error: reqwest::Error) -> Self {
|
||||
Self::new(500, format!("Unknown reqwest error: {}", error))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<serde_json::Error> for ApiError {
|
||||
impl From<serde_json::Error> for Error {
|
||||
fn from(error: serde_json::Error) -> Self {
|
||||
Self::new(500, format!("Unknown serde_json error: {}", error))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<argon2::password_hash::Error> for ApiError {
|
||||
impl From<argon2::password_hash::Error> for Error {
|
||||
fn from(error: argon2::password_hash::Error) -> Self {
|
||||
Self::new(500, format!("Unknown argon2 error: {}", error))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<redis::RedisError> for ApiError {
|
||||
impl From<redis::RedisError> for Error {
|
||||
fn from(error: redis::RedisError) -> Self {
|
||||
Self::new(500, format!("Unknown redis error: {}", error))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<s3::error::S3Error> for ApiError {
|
||||
impl From<s3::error::S3Error> for Error {
|
||||
fn from(error: s3::error::S3Error) -> Self {
|
||||
match error {
|
||||
s3::error::S3Error::Credentials(err) => {
|
||||
@@ -102,9 +122,7 @@ impl From<s3::error::S3Error> for ApiError {
|
||||
s3::error::S3Error::FromUtf8(err) => {
|
||||
Self::new(500, format!("Unknown s3 from utf8 error: {}", err))
|
||||
}
|
||||
s3::error::S3Error::FmtError(err) => {
|
||||
Self::new(500, format!("Unknown s3 fmt error: {}", err))
|
||||
}
|
||||
s3::error::S3Error::FmtError(err) => Self::new(500, format!("Unknown s3 fmt error: {}", err)),
|
||||
s3::error::S3Error::HeaderToStr(err) => {
|
||||
Self::new(500, format!("Unknown s3 header to str error: {}", err))
|
||||
}
|
||||
@@ -112,9 +130,7 @@ impl From<s3::error::S3Error> for ApiError {
|
||||
500,
|
||||
format!("Unknown s3 hmac invalid length error: {}", err),
|
||||
),
|
||||
s3::error::S3Error::Http(error) => {
|
||||
Self::new(error.status_code().as_u16(), error.to_string())
|
||||
}
|
||||
s3::error::S3Error::Http(error) => Self::new(error.status_code().as_u16(), error.to_string()),
|
||||
_ => {
|
||||
let re = Regex::new(r"HTTP (\d{3})").unwrap();
|
||||
// Apply the regex to the input string
|
||||
@@ -131,18 +147,43 @@ impl From<s3::error::S3Error> for ApiError {
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseError for ApiError {
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
let status = match StatusCode::from_u16(self.status) {
|
||||
Ok(status) => status,
|
||||
Err(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
};
|
||||
|
||||
let message = match status.as_u16() < 500 {
|
||||
true => self.message.clone(),
|
||||
false => "Internal server error".to_string(),
|
||||
};
|
||||
|
||||
HttpResponse::build(status).json(json!({ "status": status.as_u16(), "message": message }))
|
||||
impl From<sqlx::Error> for Error {
|
||||
fn from(error: sqlx::Error) -> Self {
|
||||
match error {
|
||||
sqlx::Error::RowNotFound => Error::new(404, "Not found".to_string()),
|
||||
sqlx::Error::ColumnIndexOutOfBounds { .. } => Error::new(422, error.to_string()),
|
||||
sqlx::Error::ColumnNotFound { .. } => Error::new(422, error.to_string()),
|
||||
sqlx::Error::ColumnDecode { .. } => Error::new(422, error.to_string()),
|
||||
sqlx::Error::Decode(_) => Error::new(422, error.to_string()),
|
||||
sqlx::Error::PoolTimedOut => Error::new(503, error.to_string()),
|
||||
sqlx::Error::PoolClosed => Error::new(503, error.to_string()),
|
||||
sqlx::Error::Tls(_) => Error::new(500, error.to_string()),
|
||||
sqlx::Error::Io(_) => Error::new(500, error.to_string()),
|
||||
sqlx::Error::Protocol(_) => Error::new(500, error.to_string()),
|
||||
sqlx::Error::Configuration(_) => Error::new(500, error.to_string()),
|
||||
sqlx::Error::AnyDriverError(_) => Error::new(500, error.to_string()),
|
||||
sqlx::Error::Database(err) => {
|
||||
if let Some(code) = err.code() {
|
||||
match code.trim() {
|
||||
// Unique violation
|
||||
"23505" => return Error::new(409, err.to_string()),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
Error::new(500, err.to_string())
|
||||
}
|
||||
sqlx::Error::Migrate(_) => Error::new(500, error.to_string()),
|
||||
sqlx::Error::TypeNotFound { type_name } => {
|
||||
Error::new(500, format!("Type not found: {}", type_name))
|
||||
}
|
||||
sqlx::Error::WorkerCrashed => Error::new(500, error.to_string()),
|
||||
_ => Error::new(500, error.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<sqlx::migrate::MigrateError> for Error {
|
||||
fn from(error: sqlx::migrate::MigrateError) -> Self {
|
||||
Error::new(500, error.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
extern crate diesel;
|
||||
#[macro_use]
|
||||
extern crate diesel_migrations;
|
||||
|
||||
use std::env;
|
||||
|
||||
use actix_cors::Cors;
|
||||
use actix_web::{App, HttpServer, middleware::Logger};
|
||||
use dotenv::dotenv;
|
||||
use dotenv::from_filename;
|
||||
use crate::auth::hash;
|
||||
use crate::users::{User, ADMIN_ROLE};
|
||||
|
||||
mod airports;
|
||||
mod auth;
|
||||
@@ -17,15 +15,41 @@ mod scheduler;
|
||||
mod users;
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
dotenv().ok();
|
||||
env_logger::init_from_env(env_logger::Env::default().filter_or("RUST_LOG", "warn,api=info"));
|
||||
db::init().await;
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
initialize_environment()?;
|
||||
db::initialize().await?;
|
||||
// scheduler::update_airports();
|
||||
|
||||
let host = env::var("API_HOST").unwrap_or("localhost".to_string());
|
||||
let port = env::var("API_PORT").unwrap_or("5000".to_string());
|
||||
|
||||
// Initialize admin user
|
||||
let admin_username = env::var("ADMIN_USERNAME");
|
||||
let admin_password = env::var("ADMIN_PASSWORD");
|
||||
if admin_username.is_ok() && admin_password.is_ok() {
|
||||
let username = admin_username.unwrap();
|
||||
if User::select(&username).await.is_none() {
|
||||
log::debug!("Creating default administrator");
|
||||
let password = admin_password.unwrap();
|
||||
let password_hash = hash(&password)?;
|
||||
let admin_user = User {
|
||||
email: username,
|
||||
password_hash,
|
||||
role: ADMIN_ROLE.to_string(),
|
||||
first_name: "Admin".to_string(),
|
||||
last_name: "".to_string(),
|
||||
updated_at: Default::default(),
|
||||
created_at: Default::default(),
|
||||
};
|
||||
match admin_user.insert().await {
|
||||
Ok(_) => log::debug!("Default administrator was successfully created"),
|
||||
Err(err) => {
|
||||
log::warn!("{}", err);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
let server = match HttpServer::new(move || {
|
||||
let cors = Cors::default()
|
||||
.allow_any_origin()
|
||||
@@ -49,9 +73,35 @@ async fn main() -> std::io::Result<()> {
|
||||
}
|
||||
Err(err) => {
|
||||
log::error!("Could not bind server: {}", err);
|
||||
return Err(err);
|
||||
return Err(err.into());
|
||||
}
|
||||
};
|
||||
|
||||
server.run().await
|
||||
if let Err(err) = server.run().await {
|
||||
return Err(err.into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn initialize_environment() -> std::io::Result<()> {
|
||||
// Iterate over files in the current directory
|
||||
for entry in std::fs::read_dir(".")? {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
|
||||
// Check if the file name starts with ".env" and is a file
|
||||
if let Some(file_name) = path.file_name().and_then(|n| n.to_str()) {
|
||||
if file_name.starts_with(".env") && path.is_file() {
|
||||
// Try to load the file
|
||||
if let Err(err) = from_filename(&file_name) {
|
||||
eprintln!("Failed to load {}: {}", file_name, err);
|
||||
} else {
|
||||
println!("Loaded: {}", file_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
env_logger::init_from_env(env_logger::Env::default().filter_or("RUST_LOG", "warn,api=info"));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,13 +1,71 @@
|
||||
use crate::airports::QueryAirport;
|
||||
use crate::error::ApiError;
|
||||
use crate::error::Error;
|
||||
use crate::{error::ApiResult, db};
|
||||
use crate::db::schema::metars::{self};
|
||||
use chrono::Datelike;
|
||||
use diesel::{prelude::*, sql_query};
|
||||
use log::{warn, trace};
|
||||
use chrono::{DateTime, Datelike, Utc};
|
||||
use std::collections::HashSet;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
const TABLE_NAME: &str = "metars";
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Metar {
|
||||
pub station_id: String, // icao
|
||||
pub raw_text: String,
|
||||
pub observation_time: DateTime<Utc>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub temp_c: Option<f64>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub dewpoint_c: Option<f64>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub wind_dir_degrees: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub wind_speed_kt: Option<f64>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub wind_gust_kt: Option<f64>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub variable_wind_dir_degrees: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub visibility_statute_mi: Option<String>,
|
||||
pub runway_visual_range: Vec<RunwayVisualRange>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub altim_in_hg: Option<f64>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub sea_level_pressure_mb: Option<f64>,
|
||||
pub quality_control_flags: QualityControlFlags,
|
||||
pub weather_phenomena: Vec<String>,
|
||||
pub sky_condition: Vec<SkyCondition>,
|
||||
pub flight_category: FlightCategory,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub three_hr_pressure_tendency_mb: Option<f64>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub max_t_c: Option<f64>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub min_t_c: Option<f64>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub precip_in: Option<f64>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct RunwayVisualRange {
|
||||
pub runway: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub visibility_ft: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub variable_visibility_high_ft: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub variable_visibility_low_ft: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for RunwayVisualRange {
|
||||
fn default() -> Self {
|
||||
RunwayVisualRange {
|
||||
runway: "".to_string(),
|
||||
visibility_ft: None,
|
||||
variable_visibility_high_ft: None,
|
||||
variable_visibility_low_ft: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct QualityControlFlags {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
@@ -59,28 +117,6 @@ impl Default for SkyCondition {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct RunwayVisualRange {
|
||||
pub runway: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub visibility_ft: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub variable_visibility_high_ft: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub variable_visibility_low_ft: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for RunwayVisualRange {
|
||||
fn default() -> Self {
|
||||
RunwayVisualRange {
|
||||
runway: "".to_string(),
|
||||
visibility_ft: None,
|
||||
variable_visibility_high_ft: None,
|
||||
variable_visibility_low_ft: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub enum FlightCategory {
|
||||
VFR,
|
||||
@@ -90,54 +126,14 @@ pub enum FlightCategory {
|
||||
UNKN,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Metar {
|
||||
pub raw_text: String,
|
||||
pub station_id: String,
|
||||
pub observation_time: chrono::NaiveDateTime,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub temp_c: Option<f64>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub dewpoint_c: Option<f64>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub wind_dir_degrees: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub wind_speed_kt: Option<f64>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub wind_gust_kt: Option<f64>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub variable_wind_dir_degrees: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub visibility_statute_mi: Option<String>,
|
||||
pub runway_visual_range: Vec<RunwayVisualRange>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub altim_in_hg: Option<f64>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub sea_level_pressure_mb: Option<f64>,
|
||||
pub quality_control_flags: QualityControlFlags,
|
||||
pub weather_phenomena: Vec<String>,
|
||||
pub sky_condition: Vec<SkyCondition>,
|
||||
pub flight_category: FlightCategory,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub three_hr_pressure_tendency_mb: Option<f64>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub max_t_c: Option<f64>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub min_t_c: Option<f64>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub precip_in: Option<f64>,
|
||||
}
|
||||
|
||||
impl Default for Metar {
|
||||
fn default() -> Self {
|
||||
Metar {
|
||||
Self {
|
||||
raw_text: "".to_string(),
|
||||
station_id: "".to_string(),
|
||||
observation_time: chrono::NaiveDateTime::parse_from_str(
|
||||
"1970-01-01T00:00:00",
|
||||
"%Y-%m-%dT%H:%M:%S",
|
||||
)
|
||||
.unwrap(),
|
||||
observation_time: chrono::DateTime::parse_from_rfc3339("1970-01-01T00:00:00Z")
|
||||
.unwrap()
|
||||
.with_timezone(&Utc),
|
||||
temp_c: None,
|
||||
dewpoint_c: None,
|
||||
wind_dir_degrees: None,
|
||||
@@ -160,20 +156,50 @@ impl Default for Metar {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, sqlx::FromRow, Debug)]
|
||||
struct MetarDb {
|
||||
icao: String,
|
||||
observation_time: DateTime<Utc>,
|
||||
raw_text: String,
|
||||
data: serde_json::Value,
|
||||
}
|
||||
|
||||
impl Metar {
|
||||
fn parse(metar_strings: Vec<&str>) -> ApiResult<Vec<Self>> {
|
||||
let mut metars: Vec<Self> = vec![];
|
||||
fn parse_multiple(metar_strings: &Vec<&str>) -> ApiResult<Vec<Self>> {
|
||||
let mut metars: Vec<Metar> = vec![];
|
||||
for metar_string in metar_strings {
|
||||
trace!("Parsing METAR data: {}", metar_string);
|
||||
match Metar::parse(metar_string) {
|
||||
Ok(metar) => metars.push(metar),
|
||||
Err(e) => {
|
||||
log::warn!("Failed to parse metar string: {}", e);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Ok(metars)
|
||||
}
|
||||
|
||||
fn parse(metar_string: &str) -> ApiResult<Self> {
|
||||
if metar_string.is_empty() {
|
||||
return Err(Error::new(
|
||||
404,
|
||||
"Unable to parse empty METAR data".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
log::trace!("Parsing METAR data: {}", metar_string);
|
||||
let mut metar: Metar = Metar::default();
|
||||
metar.raw_text = metar_string.to_owned();
|
||||
let mut metar_parts: Vec<&str> = metar_string.split_whitespace().collect();
|
||||
if metar_parts.len() < 4 {
|
||||
warn!(
|
||||
return Err(Error::new(
|
||||
500,
|
||||
format!(
|
||||
"Unable to parse METAR data in an unexpected format: {}",
|
||||
metar_string
|
||||
);
|
||||
continue;
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
// Station Identifier
|
||||
@@ -184,19 +210,30 @@ impl Metar {
|
||||
let observation_time = metar_parts[0];
|
||||
metar_parts.remove(0);
|
||||
if observation_time.len() != 7 {
|
||||
warn!(
|
||||
return Err(Error::new(
|
||||
500,
|
||||
format!(
|
||||
"Unable to parse observation time in {}: {}",
|
||||
observation_time, metar_string
|
||||
);
|
||||
continue;
|
||||
),
|
||||
));
|
||||
}
|
||||
let observation_time_day = &observation_time[0..2];
|
||||
let observation_time_hour = &observation_time[2..4];
|
||||
let observation_time_minute = &observation_time[4..6];
|
||||
let current_time = chrono::Utc::now().naive_utc();
|
||||
let observation_time_day = match observation_time[0..2].parse::<u32>() {
|
||||
Ok(day) => day,
|
||||
Err(err) => return Err(err.into()),
|
||||
};
|
||||
let mut observation_time_hour = match observation_time[2..4].parse::<u32>() {
|
||||
Ok(hour) => hour,
|
||||
Err(err) => return Err(err.into()),
|
||||
};
|
||||
let observation_time_minute = match observation_time[4..6].parse::<u32>() {
|
||||
Ok(minute) => minute,
|
||||
Err(err) => return Err(err.into()),
|
||||
};
|
||||
let current_time = Utc::now().naive_utc();
|
||||
|
||||
// Check if the observation time is from the previous month
|
||||
let observation_time_month =
|
||||
if current_time.day() > observation_time_day.parse::<u32>().unwrap() {
|
||||
let observation_time_month = if current_time.day() > observation_time_day {
|
||||
current_time.month() - 1
|
||||
} else {
|
||||
current_time.month()
|
||||
@@ -207,23 +244,18 @@ impl Metar {
|
||||
} else {
|
||||
current_time.year()
|
||||
};
|
||||
// Handle Daylight Savings Time
|
||||
let observation_time_hour =
|
||||
if observation_time_month == 3 && observation_time_day.parse::<u32>().unwrap() < 14 {
|
||||
observation_time_hour.parse::<u32>().unwrap() - 1
|
||||
} else {
|
||||
observation_time_hour.parse::<u32>().unwrap()
|
||||
};
|
||||
let observation_time = format!(
|
||||
"{}-{}-{}T{}:{}:00Z",
|
||||
"{:04}-{:02}-{:02}T{:02}:{:02}:00Z",
|
||||
observation_time_year,
|
||||
observation_time_month,
|
||||
observation_time_day,
|
||||
observation_time_hour,
|
||||
observation_time_minute
|
||||
);
|
||||
metar.observation_time =
|
||||
chrono::NaiveDateTime::parse_from_str(&observation_time, "%Y-%m-%dT%H:%M:%SZ").unwrap();
|
||||
metar.observation_time = match chrono::DateTime::parse_from_rfc3339(&observation_time) {
|
||||
Ok(datetime) => datetime.with_timezone(&Utc),
|
||||
Err(err) => return Err(err.into()),
|
||||
};
|
||||
|
||||
loop {
|
||||
if metar_parts.is_empty() {
|
||||
@@ -410,9 +442,10 @@ impl Metar {
|
||||
} else {
|
||||
let rvr_variable_parts: Vec<&str> = rvr_parts[1].split("V").collect();
|
||||
if rvr_variable_parts.len() != 2 {
|
||||
warn!(
|
||||
log::warn!(
|
||||
"Unable to parse runway visual range in {}: {}",
|
||||
rvr_string, metar_string
|
||||
rvr_string,
|
||||
metar_string
|
||||
);
|
||||
} else {
|
||||
rvr.variable_visibility_low_ft = Some(rvr_variable_parts[0].to_string());
|
||||
@@ -468,9 +501,10 @@ impl Metar {
|
||||
sky_condition.cloud_base_ft_agl = match cloud_base_ft_agl.parse::<i32>() {
|
||||
Ok(c) => Some(c * 100),
|
||||
Err(err) => {
|
||||
warn!(
|
||||
log::warn!(
|
||||
"Unable to parse cloud base in {}: {}",
|
||||
sky_condition_string, err
|
||||
sky_condition_string,
|
||||
err
|
||||
);
|
||||
None
|
||||
}
|
||||
@@ -509,7 +543,7 @@ impl Metar {
|
||||
metar.temp_c = match temp_c.parse::<f64>() {
|
||||
Ok(t) => Some(t),
|
||||
Err(err) => {
|
||||
warn!("Unable to parse temperature in {}: {}", temp_c, err);
|
||||
log::warn!("Unable to parse temperature in {}: {}", temp_c, err);
|
||||
None
|
||||
}
|
||||
};
|
||||
@@ -520,7 +554,7 @@ impl Metar {
|
||||
metar.dewpoint_c = match dewpoint_c.parse::<f64>() {
|
||||
Ok(d) => Some(d),
|
||||
Err(err) => {
|
||||
warn!("Unable to parse dewpoint in {}: {}", dewpoint_c, err);
|
||||
log::warn!("Unable to parse dewpoint in {}: {}", dewpoint_c, err);
|
||||
None
|
||||
}
|
||||
};
|
||||
@@ -601,9 +635,10 @@ impl Metar {
|
||||
|
||||
// Skip unexpected fields
|
||||
if !metar_parts.is_empty() {
|
||||
warn!(
|
||||
log::warn!(
|
||||
"Skipping unexpected field: '{}' ({})",
|
||||
metar_parts[0], metar_string
|
||||
metar_parts[0],
|
||||
metar_string
|
||||
);
|
||||
metar_parts.remove(0);
|
||||
}
|
||||
@@ -650,12 +685,10 @@ impl Metar {
|
||||
}
|
||||
}
|
||||
|
||||
metars.push(metar);
|
||||
}
|
||||
return Ok(metars);
|
||||
Ok(metar)
|
||||
}
|
||||
|
||||
fn get_missing_metar_icaos(db_metars: &Vec<Self>, station_icaos: &Vec<&str>) -> Vec<String> {
|
||||
fn get_missing_metar_icaos(db_metars: &Vec<Self>, station_icaos: &[&str]) -> Vec<String> {
|
||||
let mut missing_metar_icaos: Vec<String> = vec![];
|
||||
let current_time = chrono::Local::now().naive_local().and_utc().timestamp();
|
||||
let db_metars_set: HashSet<&str> = db_metars
|
||||
@@ -667,15 +700,15 @@ impl Metar {
|
||||
missing_metar_icaos.push(difference.to_string());
|
||||
}
|
||||
for metar in db_metars {
|
||||
if current_time > (metar.observation_time.and_utc().timestamp() + 3600) {
|
||||
trace!("{} METAR data is outdated", metar.station_id);
|
||||
if current_time > (metar.observation_time.timestamp() + 3600) {
|
||||
log::trace!("{} METAR data is outdated", metar.station_id);
|
||||
missing_metar_icaos.push(metar.station_id.to_string());
|
||||
}
|
||||
}
|
||||
return missing_metar_icaos;
|
||||
missing_metar_icaos
|
||||
}
|
||||
|
||||
async fn get_remote_metars(icaos: Vec<String>) -> ApiResult<Vec<Metar>> {
|
||||
async fn get_remote_metars(icaos: &[&str]) -> ApiResult<Vec<Metar>> {
|
||||
let gov_api_url = std::env::var("GOV_API_URL").expect("GOV_API_URL must be set");
|
||||
// Query the remote API for the missing METAR data 10 at a time
|
||||
let icao_chunks = icaos
|
||||
@@ -689,7 +722,7 @@ impl Metar {
|
||||
Ok(r) => {
|
||||
// Check if the status code is 200
|
||||
if r.status() != 200 {
|
||||
return Err(ApiError::new(
|
||||
return Err(Error::new(
|
||||
500,
|
||||
format!("Unable to get METAR request: {}", r.status()),
|
||||
));
|
||||
@@ -701,13 +734,13 @@ impl Metar {
|
||||
.split("\n")
|
||||
.filter(|m| !m.trim().is_empty())
|
||||
.collect();
|
||||
match Metar::parse(metar_chunk) {
|
||||
match Self::parse_multiple(&metar_chunk) {
|
||||
Ok(m) => m,
|
||||
Err(err) => return Err(err),
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
return Err(ApiError::new(
|
||||
return Err(Error::new(
|
||||
500,
|
||||
format!("Unable to parse METAR request: {}", err),
|
||||
))
|
||||
@@ -715,7 +748,7 @@ impl Metar {
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
return Err(ApiError::new(
|
||||
return Err(Error::new(
|
||||
500,
|
||||
format!("Unable to get METAR request: {}", err),
|
||||
))
|
||||
@@ -723,166 +756,100 @@ impl Metar {
|
||||
};
|
||||
metars.append(&mut m);
|
||||
}
|
||||
return Ok(metars);
|
||||
}
|
||||
|
||||
fn from_query(query_metars: Vec<QueryMetar>) -> Vec<Self> {
|
||||
let mut metars: Vec<Metar> = vec![];
|
||||
for metar in query_metars {
|
||||
let mut metar: Metar = serde_json::from_value(metar.data).unwrap();
|
||||
metar.raw_text = metar.raw_text.to_string();
|
||||
metar.station_id = metar.station_id.to_string();
|
||||
metars.push(metar);
|
||||
}
|
||||
return metars;
|
||||
}
|
||||
|
||||
fn to_insert(metars: &Vec<Self>) -> Vec<InsertMetar> {
|
||||
let mut insert_metars: Vec<InsertMetar> = vec![];
|
||||
for metar in metars {
|
||||
insert_metars.push(InsertMetar {
|
||||
icao: metar.station_id.to_string(),
|
||||
observation_time: metar.observation_time,
|
||||
raw_text: metar.raw_text.to_string(),
|
||||
data: serde_json::to_value(metar).unwrap(),
|
||||
});
|
||||
}
|
||||
return insert_metars;
|
||||
}
|
||||
|
||||
pub async fn get_all(icao_string: String) -> ApiResult<Vec<Self>> {
|
||||
if icao_string.is_empty() {
|
||||
return Ok(vec![]);
|
||||
}
|
||||
|
||||
let icaos: Vec<&str> = icao_string.split(",").collect();
|
||||
|
||||
let mut db_metars = match QueryMetar::get_all(&icaos) {
|
||||
Ok(m) => Self::from_query(m),
|
||||
Err(err) => return Err(err),
|
||||
};
|
||||
|
||||
let missing_icaos = Self::get_missing_metar_icaos(&db_metars, &icaos);
|
||||
if missing_icaos.is_empty() {
|
||||
return Ok(db_metars);
|
||||
}
|
||||
trace!("Retrieving missing METAR data for {:?}", missing_icaos);
|
||||
let missing_icaos_string: Vec<String> = missing_icaos
|
||||
.iter()
|
||||
.map(|icao| format!("{}", icao.to_string()))
|
||||
.collect();
|
||||
let mut airports: Vec<QueryAirport> = vec![];
|
||||
missing_icaos_string
|
||||
.clone()
|
||||
.iter()
|
||||
.for_each(|icao| match QueryAirport::get(icao) {
|
||||
Ok(a) => airports.push(a),
|
||||
Err(_) => {}
|
||||
});
|
||||
let missing_result = Self::get_remote_metars(missing_icaos_string).await;
|
||||
let mut missing_metars = match missing_result {
|
||||
Ok(m) => m,
|
||||
Err(err) => {
|
||||
warn!("Unable to get remote METAR data; {}", err);
|
||||
vec![]
|
||||
}
|
||||
};
|
||||
if missing_metars.len() > 0 {
|
||||
let insert_metars = Self::to_insert(&missing_metars);
|
||||
match InsertMetar::insert(&insert_metars) {
|
||||
Ok(rows) => trace!("Inserted {} metar rows", rows),
|
||||
Err(err) => warn!("Unable to insert metar data; {}", err),
|
||||
};
|
||||
// Update airports with the appropriate has_metar flag
|
||||
airports.iter().for_each(|airport| {
|
||||
if missing_metars
|
||||
.iter()
|
||||
.any(|metar| metar.station_id == airport.icao)
|
||||
{
|
||||
let updated = QueryAirport {
|
||||
icao: airport.icao.to_string(),
|
||||
category: airport.category.to_string(),
|
||||
name: airport.name.to_string(),
|
||||
elevation_ft: airport.elevation_ft,
|
||||
iso_country: airport.iso_country.to_string(),
|
||||
iso_region: airport.iso_region.to_string(),
|
||||
municipality: airport.municipality.to_string(),
|
||||
has_metar: true,
|
||||
point: airport.point,
|
||||
data: airport.data.to_owned(),
|
||||
};
|
||||
match QueryAirport::update(updated) {
|
||||
Ok(_) => {}
|
||||
Err(err) => warn!("Unable to update airport with has_metar flag; {}", err),
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
let mut metars: Vec<Metar> = vec![];
|
||||
metars.append(&mut missing_metars);
|
||||
metars.append(&mut db_metars);
|
||||
Ok(metars)
|
||||
}
|
||||
|
||||
fn from_db(metar_db: MetarDb) -> ApiResult<Metar> {
|
||||
let metar: Metar = serde_json::from_value(metar_db.data)?;
|
||||
Ok(metar)
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, AsChangeset, Insertable)]
|
||||
#[diesel(table_name = metars)]
|
||||
struct InsertMetar {
|
||||
icao: String,
|
||||
observation_time: chrono::NaiveDateTime,
|
||||
raw_text: String,
|
||||
data: serde_json::Value,
|
||||
}
|
||||
|
||||
impl InsertMetar {
|
||||
fn insert(metars: &Vec<Self>) -> ApiResult<usize> {
|
||||
let mut conn = db::connection()?;
|
||||
match diesel::insert_into(metars::table)
|
||||
.values(metars)
|
||||
.execute(&mut conn)
|
||||
{
|
||||
Ok(rows) => Ok(rows),
|
||||
Err(err) => Err(ApiError {
|
||||
status: 500,
|
||||
message: format!("{}", err),
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Queryable, QueryableByName)]
|
||||
#[diesel(table_name = metars)]
|
||||
struct QueryMetar {
|
||||
id: i32,
|
||||
icao: String,
|
||||
observation_time: chrono::NaiveDateTime,
|
||||
raw_text: String,
|
||||
data: serde_json::Value,
|
||||
}
|
||||
|
||||
impl QueryMetar {
|
||||
fn get_all(icaos: &Vec<&str>) -> ApiResult<Vec<QueryMetar>> {
|
||||
// Sanitize search to only allow [a-zA-Z0-9]
|
||||
let icaos = icaos
|
||||
.iter()
|
||||
.map(|icao| {
|
||||
icao
|
||||
.chars()
|
||||
.filter(|c| c.is_alphanumeric())
|
||||
.collect::<String>()
|
||||
fn to_db(&self) -> ApiResult<MetarDb> {
|
||||
let data = serde_json::to_value(self)?;
|
||||
Ok(MetarDb {
|
||||
icao: self.station_id.clone(),
|
||||
observation_time: self.observation_time,
|
||||
raw_text: self.raw_text.clone(),
|
||||
data,
|
||||
})
|
||||
.collect::<Vec<String>>();
|
||||
let station_query: Vec<String> = icaos
|
||||
.iter()
|
||||
.map(|icao| format!("'{}'", icao.to_string()))
|
||||
.collect();
|
||||
let mut conn = db::connection()?;
|
||||
let db_metars: Vec<Self> = match sql_query(
|
||||
format!("SELECT DISTINCT ON (icao) * FROM metars WHERE icao IN ({}) ORDER BY icao, observation_time DESC", station_query.join(","))
|
||||
).load(&mut conn) {
|
||||
}
|
||||
|
||||
pub async fn find_all(icao_list: &[&str]) -> ApiResult<Vec<Self>> {
|
||||
if icao_list.is_empty() {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
|
||||
let pool = db::pool();
|
||||
let metar_dbs: Vec<MetarDb> = match sqlx::query_as::<_, MetarDb>(&format!(
|
||||
r#"
|
||||
SELECT DISTINCT ON (icao) * FROM {} WHERE icao = ANY($1) ORDER BY icao, observation_time DESC
|
||||
"#,
|
||||
TABLE_NAME
|
||||
))
|
||||
.bind(icao_list)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
{
|
||||
Ok(m) => m,
|
||||
Err(err) => return Err(ApiError { status: 500, message: format!("{}", err) })
|
||||
Err(err) => {
|
||||
return Err(Error::new(
|
||||
500,
|
||||
format!("Unable to find METARs with input {:?}: {}", icao_list, err),
|
||||
));
|
||||
}
|
||||
};
|
||||
return Ok(db_metars);
|
||||
let mut metars: Vec<Metar> = metar_dbs
|
||||
.into_iter()
|
||||
.filter_map(|metar_db| Metar::from_db(metar_db).ok())
|
||||
.collect();
|
||||
|
||||
// Check for missing metars
|
||||
let missing_icao_list = Self::get_missing_metar_icaos(&metars, icao_list);
|
||||
if !missing_icao_list.is_empty() {
|
||||
log::trace!("Retrieving missing METAR data for {:?}", missing_icao_list);
|
||||
|
||||
let missing_icao_list: Vec<&str> = missing_icao_list.iter().map(|s| s.as_str()).collect();
|
||||
let mut missing_icao_list = Self::get_remote_metars(&missing_icao_list)
|
||||
.await
|
||||
.unwrap_or_else(|err| {
|
||||
log::warn!("Unable to get remote METAR data; {}", err);
|
||||
vec![]
|
||||
});
|
||||
|
||||
if missing_icao_list.len() > 0 {
|
||||
// Insert missing METARs
|
||||
for missing_metar in &missing_icao_list {
|
||||
missing_metar.insert().await?;
|
||||
}
|
||||
metars.append(&mut missing_icao_list)
|
||||
}
|
||||
}
|
||||
|
||||
Ok(metars)
|
||||
}
|
||||
|
||||
pub async fn insert(&self) -> ApiResult<()> {
|
||||
let pool = db::pool();
|
||||
let metar: MetarDb = self.to_db()?;
|
||||
sqlx::query(&format!(
|
||||
r#"
|
||||
INSERT INTO {} (
|
||||
icao,
|
||||
observation_time,
|
||||
raw_text,
|
||||
data
|
||||
)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
"#,
|
||||
TABLE_NAME,
|
||||
))
|
||||
.bind(metar.icao)
|
||||
.bind(metar.observation_time)
|
||||
.bind(metar.raw_text)
|
||||
.bind(metar.data)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,36 +1,25 @@
|
||||
use crate::{error::ApiError, db::Metadata};
|
||||
use crate::error::Error;
|
||||
use crate::metars::Metar;
|
||||
use actix_web::{get, web, HttpResponse, HttpRequest};
|
||||
use log::error;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct MetarsResponse {
|
||||
pub data: Vec<Metar>,
|
||||
pub meta: Metadata,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct GetAllParameters {
|
||||
struct FindAllParameters {
|
||||
icaos: Option<String>,
|
||||
}
|
||||
|
||||
#[get("metars")]
|
||||
async fn get_all(req: HttpRequest) -> HttpResponse {
|
||||
let params = web::Query::<GetAllParameters>::from_query(req.query_string()).unwrap();
|
||||
let icao_option = params.icaos.clone();
|
||||
async fn find_all(req: HttpRequest) -> HttpResponse {
|
||||
let parameters = web::Query::<FindAllParameters>::from_query(req.query_string()).unwrap();
|
||||
let icao_option = ¶meters.icaos;
|
||||
let icao_string = match icao_option {
|
||||
Some(i) => i,
|
||||
None => return HttpResponse::UnprocessableEntity().body("Missing icaos parameter"),
|
||||
};
|
||||
let icaos: Vec<&str> = icao_string.split(',').collect();
|
||||
|
||||
let metars =
|
||||
match web::block(|| Ok::<_, ApiError>(async { Metar::get_all(icao_string).await }))
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.await
|
||||
{
|
||||
let metars = match Metar::find_all(&icaos).await {
|
||||
Ok(a) => a,
|
||||
Err(err) => {
|
||||
error!("{}", err);
|
||||
@@ -41,5 +30,5 @@ async fn get_all(req: HttpRequest) -> HttpResponse {
|
||||
}
|
||||
|
||||
pub fn init_routes(config: &mut web::ServiceConfig) {
|
||||
config.service(get_all);
|
||||
config.service(find_all);
|
||||
}
|
||||
|
||||
@@ -1,74 +1,74 @@
|
||||
use tokio::time::{sleep, Duration};
|
||||
|
||||
use crate::airports::{QueryAirport, QueryFilters};
|
||||
// use crate::airports::{AirportDb, AirportFilter};
|
||||
use crate::metars::Metar;
|
||||
|
||||
pub fn update_airports() {
|
||||
tokio::spawn(async {
|
||||
let mut airports: Vec<QueryAirport> = vec![];
|
||||
let limit = 100;
|
||||
loop {
|
||||
log::debug!("METAR update start");
|
||||
let total = match QueryAirport::get_count(&QueryFilters::default()) {
|
||||
Ok(t) => t,
|
||||
Err(err) => {
|
||||
log::warn!("{}", err);
|
||||
break;
|
||||
}
|
||||
};
|
||||
if total != airports.len() as i64 {
|
||||
log::debug!("{} cached airports, expected {}", airports.len(), total);
|
||||
airports = vec![];
|
||||
let pages = ((total as f32) / (if limit <= 0 { 1 } else { limit } as f32)).ceil() as i32;
|
||||
for page in 1..(pages + 1) {
|
||||
match QueryAirport::get_all(&QueryFilters::default(), limit, page) {
|
||||
Ok(mut a) => airports.append(&mut a),
|
||||
Err(err) => {
|
||||
log::warn!("{}", err);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
log::debug!("Updating {} airport METARS", airports.len());
|
||||
|
||||
let airport_icaos: Vec<String> = airports.iter().map(|a| a.icao.to_string()).collect();
|
||||
let mut peekable = airport_icaos.into_iter().peekable();
|
||||
let mut observation_time = chrono::Utc::now().timestamp();
|
||||
|
||||
if peekable.peek().is_none() {
|
||||
log::debug!("No airports to update, sleeping for 1 hour");
|
||||
sleep(Duration::from_secs(3600)).await;
|
||||
continue;
|
||||
}
|
||||
|
||||
while peekable.peek().is_some() {
|
||||
let chunk: Vec<String> = peekable.by_ref().take(limit as usize).collect();
|
||||
let icao_string = chunk.join(",");
|
||||
log::trace!("Updating METARS for: {}", icao_string);
|
||||
match Metar::get_all(icao_string).await {
|
||||
Ok(metars) => {
|
||||
// Find the oldest observation time
|
||||
for metar in metars {
|
||||
if metar.observation_time.and_utc().timestamp() < observation_time {
|
||||
observation_time = metar.observation_time.and_utc().timestamp();
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
log::warn!("{}", err);
|
||||
}
|
||||
}
|
||||
// Sleep for 100ms between chunks to avoid rate limiting
|
||||
sleep(Duration::from_millis(100)).await;
|
||||
}
|
||||
log::debug!("METAR update complete");
|
||||
// Sleep until the earliest observation time is 1 hour old
|
||||
// Bounded by 1 and 3600 seconds
|
||||
let now = chrono::Utc::now().timestamp();
|
||||
let sleep_time = std::cmp::min(std::cmp::max(1, now - (observation_time + 3600)), 3600);
|
||||
log::debug!("Next update in {} seconds", sleep_time);
|
||||
sleep(Duration::from_secs(sleep_time as u64)).await;
|
||||
}
|
||||
});
|
||||
// tokio::spawn(async {
|
||||
// let mut airports: Vec<AirportDb> = vec![];
|
||||
// let limit = 100;
|
||||
// loop {
|
||||
// log::debug!("METAR update start");
|
||||
// let total = match AirportDb::count(&AirportFilter::default()).await {
|
||||
// Ok(t) => t,
|
||||
// Err(err) => {
|
||||
// log::warn!("{}", err);
|
||||
// break;
|
||||
// }
|
||||
// };
|
||||
// if total != airports.len() as i64 {
|
||||
// log::debug!("{} cached airports, expected {}", airports.len(), total);
|
||||
// airports = vec![];
|
||||
// let pages = ((total as f32) / (if limit <= 0 { 1 } else { limit } as f32)).ceil() as i32;
|
||||
// for page in 1..(pages + 1) {
|
||||
// match AirportDb::find_all(&AirportFilter::default(), limit, page).await {
|
||||
// Ok(mut a) => airports.append(&mut a),
|
||||
// Err(err) => {
|
||||
// log::warn!("{}", err);
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// log::debug!("Updating {} airport METARS", airports.len());
|
||||
//
|
||||
// let airport_icaos: Vec<String> = airports.iter().map(|a| a.icao.to_string()).collect();
|
||||
// let mut peekable = airport_icaos.into_iter().peekable();
|
||||
// let mut observation_time = chrono::Utc::now().timestamp();
|
||||
//
|
||||
// if peekable.peek().is_none() {
|
||||
// log::debug!("No airports to update, sleeping for 1 hour");
|
||||
// sleep(Duration::from_secs(3600)).await;
|
||||
// continue;
|
||||
// }
|
||||
//
|
||||
// while peekable.peek().is_some() {
|
||||
// let chunk: Vec<String> = peekable.by_ref().take(limit as usize).collect();
|
||||
// let icao_string = chunk.join(",");
|
||||
// log::warn!("Updating METARS for: {}", &icao_string); // TODO: back to trace after
|
||||
// match Metar::find_all(&[&icao_string]).await {
|
||||
// Ok(metars) => {
|
||||
// // Find the oldest observation time
|
||||
// for metar in metars {
|
||||
// if metar.observation_time.timestamp() < observation_time {
|
||||
// observation_time = metar.observation_time.timestamp();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// Err(err) => {
|
||||
// log::warn!("{}", err);
|
||||
// }
|
||||
// }
|
||||
// // Sleep for 100ms between chunks to avoid rate limiting
|
||||
// sleep(Duration::from_millis(100)).await;
|
||||
// }
|
||||
// log::debug!("METAR update complete");
|
||||
// // Sleep until the earliest observation time is 1 hour old
|
||||
// // Bounded by 1 and 3600 seconds
|
||||
// let now = chrono::Utc::now().timestamp();
|
||||
// let sleep_time = std::cmp::min(std::cmp::max(1, now - (observation_time + 3600)), 3600);
|
||||
// log::debug!("Next update in {} seconds", sleep_time);
|
||||
// sleep(Duration::from_secs(sleep_time as u64)).await;
|
||||
// }
|
||||
// });
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use diesel::prelude::*;
|
||||
|
||||
use crate::{
|
||||
auth::hash,
|
||||
db::{connection, schema::users},
|
||||
error::ApiResult,
|
||||
};
|
||||
use crate::{auth::hash, error::ApiResult};
|
||||
use crate::db;
|
||||
|
||||
pub const ADMIN_ROLE: &str = "ADMIN";
|
||||
pub const USER_ROLE: &str = "USER";
|
||||
const TABLE_NAME: &str = "users";
|
||||
|
||||
/**
|
||||
* RegisterRequest
|
||||
@@ -20,18 +21,15 @@ pub struct RegisterRequest {
|
||||
|
||||
impl RegisterRequest {
|
||||
pub fn to_user(self) -> ApiResult<User> {
|
||||
let hash = hash(&self.password)?;
|
||||
let password_hash = hash(&self.password)?;
|
||||
Ok(User {
|
||||
email: self.email.to_lowercase(),
|
||||
hash,
|
||||
role: "user".to_string(),
|
||||
password_hash,
|
||||
role: USER_ROLE.to_string(),
|
||||
first_name: self.first_name,
|
||||
last_name: self.last_name,
|
||||
updated_at: chrono::Utc::now().naive_utc(),
|
||||
created_at: chrono::Utc::now().naive_utc(),
|
||||
profile_picture: None,
|
||||
favorites: vec![],
|
||||
verified: false,
|
||||
updated_at: Utc::now(),
|
||||
created_at: Utc::now(),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -54,7 +52,6 @@ pub struct UserResponse {
|
||||
pub role: String,
|
||||
pub first_name: String,
|
||||
pub last_name: String,
|
||||
pub profile_picture: Option<String>,
|
||||
}
|
||||
|
||||
impl From<User> for UserResponse {
|
||||
@@ -64,45 +61,69 @@ impl From<User> for UserResponse {
|
||||
role: user.role,
|
||||
first_name: user.first_name,
|
||||
last_name: user.last_name,
|
||||
profile_picture: user.profile_picture,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* User
|
||||
*/
|
||||
#[derive(Debug, Insertable, AsChangeset, Queryable, QueryableByName, Serialize, Deserialize)]
|
||||
#[diesel(table_name = users)]
|
||||
#[derive(Serialize, Deserialize, sqlx::FromRow, Debug)]
|
||||
pub struct User {
|
||||
pub email: String,
|
||||
pub hash: String,
|
||||
pub password_hash: String,
|
||||
pub role: String,
|
||||
pub first_name: String,
|
||||
pub last_name: String,
|
||||
pub updated_at: chrono::NaiveDateTime,
|
||||
pub created_at: chrono::NaiveDateTime,
|
||||
pub profile_picture: Option<String>,
|
||||
pub favorites: Vec<String>,
|
||||
pub verified: bool,
|
||||
pub updated_at: DateTime<Utc>,
|
||||
pub created_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
impl User {
|
||||
pub fn get_by_email(email: &str) -> ApiResult<User> {
|
||||
let mut conn = connection()?;
|
||||
// Check if the user exists by email, case insensitive
|
||||
pub async fn select(email: &str) -> Option<Self> {
|
||||
let pool = db::pool();
|
||||
let user: Option<Self> = sqlx::query_as::<_, Self>(&format!(
|
||||
r#"
|
||||
SELECT * FROM {} WHERE email = LOWER($1)
|
||||
"#,
|
||||
TABLE_NAME
|
||||
))
|
||||
.bind(email)
|
||||
.fetch_optional(pool)
|
||||
.await
|
||||
.unwrap_or_else(|err| {
|
||||
log::error!("Unable to find user '{}': {}", email, err);
|
||||
None
|
||||
});
|
||||
|
||||
let user = users::table
|
||||
.filter(users::email.eq(email.to_lowercase()))
|
||||
.first(&mut conn)?;
|
||||
Ok(user)
|
||||
user
|
||||
}
|
||||
|
||||
pub fn insert(user: Self) -> ApiResult<User> {
|
||||
let mut conn = connection()?;
|
||||
let user = diesel::insert_into(users::table)
|
||||
.values(user)
|
||||
.get_result(&mut conn)?;
|
||||
pub async fn insert(&self) -> ApiResult<User> {
|
||||
let pool = db::pool();
|
||||
let user: User = sqlx::query_as::<_, Self>(&format!(
|
||||
r#"
|
||||
INSERT INTO {} (
|
||||
email,
|
||||
password_hash,
|
||||
role,
|
||||
first_name,
|
||||
last_name,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING *
|
||||
"#,
|
||||
TABLE_NAME,
|
||||
))
|
||||
.bind(&self.email)
|
||||
.bind(&self.password_hash)
|
||||
.bind(&self.role)
|
||||
.bind(&self.first_name)
|
||||
.bind(&self.last_name)
|
||||
.bind(self.created_at)
|
||||
.bind(self.updated_at)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
|
||||
Ok(user)
|
||||
}
|
||||
}
|
||||
|
||||
11
bruno/Airports/Delete Airport.bru
Normal file
@@ -0,0 +1,11 @@
|
||||
meta {
|
||||
name: Delete Airport
|
||||
type: http
|
||||
seq: 4
|
||||
}
|
||||
|
||||
delete {
|
||||
url: {{BASE_URL}}/airports/TEST
|
||||
body: none
|
||||
auth: none
|
||||
}
|
||||
11
bruno/Airports/Delete All Airports.bru
Normal file
@@ -0,0 +1,11 @@
|
||||
meta {
|
||||
name: Delete All Airports
|
||||
type: http
|
||||
seq: 5
|
||||
}
|
||||
|
||||
delete {
|
||||
url: {{BASE_URL}}/airports
|
||||
body: none
|
||||
auth: none
|
||||
}
|
||||
11
bruno/Airports/Get Airport.bru
Normal file
@@ -0,0 +1,11 @@
|
||||
meta {
|
||||
name: Get Airport
|
||||
type: http
|
||||
seq: 2
|
||||
}
|
||||
|
||||
get {
|
||||
url: {{BASE_URL}}/airports/TEST
|
||||
body: none
|
||||
auth: none
|
||||
}
|
||||
11
bruno/Airports/Get All Airports.bru
Normal file
@@ -0,0 +1,11 @@
|
||||
meta {
|
||||
name: Get All Airports
|
||||
type: http
|
||||
seq: 3
|
||||
}
|
||||
|
||||
get {
|
||||
url: {{BASE_URL}}/airports
|
||||
body: none
|
||||
auth: none
|
||||
}
|
||||
28
bruno/Airports/Insert Airport.bru
Normal 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
|
||||
}
|
||||
}
|
||||
15
bruno/Metars/Find Metars.bru
Normal 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
|
||||
}
|
||||
23
bruno/Users/Create API Key.bru
Normal 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
@@ -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
@@ -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
@@ -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
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"version": "1",
|
||||
"name": "Aviation",
|
||||
"type": "collection",
|
||||
"ignore": [
|
||||
"node_modules",
|
||||
".git"
|
||||
]
|
||||
}
|
||||
3
bruno/collection.bru
Normal file
@@ -0,0 +1,3 @@
|
||||
vars:pre-request {
|
||||
BASE_URL: http://localhost:5000
|
||||
}
|
||||
104106
data/airports_2024-09-04.json
Normal file
@@ -6,19 +6,19 @@ x-env_file: &env
|
||||
|
||||
name: aviation
|
||||
services:
|
||||
db:
|
||||
image: postgis/postgis:latest
|
||||
container_name: aviation-db
|
||||
postgres:
|
||||
image: postgis/postgis:17-3.5
|
||||
container_name: aviation-postgres
|
||||
env_file: *env
|
||||
environment:
|
||||
POSTGRES_USER: ${DATABASE_USER}
|
||||
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
|
||||
POSTGRES_DB: ${DATABASE_NAME}
|
||||
POSTGRES_USER: ${POSTGRES_USER}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
|
||||
POSTGRES_DB: ${POSTGRES_NAME}
|
||||
volumes:
|
||||
- db:/var/lib/postgresql/data
|
||||
- db_logs:/var/log
|
||||
- postgres:/var/lib/postgresql/data
|
||||
- postgres_logs:/var/log
|
||||
ports:
|
||||
- "${DATABASE_PORT:-5432}:5432"
|
||||
- "${POSTGRES_PORT:-5432}:5432"
|
||||
networks:
|
||||
- backend
|
||||
profiles:
|
||||
@@ -26,7 +26,7 @@ services:
|
||||
restart: unless-stopped
|
||||
|
||||
redis:
|
||||
image: redis:latest
|
||||
image: redis:8.0-M03 # Replace with valkey?
|
||||
container_name: aviation-redis
|
||||
volumes:
|
||||
- redis:/data
|
||||
@@ -39,7 +39,7 @@ services:
|
||||
restart: unless-stopped
|
||||
|
||||
minio:
|
||||
image: minio/minio
|
||||
image: minio/minio:RELEASE.2025-02-28T09-55-16Z
|
||||
container_name: aviation-minio
|
||||
environment:
|
||||
MINIO_ROOT_USER: ${MINIO_ROOT_USER}
|
||||
@@ -64,7 +64,7 @@ services:
|
||||
build:
|
||||
context: api
|
||||
depends_on:
|
||||
- db
|
||||
- postgres
|
||||
- redis
|
||||
- minio
|
||||
networks:
|
||||
@@ -96,8 +96,8 @@ services:
|
||||
restart: unless-stopped
|
||||
|
||||
volumes:
|
||||
db:
|
||||
db_logs:
|
||||
postgres:
|
||||
postgres_logs:
|
||||
redis:
|
||||
minio:
|
||||
|
||||
|
||||
8
ui-old/.prettierrc.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"trailingComma": "none",
|
||||
"tabWidth": 2,
|
||||
"semi": true,
|
||||
"singleQuote": true,
|
||||
"jsxSingleQuote": true,
|
||||
"printWidth": 120
|
||||
}
|
||||
39
ui-old/Dockerfile
Normal 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"]
|
||||
0
ui/next-env.d.ts → ui-old/next-env.d.ts
vendored
5445
ui-old/package-lock.json
generated
Normal file
47
ui-old/package.json
Normal 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"
|
||||
}
|
||||
}
|
||||
|
Before Width: | Height: | Size: 25 KiB After Width: | Height: | Size: 25 KiB |
|
Before Width: | Height: | Size: 871 B After Width: | Height: | Size: 871 B |
|
Before Width: | Height: | Size: 874 B After Width: | Height: | Size: 874 B |
|
Before Width: | Height: | Size: 902 B After Width: | Height: | Size: 902 B |
|
Before Width: | Height: | Size: 624 B After Width: | Height: | Size: 624 B |
|
Before Width: | Height: | Size: 904 B After Width: | Height: | Size: 904 B |
|
Before Width: | Height: | Size: 695 B After Width: | Height: | Size: 695 B |
|
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.2 KiB |
|
Before Width: | Height: | Size: 696 B After Width: | Height: | Size: 696 B |
|
Before Width: | Height: | Size: 2.5 KiB After Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.4 KiB |
|
Before Width: | Height: | Size: 618 B After Width: | Height: | Size: 618 B |
|
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
@@ -25,9 +25,7 @@ export default function RootLayout({ children }: { children: React.ReactNode })
|
||||
<ModalsProvider>
|
||||
<RecoilRootWrapper>
|
||||
<React.Suspense fallback={<Skeleton />}>
|
||||
<Loader>
|
||||
{children}
|
||||
</Loader>
|
||||
<Loader>{children}</Loader>
|
||||
</React.Suspense>
|
||||
</RecoilRootWrapper>
|
||||
</ModalsProvider>
|
||||
111
ui-old/src/components/Header/index.tsx
Normal 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}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||