Updated versions
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -38,5 +38,3 @@ yarn-error.log*
|
|||||||
|
|
||||||
target/
|
target/
|
||||||
dist/
|
dist/
|
||||||
*.env
|
|
||||||
.env.local
|
|
||||||
|
|||||||
2
Makefile
2
Makefile
@@ -4,6 +4,8 @@ SHELL := /bin/bash
|
|||||||
GIT_HASH ?= $(shell git log --format="%h" -n 1)
|
GIT_HASH ?= $(shell git log --format="%h" -n 1)
|
||||||
|
|
||||||
include .env
|
include .env
|
||||||
|
-include .env.local
|
||||||
|
export
|
||||||
|
|
||||||
.PHONY: help build start stop lint
|
.PHONY: help build start stop lint
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ DATABASE_PORT=5432
|
|||||||
REDIS_HOST=localhost
|
REDIS_HOST=localhost
|
||||||
REDIS_PORT=6379
|
REDIS_PORT=6379
|
||||||
|
|
||||||
MINIO_ROOT_USER=weather
|
MINIO_ROOT_USER=aviation
|
||||||
MINIO_ROOT_PASSWORD=
|
MINIO_ROOT_PASSWORD=
|
||||||
MINIO_HOST=localhost
|
MINIO_HOST=localhost
|
||||||
MINIO_PORT=9000
|
MINIO_PORT=9000
|
||||||
@@ -22,6 +22,6 @@ SERVICE_PORT=5000
|
|||||||
|
|
||||||
KEYS_DIR_PATH=
|
KEYS_DIR_PATH=
|
||||||
ACCESS_TOKEN_MAXAGE=5
|
ACCESS_TOKEN_MAXAGE=5
|
||||||
REFRESH_TOKEN_MAXAGE=30
|
REFRESH_TOKEN_MAXAGE=1440
|
||||||
|
|
||||||
GOV_API_URL=https://aviationweather.gov/cgi-bin/data
|
GOV_API_URL=https://aviationweather.gov/cgi-bin/data
|
||||||
1030
service/Cargo.lock
generated
1030
service/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -10,28 +10,29 @@ license = "GPL-3.0-or-later"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "4.4.0"
|
actix-web = "4.8.0"
|
||||||
actix-cors = "0.6.4"
|
actix-cors = "0.7.0"
|
||||||
actix-web-httpauth = "0.8.1"
|
actix-web-httpauth = "0.8.2"
|
||||||
actix-multipart = "0.6.1"
|
actix-multipart = "0.7.2"
|
||||||
chrono = { version = "0.4.31", features = ["serde"] }
|
chrono = { version = "0.4.38", features = ["serde"] }
|
||||||
dotenv = "0.15.0"
|
dotenv = "0.15.0"
|
||||||
diesel = { version = "2.1.2", features = ["postgres", "r2d2", "uuid", "chrono", "serde_json"] }
|
diesel = { version = "2.2.1", features = ["postgres", "r2d2", "uuid", "chrono", "serde_json"] }
|
||||||
postgis_diesel = { version = "2.2.1", features = ["serde"] }
|
postgis_diesel = { version = "2.4.0", features = ["serde"] }
|
||||||
diesel_migrations = { version = "2.1.0", features = ["postgres"] }
|
diesel_migrations = { version = "2.2.0", features = ["postgres"] }
|
||||||
env_logger = "0.10.0"
|
env_logger = "0.11.3"
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.5.0"
|
||||||
r2d2 = "0.8.10"
|
r2d2 = "0.8.10"
|
||||||
reqwest = "0.11.21"
|
reqwest = "0.12.5"
|
||||||
serde = {version = "1.0.188", features = ["derive"]}
|
serde = {version = "1.0.204", features = ["derive"]}
|
||||||
serde_json = "1.0.107"
|
serde_json = "1.0.120"
|
||||||
tokio = { version = "1.32.0", features = ["macros", "rt", "time"] }
|
tokio = { version = "1.38.0", features = ["macros", "rt", "time"] }
|
||||||
uuid = { version = "1.4.1", features = ["serde", "v4"] }
|
uuid = { version = "1.10.0", features = ["serde", "v4"] }
|
||||||
log = "0.4.20"
|
log = "0.4.22"
|
||||||
argon2 = "0.5.2"
|
argon2 = "0.5.3"
|
||||||
jsonwebtoken = "9.0.0"
|
jsonwebtoken = "9.3.0"
|
||||||
redis = { version = "0.23.3", features = ["tokio-comp", "connection-manager", "r2d2"] }
|
redis = { version = "0.25.4", features = ["tokio-comp", "connection-manager", "r2d2"] }
|
||||||
rustix = "0.38.19" # https://github.com/imsnif/bandwhich/issues/284
|
#rustix = "0.38.19" # https://github.com/imsnif/bandwhich/issues/284
|
||||||
regex = "1.10.2"
|
ahash = "0.8.11" # https://github.com/tkaitchuck/aHash/issues/200
|
||||||
futures-util = "0.3.29"
|
regex = "1.10.5"
|
||||||
rust-s3 = "0.33.0"
|
futures-util = "0.3.30"
|
||||||
|
rust-s3 = "0.34.0"
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ SHELL := /bin/bash
|
|||||||
GIT_HASH ?= $(shell git log --format="%h" -n 1)
|
GIT_HASH ?= $(shell git log --format="%h" -n 1)
|
||||||
|
|
||||||
include .env
|
include .env
|
||||||
|
-include .env.local
|
||||||
|
export
|
||||||
|
|
||||||
.PHONY: help build start stop lint
|
.PHONY: help build start stop lint
|
||||||
|
|
||||||
@@ -12,40 +14,49 @@ help: ## This info
|
|||||||
@cat Makefile | grep -E '^[a-zA-Z\/_-]+:.*?## .*$$' | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
|
@cat Makefile | grep -E '^[a-zA-Z\/_-]+:.*?## .*$$' | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
|
||||||
@echo
|
@echo
|
||||||
|
|
||||||
build: ## Build the Docker image
|
format: ## Format code
|
||||||
docker compose build
|
@echo "Formatting code..."
|
||||||
|
@cargo fmt
|
||||||
|
@echo "Format complete"
|
||||||
|
|
||||||
tag: ## Tag the Docker image
|
run: ## Run the service
|
||||||
docker tag aviation-service:latest aviation-service:${GIT_HASH}
|
@cargo run
|
||||||
|
|
||||||
utils: ## Start the utils
|
clean: ## Cleanup
|
||||||
docker compose up -d db
|
@echo "Cleaning up..."
|
||||||
docker compose up -d redis
|
@cargo clean
|
||||||
docker compose up -d minio
|
@rm -rf ../keys
|
||||||
|
@echo "Cleanup complete"
|
||||||
|
|
||||||
up: ## Start the Docker containers
|
up: ## Start the Docker containers
|
||||||
docker compose up -d
|
@docker compose --profile backend up -d
|
||||||
|
|
||||||
down: ## Stop the Docker containers
|
down: ## Stop the Docker containers
|
||||||
docker compose down
|
@docker compose --profile backend down
|
||||||
|
|
||||||
connect: ## Connect to the PSQL DB
|
connect: ## Connect to the PSQL DB
|
||||||
docker exec -it ${DATABASE_CONTAINER} psql -U postgres
|
@docker exec -it ${DATABASE_CONTAINER} psql -U postgres
|
||||||
|
|
||||||
clean: ## Cleanup Docker containers
|
docker-build: ## Build the Docker image
|
||||||
docker compose down && \
|
@docker compose build
|
||||||
docker image rm aviation-service || \
|
|
||||||
docker network rm aviation-frontend || \
|
docker-tag: ## Tag the Docker image
|
||||||
docker network rm aviation-backend
|
@docker tag aviation-service:latest aviation-service:${GIT_HASH}
|
||||||
|
|
||||||
|
docker-run: ## Start the service
|
||||||
|
@docker compose --profile service up -d
|
||||||
|
|
||||||
|
docker-clean: ## Cleanup Docker containers
|
||||||
|
@docker compose --profile backend --profile service down -v
|
||||||
|
|
||||||
clean-db: ## Remove database
|
clean-db: ## Remove database
|
||||||
docker exec -i ${DATABASE_CONTAINER} sh -c 'PGPASSWORD=${DATABASE_PASSWORD} psql -U ${DATABASE_USER} -d postgres -c "DROP DATABASE IF EXISTS \"${DATABASE_NAME}\";"'
|
@docker exec -i ${DATABASE_CONTAINER} sh -c 'PGPASSWORD=${DATABASE_PASSWORD} psql -U ${DATABASE_USER} -d postgres -c "DROP DATABASE IF EXISTS \"${DATABASE_NAME}\";"'
|
||||||
docker exec -i ${DATABASE_CONTAINER} sh -c 'PGPASSWORD=${DATABASE_PASSWORD} psql -U ${DATABASE_USER} -d postgres -c "CREATE DATABASE \"${DATABASE_NAME}\";"' || true
|
@docker exec -i ${DATABASE_CONTAINER} sh -c 'PGPASSWORD=${DATABASE_PASSWORD} psql -U ${DATABASE_USER} -d postgres -c "CREATE DATABASE \"${DATABASE_NAME}\";"' || true
|
||||||
|
|
||||||
generate: ## Generate RSA keys
|
generate-keys: ## Generate RSA keys
|
||||||
mkdir ../keys/
|
@mkdir ../keys/
|
||||||
openssl genrsa -out ../keys/access_private_key.pem 4096
|
@openssl genrsa -out ../keys/access_private_key.pem 4096
|
||||||
openssl rsa -in ../keys/access_private_key.pem -pubout -outform PEM -out ../keys/access_public_key.pem
|
@openssl rsa -in ../keys/access_private_key.pem -pubout -outform PEM -out ../keys/access_public_key.pem
|
||||||
openssl genrsa -out ../keys/refresh_private_key.pem 4096
|
@openssl genrsa -out ../keys/refresh_private_key.pem 4096
|
||||||
openssl rsa -in ../keys/refresh_private_key.pem -pubout -outform PEM -out ../keys/refresh_public_key.pem
|
@openssl rsa -in ../keys/refresh_private_key.pem -pubout -outform PEM -out ../keys/refresh_public_key.pem
|
||||||
|
|
||||||
@@ -1,12 +1,15 @@
|
|||||||
version: '3'
|
x-env_file: &env
|
||||||
|
- path: .env
|
||||||
|
required: true
|
||||||
|
- path: .env.local
|
||||||
|
required: false
|
||||||
|
|
||||||
name: aviation
|
name: aviation
|
||||||
services:
|
services:
|
||||||
db:
|
db:
|
||||||
image: postgis/postgis:latest
|
image: postgis/postgis:latest
|
||||||
container_name: aviation-db
|
container_name: aviation-db
|
||||||
env_file:
|
env_file: *env
|
||||||
- .env
|
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_USER: ${DATABASE_USER}
|
POSTGRES_USER: ${DATABASE_USER}
|
||||||
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
|
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
|
||||||
@@ -18,6 +21,8 @@ services:
|
|||||||
- "${DATABASE_PORT:-5432}:5432"
|
- "${DATABASE_PORT:-5432}:5432"
|
||||||
networks:
|
networks:
|
||||||
- backend
|
- backend
|
||||||
|
profiles:
|
||||||
|
- backend
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
redis:
|
redis:
|
||||||
image: redis:latest
|
image: redis:latest
|
||||||
@@ -28,6 +33,8 @@ services:
|
|||||||
- ${REDIS_PORT:-6379}:6379
|
- ${REDIS_PORT:-6379}:6379
|
||||||
networks:
|
networks:
|
||||||
- backend
|
- backend
|
||||||
|
profiles:
|
||||||
|
- backend
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
minio:
|
minio:
|
||||||
image: minio/minio
|
image: minio/minio
|
||||||
@@ -42,13 +49,14 @@ services:
|
|||||||
- ${MINIO_PORT_INTERNAL:-9001}:9001
|
- ${MINIO_PORT_INTERNAL:-9001}:9001
|
||||||
networks:
|
networks:
|
||||||
- backend
|
- backend
|
||||||
|
profiles:
|
||||||
|
- backend
|
||||||
command: server --console-address ":9001" /data
|
command: server --console-address ":9001" /data
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|
||||||
service:
|
service:
|
||||||
container_name: aviation-service
|
container_name: aviation-service
|
||||||
env_file:
|
env_file: *env
|
||||||
- .env
|
|
||||||
environment:
|
environment:
|
||||||
DATABASE_HOST: db
|
DATABASE_HOST: db
|
||||||
DATABASE_PORT: 5432
|
DATABASE_PORT: 5432
|
||||||
@@ -70,6 +78,8 @@ services:
|
|||||||
networks:
|
networks:
|
||||||
- frontend
|
- frontend
|
||||||
- backend
|
- backend
|
||||||
|
profiles:
|
||||||
|
- service
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
|
|||||||
3
service/rust-toolchain.toml
Normal file
3
service/rust-toolchain.toml
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
[toolchain]
|
||||||
|
channel = "stable"
|
||||||
|
components = ["rustfmt", "clippy"]
|
||||||
3
service/rustfmt.toml
Normal file
3
service/rustfmt.toml
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
indent_style = "Block"
|
||||||
|
reorder_imports = false
|
||||||
|
tab_spaces = 2
|
||||||
@@ -67,8 +67,8 @@ impl Into<QueryAirport> for Airport {
|
|||||||
error!("{}", err);
|
error!("{}", err);
|
||||||
serde_json::Value::Null
|
serde_json::Value::Null
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,7 +95,7 @@ pub enum AirportCategory {
|
|||||||
#[serde(rename = "balloonport")]
|
#[serde(rename = "balloonport")]
|
||||||
Balloonport,
|
Balloonport,
|
||||||
#[serde(rename = "unknown")]
|
#[serde(rename = "unknown")]
|
||||||
Unknown
|
Unknown,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for AirportCategory {
|
impl FromStr for AirportCategory {
|
||||||
@@ -109,7 +109,7 @@ impl FromStr for AirportCategory {
|
|||||||
"closed" => Ok(AirportCategory::Closed),
|
"closed" => Ok(AirportCategory::Closed),
|
||||||
"seaplane_base" => Ok(AirportCategory::Seaplane),
|
"seaplane_base" => Ok(AirportCategory::Seaplane),
|
||||||
"balloonport" => Ok(AirportCategory::Balloonport),
|
"balloonport" => Ok(AirportCategory::Balloonport),
|
||||||
_ => Ok(AirportCategory::Unknown)
|
_ => Ok(AirportCategory::Unknown),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -124,7 +124,7 @@ impl Display for AirportCategory {
|
|||||||
AirportCategory::Closed => write!(f, "closed"),
|
AirportCategory::Closed => write!(f, "closed"),
|
||||||
AirportCategory::Seaplane => write!(f, "seaplane_base"),
|
AirportCategory::Seaplane => write!(f, "seaplane_base"),
|
||||||
AirportCategory::Balloonport => write!(f, "balloonport"),
|
AirportCategory::Balloonport => write!(f, "balloonport"),
|
||||||
AirportCategory::Unknown => write!(f, "unknown")
|
AirportCategory::Unknown => write!(f, "unknown"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -141,7 +141,7 @@ pub struct QueryAirport {
|
|||||||
pub municipality: String,
|
pub municipality: String,
|
||||||
pub has_metar: bool,
|
pub has_metar: bool,
|
||||||
pub point: Point,
|
pub point: Point,
|
||||||
pub data: serde_json::Value
|
pub data: serde_json::Value,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -151,7 +151,8 @@ pub struct QueryFilters {
|
|||||||
pub bounds: Option<Polygon<Point>>,
|
pub bounds: Option<Polygon<Point>>,
|
||||||
pub categories: Option<Vec<AirportCategory>>,
|
pub categories: Option<Vec<AirportCategory>>,
|
||||||
pub order_field: Option<QueryOrderField>,
|
pub order_field: Option<QueryOrderField>,
|
||||||
pub order_by: Option<QueryOrderBy>
|
pub order_by: Option<QueryOrderBy>,
|
||||||
|
pub has_metar: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for QueryFilters {
|
impl Default for QueryFilters {
|
||||||
@@ -162,7 +163,8 @@ impl Default for QueryFilters {
|
|||||||
bounds: None,
|
bounds: None,
|
||||||
categories: None,
|
categories: None,
|
||||||
order_field: None,
|
order_field: None,
|
||||||
order_by: None
|
order_by: None,
|
||||||
|
has_metar: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -170,7 +172,7 @@ impl Default for QueryFilters {
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum QueryOrderBy {
|
pub enum QueryOrderBy {
|
||||||
Asc,
|
Asc,
|
||||||
Desc
|
Desc,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for QueryOrderBy {
|
impl FromStr for QueryOrderBy {
|
||||||
@@ -179,7 +181,7 @@ impl FromStr for QueryOrderBy {
|
|||||||
match s {
|
match s {
|
||||||
"asc" => Ok(QueryOrderBy::Asc),
|
"asc" => Ok(QueryOrderBy::Asc),
|
||||||
"desc" => Ok(QueryOrderBy::Desc),
|
"desc" => Ok(QueryOrderBy::Desc),
|
||||||
_ => Err(())
|
_ => Err(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -204,7 +206,7 @@ impl FromStr for QueryOrderField {
|
|||||||
"iso_country" => Ok(QueryOrderField::Country),
|
"iso_country" => Ok(QueryOrderField::Country),
|
||||||
"iso_region" => Ok(QueryOrderField::Region),
|
"iso_region" => Ok(QueryOrderField::Region),
|
||||||
"municipality" => Ok(QueryOrderField::Municipality),
|
"municipality" => Ok(QueryOrderField::Municipality),
|
||||||
_ => Err(())
|
_ => Err(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -229,7 +231,7 @@ impl QueryAirport {
|
|||||||
QueryOrderField::Municipality => format!("{}, municipality ASC", query),
|
QueryOrderField::Municipality => format!("{}, municipality ASC", query),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
},
|
}
|
||||||
QueryOrderBy::Desc => {
|
QueryOrderBy::Desc => {
|
||||||
if let Some(order_field) = &filters.order_field {
|
if let Some(order_field) = &filters.order_field {
|
||||||
query = match order_field {
|
query = match order_field {
|
||||||
@@ -249,7 +251,12 @@ impl QueryAirport {
|
|||||||
|
|
||||||
let airports: Vec<QueryAirport> = match sql_query(query).load(&mut conn) {
|
let airports: Vec<QueryAirport> = match sql_query(query).load(&mut conn) {
|
||||||
Ok(a) => a,
|
Ok(a) => a,
|
||||||
Err(err) => return Err(ServiceError { status: 500, message: format!("{}", err) })
|
Err(err) => {
|
||||||
|
return Err(ServiceError {
|
||||||
|
status: 500,
|
||||||
|
message: format!("{}", err),
|
||||||
|
})
|
||||||
|
}
|
||||||
};
|
};
|
||||||
Ok(airports)
|
Ok(airports)
|
||||||
}
|
}
|
||||||
@@ -268,12 +275,17 @@ impl QueryAirport {
|
|||||||
#[derive(Debug, Queryable, QueryableByName)]
|
#[derive(Debug, Queryable, QueryableByName)]
|
||||||
#[diesel(table_name = airports)]
|
#[diesel(table_name = airports)]
|
||||||
struct Count {
|
struct Count {
|
||||||
count: i64
|
count: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
let count: Vec<Count> = match sql_query(query).load(&mut conn) {
|
let count: Vec<Count> = match sql_query(query).load(&mut conn) {
|
||||||
Ok(a) => a,
|
Ok(a) => a,
|
||||||
Err(err) => return Err(ServiceError { status: 500, message: format!("{}", err) })
|
Err(err) => {
|
||||||
|
return Err(ServiceError {
|
||||||
|
status: 500,
|
||||||
|
message: format!("{}", err),
|
||||||
|
})
|
||||||
|
}
|
||||||
};
|
};
|
||||||
return Ok(count[0].count);
|
return Ok(count[0].count);
|
||||||
}
|
}
|
||||||
@@ -286,7 +298,10 @@ impl QueryAirport {
|
|||||||
if let Some(bounds) = &filters.bounds {
|
if let Some(bounds) = &filters.bounds {
|
||||||
// convert bounds to a WKT polygon
|
// convert bounds to a WKT polygon
|
||||||
if bounds.rings.len() > 1 {
|
if bounds.rings.len() > 1 {
|
||||||
return Err(ServiceError { status: 400, message: "Only one polygon is allowed".to_string() })
|
return Err(ServiceError {
|
||||||
|
status: 400,
|
||||||
|
message: "Only one polygon is allowed".to_string(),
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
let mut points: Vec<String> = vec![];
|
let mut points: Vec<String> = vec![];
|
||||||
bounds.rings.iter().for_each(|ring| {
|
bounds.rings.iter().for_each(|ring| {
|
||||||
@@ -295,28 +310,58 @@ impl QueryAirport {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
let bounds = format!("POLYGON(({}))", points.join(","));
|
let bounds = format!("POLYGON(({}))", points.join(","));
|
||||||
parts.push(format!("ST_Contains(ST_GeomFromText('{}', 4326), point)", bounds));
|
parts.push(format!(
|
||||||
|
"ST_Contains(ST_GeomFromText('{}', 4326), point)",
|
||||||
|
bounds
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(categories) = &filters.categories {
|
if let Some(categories) = &filters.categories {
|
||||||
parts.push(format!("({})", categories.iter().map(|category| format!("category = '{}'", category.to_string())).collect::<Vec<String>>().join(" OR ")));
|
parts.push(format!(
|
||||||
|
"({})",
|
||||||
|
categories
|
||||||
|
.iter()
|
||||||
|
.map(|category| format!("category = '{}'", category.to_string()))
|
||||||
|
.collect::<Vec<String>>()
|
||||||
|
.join(" OR ")
|
||||||
|
));
|
||||||
}
|
}
|
||||||
fn sanitize_icao(icao: &str) -> String {
|
fn sanitize_icao(icao: &str) -> String {
|
||||||
// Sanitize search to only allow [a-zA-Z0-9-\\s]
|
// Sanitize search to only allow [a-zA-Z0-9-\\s]
|
||||||
icao.chars().filter(|c| c.is_alphanumeric() || *c == '-' || *c == ' ').collect::<String>()
|
icao
|
||||||
|
.chars()
|
||||||
|
.filter(|c| c.is_alphanumeric() || *c == '-' || *c == ' ')
|
||||||
|
.collect::<String>()
|
||||||
}
|
}
|
||||||
if &filters.icaos.is_some() == &true && &filters.name.is_some() == &true {
|
if &filters.icaos.is_some() == &true && &filters.name.is_some() == &true {
|
||||||
let icaos = filters.icaos.as_ref().unwrap();
|
let icaos = filters.icaos.as_ref().unwrap();
|
||||||
let name = sanitize_icao(filters.name.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 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);
|
let name_part = format!("name ILIKE '%{}%'", name);
|
||||||
parts.push(format!("({} OR {})", icao_part, name_part));
|
parts.push(format!("({} OR {})", icao_part, name_part));
|
||||||
} else if let Some(icaos) = &filters.icaos {
|
} 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 ")));
|
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 {
|
} else if let Some(name) = &filters.name {
|
||||||
let search = sanitize_icao(name);
|
let search = sanitize_icao(name);
|
||||||
parts.push(format!("name ILIKE '%{}%'", search));
|
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 {
|
if parts.len() > 0 {
|
||||||
query = format!("{} WHERE {}", query, parts.join(" AND "));
|
query = format!("{} WHERE {}", query, parts.join(" AND "));
|
||||||
@@ -327,26 +372,32 @@ impl QueryAirport {
|
|||||||
|
|
||||||
pub fn get(icao: &str) -> Result<Self, ServiceError> {
|
pub fn get(icao: &str) -> Result<Self, ServiceError> {
|
||||||
let mut conn = db::connection()?;
|
let mut conn = db::connection()?;
|
||||||
let airport = airports::table.filter(airports::icao.eq(icao)).first(&mut conn)?;
|
let airport = airports::table
|
||||||
|
.filter(airports::icao.eq(icao))
|
||||||
|
.first(&mut conn)?;
|
||||||
Ok(airport)
|
Ok(airport)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert(airport: Self) -> Result<Self, ServiceError> {
|
pub fn insert(airport: Self) -> Result<Self, ServiceError> {
|
||||||
let mut conn: r2d2::PooledConnection<diesel::r2d2::ConnectionManager<PgConnection>> = db::connection()?;
|
let mut conn: r2d2::PooledConnection<diesel::r2d2::ConnectionManager<PgConnection>> =
|
||||||
|
db::connection()?;
|
||||||
let airport = Self::from(airport);
|
let airport = Self::from(airport);
|
||||||
let airport = diesel::insert_into(airports::table)
|
let airport = diesel::insert_into(airports::table)
|
||||||
.values(airport)
|
.values(airport)
|
||||||
|
.on_conflict_do_nothing()
|
||||||
.get_result(&mut conn)?;
|
.get_result(&mut conn)?;
|
||||||
Ok(airport)
|
Ok(airport)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert_all(airports: Vec<Self>) -> Result<Vec<Self>, ServiceError> {
|
pub fn insert_all(airports: Vec<Self>) -> Result<Vec<Self>, ServiceError> {
|
||||||
let mut conn: r2d2::PooledConnection<diesel::r2d2::ConnectionManager<PgConnection>> = db::connection()?;
|
let mut conn: r2d2::PooledConnection<diesel::r2d2::ConnectionManager<PgConnection>> =
|
||||||
|
db::connection()?;
|
||||||
let mut inserted_airports: Vec<Self> = vec![];
|
let mut inserted_airports: Vec<Self> = vec![];
|
||||||
for airport in airports {
|
for airport in airports {
|
||||||
let airport = Self::from(airport);
|
let airport = Self::from(airport);
|
||||||
let airport = diesel::insert_into(airports::table)
|
let airport = diesel::insert_into(airports::table)
|
||||||
.values(airport)
|
.values(airport)
|
||||||
|
.on_conflict_do_nothing()
|
||||||
.get_result(&mut conn)?;
|
.get_result(&mut conn)?;
|
||||||
inserted_airports.push(airport);
|
inserted_airports.push(airport);
|
||||||
}
|
}
|
||||||
@@ -365,8 +416,10 @@ impl QueryAirport {
|
|||||||
pub fn delete(icao: Option<String>) -> Result<usize, ServiceError> {
|
pub fn delete(icao: Option<String>) -> Result<usize, ServiceError> {
|
||||||
let mut conn = db::connection()?;
|
let mut conn = db::connection()?;
|
||||||
let res = match icao {
|
let res = match icao {
|
||||||
Some(icao) => diesel::delete(airports::table.filter(airports::icao.eq(icao))).execute(&mut conn)?,
|
Some(icao) => {
|
||||||
None => diesel::delete(airports::table).execute(&mut conn)?
|
diesel::delete(airports::table.filter(airports::icao.eq(icao))).execute(&mut conn)?
|
||||||
|
}
|
||||||
|
None => diesel::delete(airports::table).execute(&mut conn)?,
|
||||||
};
|
};
|
||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use futures_util::stream::StreamExt as _;
|
use futures_util::stream::StreamExt as _;
|
||||||
|
|
||||||
use crate::{airports::{QueryAirport, QueryFilters, QueryOrderField, QueryOrderBy, Airport, AirportCategory}, db::{Response, Metadata}, auth::{JwtAuth, verify_role}};
|
use crate::{
|
||||||
|
airports::{QueryAirport, QueryFilters, QueryOrderField, QueryOrderBy, Airport, AirportCategory},
|
||||||
|
db::{Response, Metadata},
|
||||||
|
auth::{JwtAuth, verify_role},
|
||||||
|
};
|
||||||
use actix_multipart::Multipart;
|
use actix_multipart::Multipart;
|
||||||
use actix_web::{delete, get, post, put, web, HttpResponse, HttpRequest, ResponseError};
|
use actix_web::{delete, get, post, put, web, HttpResponse, HttpRequest, ResponseError};
|
||||||
use log::{error, warn};
|
use log::{error, warn};
|
||||||
@@ -16,21 +20,22 @@ struct GetAllParameters {
|
|||||||
categories: Option<String>,
|
categories: Option<String>,
|
||||||
order_field: Option<String>,
|
order_field: Option<String>,
|
||||||
order_by: Option<String>,
|
order_by: Option<String>,
|
||||||
|
has_metar: Option<String>,
|
||||||
limit: Option<i32>,
|
limit: Option<i32>,
|
||||||
page: Option<i32>
|
page: Option<i32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/import")]
|
#[post("/import")]
|
||||||
async fn import(mut payload: Multipart, auth: JwtAuth) -> HttpResponse {
|
async fn import(mut payload: Multipart, auth: JwtAuth) -> HttpResponse {
|
||||||
if let Err(err) = verify_role(&auth, "admin") {
|
if let Err(err) = verify_role(&auth, "admin") {
|
||||||
return ResponseError::error_response(&err)
|
return ResponseError::error_response(&err);
|
||||||
};
|
};
|
||||||
|
|
||||||
while let Some(item) = payload.next().await {
|
while let Some(item) = payload.next().await {
|
||||||
let mut bytes = web::BytesMut::new();
|
let mut bytes = web::BytesMut::new();
|
||||||
let mut field = match item {
|
let mut field = match item {
|
||||||
Ok(field) => field,
|
Ok(field) => field,
|
||||||
Err(err) => return ResponseError::error_response(&err)
|
Err(err) => return ResponseError::error_response(&err),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Build bytes from chunks
|
// Build bytes from chunks
|
||||||
@@ -57,10 +62,10 @@ async fn import(mut payload: Multipart, auth: JwtAuth) -> HttpResponse {
|
|||||||
// Convert Vec<Airport> to Vec<QueryAirport> and insert into database
|
// Convert Vec<Airport> to Vec<QueryAirport> and insert into database
|
||||||
let query_airports: Vec<QueryAirport> = airports.into_iter().map(|a| a.into()).collect();
|
let query_airports: Vec<QueryAirport> = airports.into_iter().map(|a| a.into()).collect();
|
||||||
match QueryAirport::insert_all(query_airports) {
|
match QueryAirport::insert_all(query_airports) {
|
||||||
Ok(_) => {},
|
Ok(_) => {}
|
||||||
Err(err) => return ResponseError::error_response(&err)
|
Err(err) => return ResponseError::error_response(&err),
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
}
|
||||||
HttpResponse::Ok().finish()
|
HttpResponse::Ok().finish()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,83 +75,117 @@ async fn get_all(req: HttpRequest) -> HttpResponse {
|
|||||||
let mut filters = QueryFilters::default();
|
let mut filters = QueryFilters::default();
|
||||||
filters.icaos = match ¶ms.icaos {
|
filters.icaos = match ¶ms.icaos {
|
||||||
Some(i) => Some(i.split(",").map(|s| s.to_string()).collect()),
|
Some(i) => Some(i.split(",").map(|s| s.to_string()).collect()),
|
||||||
None => None
|
None => None,
|
||||||
};
|
};
|
||||||
filters.name = params.name.clone();
|
filters.name = params.name.clone();
|
||||||
filters.categories = match ¶ms.categories {
|
filters.categories = match ¶ms.categories {
|
||||||
Some(c) => Some(c.split(",").map(|s| AirportCategory::from_str(s).unwrap()).collect()),
|
Some(c) => Some(
|
||||||
None => None
|
c.split(",")
|
||||||
|
.map(|s| AirportCategory::from_str(s).unwrap())
|
||||||
|
.collect(),
|
||||||
|
),
|
||||||
|
None => None,
|
||||||
};
|
};
|
||||||
filters.bounds = match ¶ms.bounds {
|
filters.bounds = match ¶ms.bounds {
|
||||||
Some(b) => {
|
Some(b) => {
|
||||||
let bounds: Vec<&str> = b.split(",").collect();
|
let bounds: Vec<&str> = b.split(",").collect();
|
||||||
if bounds.len() != 4 {
|
if bounds.len() != 4 {
|
||||||
warn!("Expected 4 bounds, received {}: {}", bounds.len(), b);
|
warn!("Expected 4 bounds, received {}: {}", bounds.len(), b);
|
||||||
return HttpResponse::UnprocessableEntity().body(format!("Received {}; expected NE_LAT,NE_LON,SW_LAT,SW_LON", 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>() {
|
let ne_lat = match bounds[0].parse::<f64>() {
|
||||||
Ok(b) => b,
|
Ok(b) => b,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
warn!("{}", err);
|
warn!("{}", err);
|
||||||
return HttpResponse::UnprocessableEntity().body(format!("{}", err))
|
return HttpResponse::UnprocessableEntity().body(format!("{}", err));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let ne_lon = match bounds[1].parse::<f64>() {
|
let ne_lon = match bounds[1].parse::<f64>() {
|
||||||
Ok(b) => b,
|
Ok(b) => b,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
warn!("{}", err);
|
warn!("{}", err);
|
||||||
return HttpResponse::UnprocessableEntity().body(format!("{}", err))
|
return HttpResponse::UnprocessableEntity().body(format!("{}", err));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let sw_lat = match bounds[2].parse::<f64>() {
|
let sw_lat = match bounds[2].parse::<f64>() {
|
||||||
Ok(b) => b,
|
Ok(b) => b,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
warn!("{}", err);
|
warn!("{}", err);
|
||||||
return HttpResponse::UnprocessableEntity().body(format!("{}", err))
|
return HttpResponse::UnprocessableEntity().body(format!("{}", err));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let sw_lon = match bounds[3].parse::<f64>() {
|
let sw_lon = match bounds[3].parse::<f64>() {
|
||||||
Ok(b) => b,
|
Ok(b) => b,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
warn!("{}", err);
|
warn!("{}", err);
|
||||||
return HttpResponse::UnprocessableEntity().body(format!("{}", err))
|
return HttpResponse::UnprocessableEntity().body(format!("{}", err));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let mut polygon: Polygon<Point> = Polygon::new(Some(4326));
|
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 {
|
||||||
polygon.add_point(Point { x: ne_lon, y: sw_lat, srid: Some(4326) });
|
x: sw_lon,
|
||||||
polygon.add_point(Point { x: ne_lon, y: ne_lat, srid: Some(4326) });
|
y: sw_lat,
|
||||||
polygon.add_point(Point { x: sw_lon, y: ne_lat, srid: Some(4326) });
|
srid: 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)
|
Some(polygon)
|
||||||
},
|
}
|
||||||
None => None
|
None => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
filters.order_by = match ¶ms.order_by {
|
filters.order_by = match ¶ms.order_by {
|
||||||
Some(o) => Some(QueryOrderBy::from_str(&o).unwrap()),
|
Some(o) => Some(QueryOrderBy::from_str(&o).unwrap()),
|
||||||
None => None
|
None => None,
|
||||||
};
|
};
|
||||||
filters.order_field = match ¶ms.order_field {
|
filters.order_field = match ¶ms.order_field {
|
||||||
Some(o) => Some(QueryOrderField::from_str(&o).unwrap()),
|
Some(o) => Some(QueryOrderField::from_str(&o).unwrap()),
|
||||||
None => None
|
None => None,
|
||||||
|
};
|
||||||
|
filters.has_metar = match ¶ms.has_metar {
|
||||||
|
Some(h) => Some(h.parse::<bool>().unwrap()),
|
||||||
|
None => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let limit = match params.limit {
|
let limit = match params.limit {
|
||||||
Some(l) => l,
|
Some(l) => l,
|
||||||
None => 100
|
None => 100,
|
||||||
};
|
};
|
||||||
let page = match params.page {
|
let page = match params.page {
|
||||||
Some(p) => p,
|
Some(p) => p,
|
||||||
None => 1
|
None => 1,
|
||||||
};
|
};
|
||||||
let total = match QueryAirport::get_count(&filters) {
|
let total = match QueryAirport::get_count(&filters) {
|
||||||
Ok(t) => t,
|
Ok(t) => t,
|
||||||
Err(_) => 0
|
Err(_) => 0,
|
||||||
};
|
};
|
||||||
let pages = ((total as f64) / (if limit <= 0 { 1 } else { limit } as f64)).ceil() as i64;
|
let pages = ((total as f64) / (if limit <= 0 { 1 } else { limit } as f64)).ceil() as i64;
|
||||||
|
|
||||||
match web::block(move || QueryAirport::get_all(&filters, limit, page)).await.unwrap() {
|
match web::block(move || QueryAirport::get_all(&filters, limit, page))
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
{
|
||||||
Ok(a) => {
|
Ok(a) => {
|
||||||
// Convert Vec<QueryAirport> to Vec<Airport>
|
// Convert Vec<QueryAirport> to Vec<Airport>
|
||||||
let mut airports: Vec<Airport> = vec![];
|
let mut airports: Vec<Airport> = vec![];
|
||||||
@@ -155,9 +194,14 @@ async fn get_all(req: HttpRequest) -> HttpResponse {
|
|||||||
}
|
}
|
||||||
HttpResponse::Ok().json(Response {
|
HttpResponse::Ok().json(Response {
|
||||||
data: airports,
|
data: airports,
|
||||||
meta: Some(Metadata { page, limit, pages, total })
|
meta: Some(Metadata {
|
||||||
|
page,
|
||||||
|
limit,
|
||||||
|
pages,
|
||||||
|
total,
|
||||||
|
}),
|
||||||
})
|
})
|
||||||
},
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("{}", err);
|
error!("{}", err);
|
||||||
err.to_http_response()
|
err.to_http_response()
|
||||||
@@ -172,9 +216,14 @@ async fn get(icao: web::Path<String>) -> HttpResponse {
|
|||||||
let airport: Airport = a.into();
|
let airport: Airport = a.into();
|
||||||
HttpResponse::Ok().json(Response {
|
HttpResponse::Ok().json(Response {
|
||||||
data: airport,
|
data: airport,
|
||||||
meta: Some(Metadata { page: 1, limit: 1, pages: 1, total: 1 })
|
meta: Some(Metadata {
|
||||||
|
page: 1,
|
||||||
|
limit: 1,
|
||||||
|
pages: 1,
|
||||||
|
total: 1,
|
||||||
|
}),
|
||||||
})
|
})
|
||||||
},
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("{}", err);
|
error!("{}", err);
|
||||||
err.to_http_response()
|
err.to_http_response()
|
||||||
@@ -185,15 +234,15 @@ async fn get(icao: web::Path<String>) -> HttpResponse {
|
|||||||
#[post("")]
|
#[post("")]
|
||||||
async fn create(airport: web::Json<Airport>, auth: JwtAuth) -> HttpResponse {
|
async fn create(airport: web::Json<Airport>, auth: JwtAuth) -> HttpResponse {
|
||||||
let _ = match verify_role(&auth, "admin") {
|
let _ = match verify_role(&auth, "admin") {
|
||||||
Ok(_) => {},
|
Ok(_) => {}
|
||||||
Err(err) => return ResponseError::error_response(&err)
|
Err(err) => return ResponseError::error_response(&err),
|
||||||
};
|
};
|
||||||
let query_airport: QueryAirport = airport.into_inner().into();
|
let query_airport: QueryAirport = airport.into_inner().into();
|
||||||
match QueryAirport::insert(query_airport) {
|
match QueryAirport::insert(query_airport) {
|
||||||
Ok(a) => {
|
Ok(a) => {
|
||||||
let airport: Airport = a.into();
|
let airport: Airport = a.into();
|
||||||
HttpResponse::Ok().json(airport)
|
HttpResponse::Ok().json(airport)
|
||||||
},
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("{}", err);
|
error!("{}", err);
|
||||||
err.to_http_response()
|
err.to_http_response()
|
||||||
@@ -202,17 +251,21 @@ async fn create(airport: web::Json<Airport>, auth: JwtAuth) -> HttpResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[put("/{icao}")]
|
#[put("/{icao}")]
|
||||||
async fn update(_icao: web::Path<String>, airport: web::Json<Airport>, auth: JwtAuth) -> HttpResponse {
|
async fn update(
|
||||||
|
_icao: web::Path<String>,
|
||||||
|
airport: web::Json<Airport>,
|
||||||
|
auth: JwtAuth,
|
||||||
|
) -> HttpResponse {
|
||||||
let _ = match verify_role(&auth, "admin") {
|
let _ = match verify_role(&auth, "admin") {
|
||||||
Ok(_) => {},
|
Ok(_) => {}
|
||||||
Err(err) => return ResponseError::error_response(&err)
|
Err(err) => return ResponseError::error_response(&err),
|
||||||
};
|
};
|
||||||
let query_airport: QueryAirport = airport.into_inner().into();
|
let query_airport: QueryAirport = airport.into_inner().into();
|
||||||
match QueryAirport::update(query_airport) {
|
match QueryAirport::update(query_airport) {
|
||||||
Ok(a) => {
|
Ok(a) => {
|
||||||
let airport: Airport = a.into();
|
let airport: Airport = a.into();
|
||||||
HttpResponse::Ok().json(airport)
|
HttpResponse::Ok().json(airport)
|
||||||
},
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("{}", err);
|
error!("{}", err);
|
||||||
err.to_http_response()
|
err.to_http_response()
|
||||||
@@ -223,8 +276,8 @@ async fn update(_icao: web::Path<String>, airport: web::Json<Airport>, auth: Jwt
|
|||||||
#[delete("")]
|
#[delete("")]
|
||||||
async fn delete_all(auth: JwtAuth) -> HttpResponse {
|
async fn delete_all(auth: JwtAuth) -> HttpResponse {
|
||||||
let _ = match verify_role(&auth, "admin") {
|
let _ = match verify_role(&auth, "admin") {
|
||||||
Ok(_) => {},
|
Ok(_) => {}
|
||||||
Err(err) => return ResponseError::error_response(&err)
|
Err(err) => return ResponseError::error_response(&err),
|
||||||
};
|
};
|
||||||
match QueryAirport::delete(None) {
|
match QueryAirport::delete(None) {
|
||||||
Ok(_) => HttpResponse::NoContent().finish(),
|
Ok(_) => HttpResponse::NoContent().finish(),
|
||||||
@@ -238,8 +291,8 @@ async fn delete_all(auth: JwtAuth) -> HttpResponse {
|
|||||||
#[delete("/{icao}")]
|
#[delete("/{icao}")]
|
||||||
async fn delete(icao: web::Path<String>, auth: JwtAuth) -> HttpResponse {
|
async fn delete(icao: web::Path<String>, auth: JwtAuth) -> HttpResponse {
|
||||||
let _ = match verify_role(&auth, "admin") {
|
let _ = match verify_role(&auth, "admin") {
|
||||||
Ok(_) => {},
|
Ok(_) => {}
|
||||||
Err(err) => return ResponseError::error_response(&err)
|
Err(err) => return ResponseError::error_response(&err),
|
||||||
};
|
};
|
||||||
match QueryAirport::delete(Some(icao.into_inner())) {
|
match QueryAirport::delete(Some(icao.into_inner())) {
|
||||||
Ok(_) => HttpResponse::NoContent().finish(),
|
Ok(_) => HttpResponse::NoContent().finish(),
|
||||||
@@ -251,13 +304,14 @@ async fn delete(icao: web::Path<String>, auth: JwtAuth) -> HttpResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn init_routes(config: &mut web::ServiceConfig) {
|
pub fn init_routes(config: &mut web::ServiceConfig) {
|
||||||
config.service(web::scope("airports")
|
config.service(
|
||||||
|
web::scope("airports")
|
||||||
.service(get_all)
|
.service(get_all)
|
||||||
.service(get)
|
.service(get)
|
||||||
.service(create)
|
.service(create)
|
||||||
.service(update)
|
.service(update)
|
||||||
.service(delete)
|
.service(delete)
|
||||||
.service(delete_all)
|
.service(delete_all)
|
||||||
.service(import)
|
.service(import),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,11 @@
|
|||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
use argon2::{password_hash::{rand_core::OsRng, PasswordHasher, PasswordVerifier, SaltString, Error as HashError}, Argon2, PasswordHash};
|
use argon2::{
|
||||||
|
password_hash::{
|
||||||
|
rand_core::OsRng, PasswordHasher, PasswordVerifier, SaltString, Error as HashError,
|
||||||
|
},
|
||||||
|
Argon2, PasswordHash,
|
||||||
|
};
|
||||||
use jsonwebtoken::{DecodingKey, EncodingKey, Header, encode, decode, Validation, Algorithm};
|
use jsonwebtoken::{DecodingKey, EncodingKey, Header, encode, decode, Validation, Algorithm};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
@@ -18,7 +23,7 @@ struct TokenClaims {
|
|||||||
iss: String, // Issuer
|
iss: String, // Issuer
|
||||||
exp: i64, // Expiration time
|
exp: i64, // Expiration time
|
||||||
iat: i64, // Issued At
|
iat: i64, // Issued At
|
||||||
nbf: i64 // Not Before
|
nbf: i64, // Not Before
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
@@ -26,7 +31,7 @@ pub struct TokenDetails {
|
|||||||
pub token: Option<String>,
|
pub token: Option<String>,
|
||||||
pub token_uuid: uuid::Uuid,
|
pub token_uuid: uuid::Uuid,
|
||||||
pub email: String,
|
pub email: String,
|
||||||
pub expires_in: Option<i64>
|
pub expires_in: Option<i64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn verify_token(token: &str, public_key: &str) -> Result<TokenDetails, ServiceError> {
|
pub fn verify_token(token: &str, public_key: &str) -> Result<TokenDetails, ServiceError> {
|
||||||
@@ -35,7 +40,12 @@ pub fn verify_token(token: &str, public_key: &str) -> Result<TokenDetails, Servi
|
|||||||
let decoded = decode::<TokenClaims>(token, &key, &validation)?;
|
let decoded = decode::<TokenClaims>(token, &key, &validation)?;
|
||||||
let email = decoded.claims.sub;
|
let email = decoded.claims.sub;
|
||||||
let token_uuid = uuid::Uuid::parse_str(decoded.claims.token_uuid.as_str()).unwrap();
|
let token_uuid = uuid::Uuid::parse_str(decoded.claims.token_uuid.as_str()).unwrap();
|
||||||
Ok(TokenDetails { token: None, token_uuid, email, expires_in: None })
|
Ok(TokenDetails {
|
||||||
|
token: None,
|
||||||
|
token_uuid,
|
||||||
|
email,
|
||||||
|
expires_in: None,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_access_token(email: &str) -> Result<TokenDetails, ServiceError> {
|
pub fn generate_access_token(email: &str) -> Result<TokenDetails, ServiceError> {
|
||||||
@@ -54,17 +64,22 @@ pub fn generate_refresh_token(email: &str) -> Result<TokenDetails, ServiceError>
|
|||||||
.parse::<i64>()
|
.parse::<i64>()
|
||||||
.expect("REFRESH_TOKEN_MAXAGE must be an integer");
|
.expect("REFRESH_TOKEN_MAXAGE must be an integer");
|
||||||
let keys_dir = env::var("KEYS_DIR_PATH")?;
|
let keys_dir = env::var("KEYS_DIR_PATH")?;
|
||||||
let refresh_private_key = std::fs::read_to_string(format!("{}/refresh_private_key.pem", keys_dir))?;
|
let refresh_private_key =
|
||||||
|
std::fs::read_to_string(format!("{}/refresh_private_key.pem", keys_dir))?;
|
||||||
generate_token(&email, refresh_token_max_age, &refresh_private_key)
|
generate_token(&email, refresh_token_max_age, &refresh_private_key)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_token(email: &str, ttl: i64, private_key: &str) -> Result<TokenDetails, ServiceError> {
|
pub fn generate_token(
|
||||||
|
email: &str,
|
||||||
|
ttl: i64,
|
||||||
|
private_key: &str,
|
||||||
|
) -> Result<TokenDetails, ServiceError> {
|
||||||
let now = chrono::Utc::now();
|
let now = chrono::Utc::now();
|
||||||
let mut token_details = TokenDetails {
|
let mut token_details = TokenDetails {
|
||||||
token: None,
|
token: None,
|
||||||
token_uuid: uuid::Uuid::new_v4(),
|
token_uuid: uuid::Uuid::new_v4(),
|
||||||
email: email.to_string(),
|
email: email.to_string(),
|
||||||
expires_in: Some((now + chrono::Duration::minutes(ttl)).timestamp())
|
expires_in: Some((now + chrono::Duration::minutes(ttl)).timestamp()),
|
||||||
};
|
};
|
||||||
let claims = TokenClaims {
|
let claims = TokenClaims {
|
||||||
sub: token_details.email.clone(),
|
sub: token_details.email.clone(),
|
||||||
@@ -72,7 +87,7 @@ pub fn generate_token(email: &str, ttl: i64, private_key: &str) -> Result<TokenD
|
|||||||
token_uuid: token_details.token_uuid.to_string(),
|
token_uuid: token_details.token_uuid.to_string(),
|
||||||
exp: token_details.expires_in.unwrap(),
|
exp: token_details.expires_in.unwrap(),
|
||||||
iat: now.timestamp(),
|
iat: now.timestamp(),
|
||||||
nbf: now.timestamp()
|
nbf: now.timestamp(),
|
||||||
};
|
};
|
||||||
let header = Header::new(Algorithm::RS256);
|
let header = Header::new(Algorithm::RS256);
|
||||||
let key = EncodingKey::from_rsa_pem(private_key.as_bytes())?;
|
let key = EncodingKey::from_rsa_pem(private_key.as_bytes())?;
|
||||||
@@ -83,7 +98,11 @@ pub fn generate_token(email: &str, ttl: i64, private_key: &str) -> Result<TokenD
|
|||||||
|
|
||||||
pub fn hash_password(password: &[u8]) -> Result<String, HashError> {
|
pub fn hash_password(password: &[u8]) -> Result<String, HashError> {
|
||||||
let salt = SaltString::generate(&mut OsRng);
|
let salt = SaltString::generate(&mut OsRng);
|
||||||
Ok(Argon2::default().hash_password(password, &salt)?.to_string())
|
Ok(
|
||||||
|
Argon2::default()
|
||||||
|
.hash_password(password, &salt)?
|
||||||
|
.to_string(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn verify_password(hash: &str, password: &[u8]) -> Result<(), HashError> {
|
pub fn verify_password(hash: &str, password: &[u8]) -> Result<(), HashError> {
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
use std::{future::{ready, Ready}, env};
|
use std::{
|
||||||
|
future::{ready, Ready},
|
||||||
|
env,
|
||||||
|
};
|
||||||
use actix_web::{FromRequest, Error as ActixError, HttpRequest, dev::Payload, http};
|
use actix_web::{FromRequest, Error as ActixError, HttpRequest, dev::Payload, http};
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use log::error;
|
use log::error;
|
||||||
@@ -93,7 +96,10 @@ impl InsertUser {
|
|||||||
Ok(user)
|
Ok(user)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_profile_picture(email: &str, profile_picture: Option<&str>) -> Result<QueryUser, ServiceError> {
|
pub fn update_profile_picture(
|
||||||
|
email: &str,
|
||||||
|
profile_picture: Option<&str>,
|
||||||
|
) -> Result<QueryUser, ServiceError> {
|
||||||
let mut conn = connection()?;
|
let mut conn = connection()?;
|
||||||
let user = diesel::update(users::table)
|
let user = diesel::update(users::table)
|
||||||
.filter(users::email.eq(&email))
|
.filter(users::email.eq(&email))
|
||||||
@@ -136,7 +142,7 @@ impl From<QueryUser> for ResponseUser {
|
|||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct JwtAuth {
|
pub struct JwtAuth {
|
||||||
pub token: uuid::Uuid,
|
pub token: uuid::Uuid,
|
||||||
pub user: ResponseUser
|
pub user: ResponseUser,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromRequest for JwtAuth {
|
impl FromRequest for JwtAuth {
|
||||||
@@ -147,18 +153,23 @@ impl FromRequest for JwtAuth {
|
|||||||
.cookie("access_token")
|
.cookie("access_token")
|
||||||
.map(|c| c.value().to_string())
|
.map(|c| c.value().to_string())
|
||||||
.or_else(|| {
|
.or_else(|| {
|
||||||
req.headers().get(http::header::AUTHORIZATION)
|
req
|
||||||
|
.headers()
|
||||||
|
.get(http::header::AUTHORIZATION)
|
||||||
.map(|h| h.to_str().unwrap().split_at(7).1.to_string())
|
.map(|h| h.to_str().unwrap().split_at(7).1.to_string())
|
||||||
}) {
|
}) {
|
||||||
Some(token) => token,
|
Some(token) => token,
|
||||||
None => return ready(Err(ActixError::from(ServiceError {
|
None => {
|
||||||
|
return ready(Err(ActixError::from(ServiceError {
|
||||||
status: 401,
|
status: 401,
|
||||||
message: "Unauthorized".to_string()
|
message: "Unauthorized".to_string(),
|
||||||
})))
|
})))
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let keys_dir = env::var("KEYS_DIR_PATH").expect("KEYS_DIR_PATH must be set");
|
let keys_dir = env::var("KEYS_DIR_PATH").expect("KEYS_DIR_PATH must be set");
|
||||||
let public_key = std::fs::read_to_string(format!("{}/access_public_key.pem", keys_dir)).expect("Failed to read access public key");
|
let public_key = std::fs::read_to_string(format!("{}/access_public_key.pem", keys_dir))
|
||||||
|
.expect("Failed to read access public key");
|
||||||
|
|
||||||
let access_token_details = match verify_token(&access_token, &public_key) {
|
let access_token_details = match verify_token(&access_token, &public_key) {
|
||||||
Ok(token_details) => token_details,
|
Ok(token_details) => token_details,
|
||||||
@@ -166,12 +177,13 @@ impl FromRequest for JwtAuth {
|
|||||||
error!("Failed to verify access token: {}", err);
|
error!("Failed to verify access token: {}", err);
|
||||||
return ready(Err(ActixError::from(ServiceError {
|
return ready(Err(ActixError::from(ServiceError {
|
||||||
status: 401,
|
status: 401,
|
||||||
message: format!("Failed to verify access token: {}", err)
|
message: format!("Failed to verify access token: {}", err),
|
||||||
})))
|
})));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let access_token_uuid = uuid::Uuid::parse_str(&access_token_details.token_uuid.to_string()).unwrap();
|
let access_token_uuid =
|
||||||
|
uuid::Uuid::parse_str(&access_token_details.token_uuid.to_string()).unwrap();
|
||||||
|
|
||||||
let mut conn = match crate::db::redis_connection() {
|
let mut conn = match crate::db::redis_connection() {
|
||||||
Ok(conn) => conn,
|
Ok(conn) => conn,
|
||||||
@@ -179,8 +191,8 @@ impl FromRequest for JwtAuth {
|
|||||||
error!("Failed to get redis connection: {}", err);
|
error!("Failed to get redis connection: {}", err);
|
||||||
return ready(Err(ActixError::from(ServiceError {
|
return ready(Err(ActixError::from(ServiceError {
|
||||||
status: 500,
|
status: 500,
|
||||||
message: format!("Failed to get redis connection: {}", err)
|
message: format!("Failed to get redis connection: {}", err),
|
||||||
})))
|
})));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let user_email = match conn.get::<_, String>(access_token_uuid.clone().to_string()) {
|
let user_email = match conn.get::<_, String>(access_token_uuid.clone().to_string()) {
|
||||||
@@ -188,22 +200,25 @@ impl FromRequest for JwtAuth {
|
|||||||
Err(_) => {
|
Err(_) => {
|
||||||
return ready(Err(ActixError::from(ServiceError {
|
return ready(Err(ActixError::from(ServiceError {
|
||||||
status: 401,
|
status: 401,
|
||||||
message: format!("Access token was not found")
|
message: format!("Access token was not found"),
|
||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
match QueryUser::get_by_email(&user_email) {
|
match QueryUser::get_by_email(&user_email) {
|
||||||
Ok(user) => {
|
Ok(user) => ready(Ok(JwtAuth {
|
||||||
ready(Ok(JwtAuth { token: access_token_uuid, user: user.into() }))
|
token: access_token_uuid,
|
||||||
}
|
user: user.into(),
|
||||||
Err(_) => return ready(Err(ActixError::from(ServiceError {
|
})),
|
||||||
|
Err(_) => {
|
||||||
|
return ready(Err(ActixError::from(ServiceError {
|
||||||
status: 401,
|
status: 401,
|
||||||
message: format!("User was not found")
|
message: format!("User was not found"),
|
||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn verify_role(auth: &JwtAuth, role: &str) -> Result<(), ServiceError> {
|
pub fn verify_role(auth: &JwtAuth, role: &str) -> Result<(), ServiceError> {
|
||||||
if auth.user.role == role {
|
if auth.user.role == role {
|
||||||
@@ -211,7 +226,7 @@ pub fn verify_role(auth: &JwtAuth, role: &str) -> Result<(), ServiceError> {
|
|||||||
} else {
|
} else {
|
||||||
Err(ServiceError {
|
Err(ServiceError {
|
||||||
status: 403,
|
status: 403,
|
||||||
message: "Forbidden".to_string()
|
message: "Forbidden".to_string(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +1,32 @@
|
|||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
use actix_web::{get, post, web, HttpResponse, ResponseError, cookie::{Cookie, time::Duration}, HttpRequest};
|
use actix_web::{
|
||||||
|
get, post, web, HttpResponse, ResponseError,
|
||||||
|
cookie::{Cookie, time::Duration},
|
||||||
|
HttpRequest,
|
||||||
|
};
|
||||||
use log::error;
|
use log::error;
|
||||||
use redis::AsyncCommands;
|
use redis::AsyncCommands;
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
use crate::{error_handler::ServiceError, db::Response};
|
use crate::{error_handler::ServiceError, db::Response};
|
||||||
|
|
||||||
use crate::{auth::{LoginRequest, RegisterUser, InsertUser, QueryUser, verify_password, JwtAuth, verify_token, generate_access_token, generate_refresh_token}, db};
|
use crate::{
|
||||||
|
auth::{
|
||||||
|
LoginRequest, RegisterUser, InsertUser, QueryUser, verify_password, JwtAuth, verify_token,
|
||||||
|
generate_access_token, generate_refresh_token,
|
||||||
|
},
|
||||||
|
db,
|
||||||
|
};
|
||||||
|
|
||||||
#[post("/register")]
|
#[post("/register")]
|
||||||
async fn register(user: web::Json<RegisterUser>) -> HttpResponse {
|
async fn register(user: web::Json<RegisterUser>) -> HttpResponse {
|
||||||
let register_user = user.0;
|
let register_user = user.0;
|
||||||
let insert_user: InsertUser = match register_user.convert_to_insert() {
|
let insert_user: InsertUser = match register_user.convert_to_insert() {
|
||||||
Ok(user) => user,
|
Ok(user) => user,
|
||||||
Err(err) => return ResponseError::error_response(&err)
|
Err(err) => return ResponseError::error_response(&err),
|
||||||
};
|
};
|
||||||
match InsertUser::insert(insert_user) {
|
match InsertUser::insert(insert_user) {
|
||||||
Ok(_) => {
|
Ok(_) => HttpResponse::Created().finish(),
|
||||||
HttpResponse::Created().finish()
|
|
||||||
},
|
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
// Obfuscate the service error message to prevent leaking database details
|
// Obfuscate the service error message to prevent leaking database details
|
||||||
if err.status == 409 {
|
if err.status == 409 {
|
||||||
@@ -36,7 +44,7 @@ async fn login(request: web::Json<LoginRequest>) -> HttpResponse {
|
|||||||
|
|
||||||
let query_user = match QueryUser::get_by_email(&email) {
|
let query_user = match QueryUser::get_by_email(&email) {
|
||||||
Ok(query_user) => query_user,
|
Ok(query_user) => query_user,
|
||||||
Err(err) => return ResponseError::error_response(&err)
|
Err(err) => return ResponseError::error_response(&err),
|
||||||
};
|
};
|
||||||
let hash = &query_user.hash;
|
let hash = &query_user.hash;
|
||||||
let password = request.password.as_bytes();
|
let password = request.password.as_bytes();
|
||||||
@@ -46,7 +54,7 @@ async fn login(request: web::Json<LoginRequest>) -> HttpResponse {
|
|||||||
Ok(token_details) => token_details,
|
Ok(token_details) => token_details,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("Failed to generate access token: {}", err);
|
error!("Failed to generate access token: {}", err);
|
||||||
return ResponseError::error_response(&err)
|
return ResponseError::error_response(&err);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -54,7 +62,7 @@ async fn login(request: web::Json<LoginRequest>) -> HttpResponse {
|
|||||||
Ok(token_details) => token_details,
|
Ok(token_details) => token_details,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("Failed to generate refresh token: {}", err);
|
error!("Failed to generate refresh token: {}", err);
|
||||||
return ResponseError::error_response(&err)
|
return ResponseError::error_response(&err);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -62,7 +70,7 @@ async fn login(request: web::Json<LoginRequest>) -> HttpResponse {
|
|||||||
Ok(conn) => conn,
|
Ok(conn) => conn,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("Failed to get redis connection: {}", err);
|
error!("Failed to get redis connection: {}", err);
|
||||||
return ResponseError::error_response(&err)
|
return ResponseError::error_response(&err);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -76,31 +84,47 @@ async fn login(request: web::Json<LoginRequest>) -> HttpResponse {
|
|||||||
.parse::<i64>()
|
.parse::<i64>()
|
||||||
.expect("REFRESH_TOKEN_MAXAGE must be an integer");
|
.expect("REFRESH_TOKEN_MAXAGE must be an integer");
|
||||||
|
|
||||||
let access_result: redis::RedisResult<()> = conn.set_ex(access_token_details.token_uuid.to_string(), &email, (access_token_max_age * 60) as usize).await;
|
let access_result: redis::RedisResult<()> = conn
|
||||||
|
.set_ex(
|
||||||
|
access_token_details.token_uuid.to_string(),
|
||||||
|
&email,
|
||||||
|
(access_token_max_age * 60) as u64,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
if let Err(err) = access_result {
|
if let Err(err) = access_result {
|
||||||
error!("Failed to set access token in redis: {}", err);
|
error!("Failed to set access token in redis: {}", err);
|
||||||
return ResponseError::error_response(&ServiceError {
|
return ResponseError::error_response(&ServiceError {
|
||||||
status: 500,
|
status: 500,
|
||||||
message: format!("Failed to set access token in redis: {}", err)
|
message: format!("Failed to set access token in redis: {}", err),
|
||||||
})
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
let refresh_result: redis::RedisResult<()> = conn.set_ex(refresh_token_details.token_uuid.to_string(), &email, (refresh_token_max_age * 60) as usize).await;
|
let refresh_result: redis::RedisResult<()> = conn
|
||||||
|
.set_ex(
|
||||||
|
refresh_token_details.token_uuid.to_string(),
|
||||||
|
&email,
|
||||||
|
(refresh_token_max_age * 60) as u64,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
if let Err(err) = refresh_result {
|
if let Err(err) = refresh_result {
|
||||||
error!("Failed to set refresh token in redis: {}", err);
|
error!("Failed to set refresh token in redis: {}", err);
|
||||||
return ResponseError::error_response(&ServiceError {
|
return ResponseError::error_response(&ServiceError {
|
||||||
status: 500,
|
status: 500,
|
||||||
message: format!("Failed to set refresh token in redis: {}", err)
|
message: format!("Failed to set refresh token in redis: {}", err),
|
||||||
})
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
let access_cookie = Cookie::build("access_token", access_token_details.token.clone().unwrap())
|
let access_cookie =
|
||||||
|
Cookie::build("access_token", access_token_details.token.clone().unwrap())
|
||||||
.path("/")
|
.path("/")
|
||||||
.max_age(Duration::new(access_token_max_age * 60, 0))
|
.max_age(Duration::new(access_token_max_age * 60, 0))
|
||||||
.http_only(true)
|
.http_only(true)
|
||||||
.secure(true)
|
.secure(true)
|
||||||
.finish();
|
.finish();
|
||||||
let refresh_cookie = Cookie::build("refresh_token", refresh_token_details.token.clone().unwrap())
|
let refresh_cookie = Cookie::build(
|
||||||
|
"refresh_token",
|
||||||
|
refresh_token_details.token.clone().unwrap(),
|
||||||
|
)
|
||||||
.path("/")
|
.path("/")
|
||||||
.max_age(Duration::new(refresh_token_max_age * 60, 0))
|
.max_age(Duration::new(refresh_token_max_age * 60, 0))
|
||||||
.http_only(true)
|
.http_only(true)
|
||||||
@@ -112,18 +136,22 @@ async fn login(request: web::Json<LoginRequest>) -> HttpResponse {
|
|||||||
.http_only(false)
|
.http_only(false)
|
||||||
.finish();
|
.finish();
|
||||||
|
|
||||||
let access_token_uuid = uuid::Uuid::parse_str(&access_token_details.token_uuid.to_string()).unwrap();
|
let access_token_uuid =
|
||||||
|
uuid::Uuid::parse_str(&access_token_details.token_uuid.to_string()).unwrap();
|
||||||
|
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
.cookie(access_cookie)
|
.cookie(access_cookie)
|
||||||
.cookie(refresh_cookie)
|
.cookie(refresh_cookie)
|
||||||
.cookie(logged_in_cookie)
|
.cookie(logged_in_cookie)
|
||||||
.json(JwtAuth { token: access_token_uuid, user: query_user.into() })
|
.json(JwtAuth {
|
||||||
},
|
token: access_token_uuid,
|
||||||
|
user: query_user.into(),
|
||||||
|
})
|
||||||
|
}
|
||||||
Err(err) => ResponseError::error_response(&ServiceError {
|
Err(err) => ResponseError::error_response(&ServiceError {
|
||||||
status: 401,
|
status: 401,
|
||||||
message: err.to_string()
|
message: err.to_string(),
|
||||||
})
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,10 +166,10 @@ async fn session(req: HttpRequest) -> HttpResponse {
|
|||||||
.expect("Unable to read refresh public key");
|
.expect("Unable to read refresh public key");
|
||||||
match verify_token(&access_token, &public_key) {
|
match verify_token(&access_token, &public_key) {
|
||||||
Ok(_) => true,
|
Ok(_) => true,
|
||||||
Err(_) => false
|
Err(_) => false,
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
None => false
|
None => false,
|
||||||
};
|
};
|
||||||
if !has_session {
|
if !has_session {
|
||||||
// If there is a refresh_token cookie, check if it is valid
|
// If there is a refresh_token cookie, check if it is valid
|
||||||
@@ -152,37 +180,41 @@ async fn session(req: HttpRequest) -> HttpResponse {
|
|||||||
.expect("Unable to read refresh public key");
|
.expect("Unable to read refresh public key");
|
||||||
match verify_token(&refresh_token, &public_key) {
|
match verify_token(&refresh_token, &public_key) {
|
||||||
Ok(_) => return HttpResponse::Ok().json(true),
|
Ok(_) => return HttpResponse::Ok().json(true),
|
||||||
Err(_) => return HttpResponse::Ok().json(false)
|
Err(_) => return HttpResponse::Ok().json(false),
|
||||||
};
|
};
|
||||||
},
|
}
|
||||||
None => return HttpResponse::Ok().json(false)
|
None => return HttpResponse::Ok().json(false),
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
return HttpResponse::Ok().json(true)
|
return HttpResponse::Ok().json(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct RefreshParams {
|
struct RefreshParams {
|
||||||
refresh_token_rotation: Option<bool>
|
refresh_token_rotation: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/refresh")]
|
#[get("/refresh")]
|
||||||
async fn refresh(req: HttpRequest) -> HttpResponse {
|
async fn refresh(req: HttpRequest) -> HttpResponse {
|
||||||
let params = match web::Query::<RefreshParams>::from_query(req.query_string()) {
|
let params = match web::Query::<RefreshParams>::from_query(req.query_string()) {
|
||||||
Ok(params) => params,
|
Ok(params) => params,
|
||||||
Err(err) => return ResponseError::error_response(&ServiceError {
|
Err(err) => {
|
||||||
|
return ResponseError::error_response(&ServiceError {
|
||||||
status: 422,
|
status: 422,
|
||||||
message: err.to_string()
|
message: err.to_string(),
|
||||||
})
|
})
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let refresh_token = match req.cookie("refresh_token") {
|
let refresh_token = match req.cookie("refresh_token") {
|
||||||
Some(cookie) => cookie.value().to_string(),
|
Some(cookie) => cookie.value().to_string(),
|
||||||
None => return ResponseError::error_response(&ServiceError {
|
None => {
|
||||||
|
return ResponseError::error_response(&ServiceError {
|
||||||
status: 401,
|
status: 401,
|
||||||
message: "Refresh token not found".to_string()
|
message: "Refresh token not found".to_string(),
|
||||||
})
|
})
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let keys_dir = env::var("KEYS_DIR_PATH").expect("KEYS_DIR_PATH must be set");
|
let keys_dir = env::var("KEYS_DIR_PATH").expect("KEYS_DIR_PATH must be set");
|
||||||
@@ -190,7 +222,7 @@ async fn refresh(req: HttpRequest) -> HttpResponse {
|
|||||||
.expect("Unable to read refresh public key");
|
.expect("Unable to read refresh public key");
|
||||||
let refresh_token_details = match verify_token(&refresh_token, &public_key) {
|
let refresh_token_details = match verify_token(&refresh_token, &public_key) {
|
||||||
Ok(token_details) => token_details,
|
Ok(token_details) => token_details,
|
||||||
Err(err) => return ResponseError::error_response(&err)
|
Err(err) => return ResponseError::error_response(&err),
|
||||||
};
|
};
|
||||||
|
|
||||||
let email = refresh_token_details.email.clone();
|
let email = refresh_token_details.email.clone();
|
||||||
@@ -201,7 +233,7 @@ async fn refresh(req: HttpRequest) -> HttpResponse {
|
|||||||
Ok(token_details) => token_details,
|
Ok(token_details) => token_details,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("Failed to generate access token: {}", err);
|
error!("Failed to generate access token: {}", err);
|
||||||
return ResponseError::error_response(&err)
|
return ResponseError::error_response(&err);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -209,7 +241,7 @@ async fn refresh(req: HttpRequest) -> HttpResponse {
|
|||||||
Ok(conn) => conn,
|
Ok(conn) => conn,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("Failed to get redis connection: {}", err);
|
error!("Failed to get redis connection: {}", err);
|
||||||
return ResponseError::error_response(&err)
|
return ResponseError::error_response(&err);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -223,11 +255,10 @@ async fn refresh(req: HttpRequest) -> HttpResponse {
|
|||||||
match verify_token(&access_token, &public_key) {
|
match verify_token(&access_token, &public_key) {
|
||||||
Ok(token_details) => {
|
Ok(token_details) => {
|
||||||
let _: redis::RedisResult<()> = conn.del(token_details.token_uuid.to_string()).await;
|
let _: redis::RedisResult<()> = conn.del(token_details.token_uuid.to_string()).await;
|
||||||
|
}
|
||||||
},
|
|
||||||
Err(_) => {}
|
Err(_) => {}
|
||||||
};
|
};
|
||||||
},
|
}
|
||||||
None => {}
|
None => {}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -236,16 +267,23 @@ async fn refresh(req: HttpRequest) -> HttpResponse {
|
|||||||
.parse::<i64>()
|
.parse::<i64>()
|
||||||
.expect("ACCESS_TOKEN_MAXAGE must be an integer");
|
.expect("ACCESS_TOKEN_MAXAGE must be an integer");
|
||||||
|
|
||||||
let access_result: redis::RedisResult<()> = conn.set_ex(access_token_details.token_uuid.to_string(), &email, (access_token_max_age * 60) as usize).await;
|
let access_result: redis::RedisResult<()> = conn
|
||||||
|
.set_ex(
|
||||||
|
access_token_details.token_uuid.to_string(),
|
||||||
|
&email,
|
||||||
|
(access_token_max_age * 60) as u64,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
if let Err(err) = access_result {
|
if let Err(err) = access_result {
|
||||||
error!("Failed to set access token in redis: {}", err);
|
error!("Failed to set access token in redis: {}", err);
|
||||||
return ResponseError::error_response(&ServiceError {
|
return ResponseError::error_response(&ServiceError {
|
||||||
status: 500,
|
status: 500,
|
||||||
message: format!("Failed to set access token in redis: {}", err)
|
message: format!("Failed to set access token in redis: {}", err),
|
||||||
})
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
let access_cookie = Cookie::build("access_token", access_token_details.token.clone().unwrap())
|
let access_cookie =
|
||||||
|
Cookie::build("access_token", access_token_details.token.clone().unwrap())
|
||||||
.path("/")
|
.path("/")
|
||||||
.max_age(Duration::new(access_token_max_age * 60, 0))
|
.max_age(Duration::new(access_token_max_age * 60, 0))
|
||||||
.http_only(true)
|
.http_only(true)
|
||||||
@@ -257,22 +295,24 @@ async fn refresh(req: HttpRequest) -> HttpResponse {
|
|||||||
.http_only(false)
|
.http_only(false)
|
||||||
.finish();
|
.finish();
|
||||||
|
|
||||||
let access_token_uuid = uuid::Uuid::parse_str(&access_token_details.token_uuid.to_string()).unwrap();
|
let access_token_uuid =
|
||||||
|
uuid::Uuid::parse_str(&access_token_details.token_uuid.to_string()).unwrap();
|
||||||
|
|
||||||
// Refresh the refresh token if requested
|
// Refresh the refresh token if requested
|
||||||
let refresh_token_rotation = match params.refresh_token_rotation {
|
let refresh_token_rotation = match params.refresh_token_rotation {
|
||||||
Some(refresh_token_rotation) => refresh_token_rotation,
|
Some(refresh_token_rotation) => refresh_token_rotation,
|
||||||
None => false
|
None => false,
|
||||||
};
|
};
|
||||||
if refresh_token_rotation {
|
if refresh_token_rotation {
|
||||||
// Delete the old refresh token
|
// Delete the old refresh token
|
||||||
let _: redis::RedisResult<()> = conn.del(refresh_token_details.token_uuid.to_string()).await;
|
let _: redis::RedisResult<()> =
|
||||||
|
conn.del(refresh_token_details.token_uuid.to_string()).await;
|
||||||
|
|
||||||
let refresh_token_details = match generate_refresh_token(&refresh_token_details.email) {
|
let refresh_token_details = match generate_refresh_token(&refresh_token_details.email) {
|
||||||
Ok(token_details) => token_details,
|
Ok(token_details) => token_details,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("Failed to generate refresh token: {}", err);
|
error!("Failed to generate refresh token: {}", err);
|
||||||
return ResponseError::error_response(&err)
|
return ResponseError::error_response(&err);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -281,16 +321,25 @@ async fn refresh(req: HttpRequest) -> HttpResponse {
|
|||||||
.parse::<i64>()
|
.parse::<i64>()
|
||||||
.expect("REFRESH_TOKEN_MAXAGE must be an integer");
|
.expect("REFRESH_TOKEN_MAXAGE must be an integer");
|
||||||
|
|
||||||
let refresh_result: redis::RedisResult<()> = conn.set_ex(refresh_token_details.token_uuid.to_string(), &refresh_token_details.email, (refresh_token_max_age * 60) as usize).await;
|
let refresh_result: redis::RedisResult<()> = conn
|
||||||
|
.set_ex(
|
||||||
|
refresh_token_details.token_uuid.to_string(),
|
||||||
|
&refresh_token_details.email,
|
||||||
|
(refresh_token_max_age * 60) as u64,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
if let Err(err) = refresh_result {
|
if let Err(err) = refresh_result {
|
||||||
error!("Failed to set refresh token in redis: {}", err);
|
error!("Failed to set refresh token in redis: {}", err);
|
||||||
return ResponseError::error_response(&ServiceError {
|
return ResponseError::error_response(&ServiceError {
|
||||||
status: 500,
|
status: 500,
|
||||||
message: format!("Failed to set refresh token in redis: {}", err)
|
message: format!("Failed to set refresh token in redis: {}", err),
|
||||||
})
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
let refresh_cookie = Cookie::build("refresh_token", refresh_token_details.token.clone().unwrap())
|
let refresh_cookie = Cookie::build(
|
||||||
|
"refresh_token",
|
||||||
|
refresh_token_details.token.clone().unwrap(),
|
||||||
|
)
|
||||||
.path("/")
|
.path("/")
|
||||||
.max_age(Duration::new(refresh_token_max_age * 60, 0))
|
.max_age(Duration::new(refresh_token_max_age * 60, 0))
|
||||||
.http_only(true)
|
.http_only(true)
|
||||||
@@ -301,15 +350,21 @@ async fn refresh(req: HttpRequest) -> HttpResponse {
|
|||||||
.cookie(refresh_cookie)
|
.cookie(refresh_cookie)
|
||||||
.cookie(access_cookie)
|
.cookie(access_cookie)
|
||||||
.cookie(logged_in_cookie)
|
.cookie(logged_in_cookie)
|
||||||
.json(JwtAuth { token: access_token_uuid, user: query_user.into() })
|
.json(JwtAuth {
|
||||||
|
token: access_token_uuid,
|
||||||
|
user: query_user.into(),
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
.cookie(access_cookie)
|
.cookie(access_cookie)
|
||||||
.cookie(logged_in_cookie)
|
.cookie(logged_in_cookie)
|
||||||
.json(JwtAuth { token: access_token_uuid, user: query_user.into() })
|
.json(JwtAuth {
|
||||||
|
token: access_token_uuid,
|
||||||
|
user: query_user.into(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Err(err) => return ResponseError::error_response(&err)
|
Err(err) => return ResponseError::error_response(&err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -317,37 +372,41 @@ async fn refresh(req: HttpRequest) -> HttpResponse {
|
|||||||
async fn logout(req: HttpRequest, auth: JwtAuth) -> HttpResponse {
|
async fn logout(req: HttpRequest, auth: JwtAuth) -> HttpResponse {
|
||||||
let refresh_token = match req.cookie("refresh_token") {
|
let refresh_token = match req.cookie("refresh_token") {
|
||||||
Some(cookie) => cookie.value().to_string(),
|
Some(cookie) => cookie.value().to_string(),
|
||||||
None => return ResponseError::error_response(&ServiceError {
|
None => {
|
||||||
|
return ResponseError::error_response(&ServiceError {
|
||||||
status: 401,
|
status: 401,
|
||||||
message: "Refresh token not found".to_string()
|
message: "Refresh token not found".to_string(),
|
||||||
})
|
})
|
||||||
|
}
|
||||||
};
|
};
|
||||||
let keys_dir = env::var("KEYS_DIR_PATH").expect("KEYS_DIR_PATH must be set");
|
let keys_dir = env::var("KEYS_DIR_PATH").expect("KEYS_DIR_PATH must be set");
|
||||||
let public_key = std::fs::read_to_string(format!("{}/refresh_public_key.pem", keys_dir))
|
let public_key = std::fs::read_to_string(format!("{}/refresh_public_key.pem", keys_dir))
|
||||||
.expect("Unable to read refresh public key");
|
.expect("Unable to read refresh public key");
|
||||||
let refresh_token_details = match verify_token(&refresh_token, &public_key) {
|
let refresh_token_details = match verify_token(&refresh_token, &public_key) {
|
||||||
Ok(token_details) => token_details,
|
Ok(token_details) => token_details,
|
||||||
Err(err) => return ResponseError::error_response(&err)
|
Err(err) => return ResponseError::error_response(&err),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut conn = match db::redis_async_connection().await {
|
let mut conn = match db::redis_async_connection().await {
|
||||||
Ok(conn) => conn,
|
Ok(conn) => conn,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("Failed to get redis connection: {}", err);
|
error!("Failed to get redis connection: {}", err);
|
||||||
return ResponseError::error_response(&err)
|
return ResponseError::error_response(&err);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let access_result: redis::RedisResult<()> = conn.del(&[
|
let access_result: redis::RedisResult<()> = conn
|
||||||
|
.del(&[
|
||||||
refresh_token_details.token_uuid.to_string(),
|
refresh_token_details.token_uuid.to_string(),
|
||||||
auth.token.to_string()
|
auth.token.to_string(),
|
||||||
]).await;
|
])
|
||||||
|
.await;
|
||||||
if let Err(err) = access_result {
|
if let Err(err) = access_result {
|
||||||
error!("Failed to set access token in redis: {}", err);
|
error!("Failed to set access token in redis: {}", err);
|
||||||
return ResponseError::error_response(&ServiceError {
|
return ResponseError::error_response(&ServiceError {
|
||||||
status: 500,
|
status: 500,
|
||||||
message: format!("Failed to set access token in redis: {}", err)
|
message: format!("Failed to set access token in redis: {}", err),
|
||||||
})
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
let access_cookie = Cookie::build("access_token", "")
|
let access_cookie = Cookie::build("access_token", "")
|
||||||
@@ -382,7 +441,7 @@ async fn me(auth: JwtAuth) -> HttpResponse {
|
|||||||
async fn roles() -> HttpResponse {
|
async fn roles() -> HttpResponse {
|
||||||
HttpResponse::Ok().json(Response {
|
HttpResponse::Ok().json(Response {
|
||||||
data: vec!["admin", "user"],
|
data: vec!["admin", "user"],
|
||||||
meta: None
|
meta: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -397,12 +456,13 @@ pub fn init_routes(config: &mut web::ServiceConfig) {
|
|||||||
u.role = "admin".to_string();
|
u.role = "admin".to_string();
|
||||||
u.verified = true;
|
u.verified = true;
|
||||||
let _ = InsertUser::insert(u);
|
let _ = InsertUser::insert(u);
|
||||||
config.service(web::scope("auth")
|
config.service(
|
||||||
|
web::scope("auth")
|
||||||
.service(register)
|
.service(register)
|
||||||
.service(login)
|
.service(login)
|
||||||
.service(refresh)
|
.service(refresh)
|
||||||
.service(logout)
|
.service(logout)
|
||||||
.service(me)
|
.service(me)
|
||||||
.service(roles)
|
.service(roles),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,10 @@
|
|||||||
use crate::error_handler::ServiceError;
|
use crate::error_handler::ServiceError;
|
||||||
use diesel::{r2d2::ConnectionManager, PgConnection};
|
use diesel::{r2d2::ConnectionManager, PgConnection};
|
||||||
use redis::{Client as RedisClient, aio::Connection as RedisConnection};
|
use redis::{Client as RedisClient, aio::MultiplexedConnection as RedisConnection};
|
||||||
use s3::{Bucket, Region, creds::Credentials, BucketConfiguration, request::ResponseData, bucket_ops::CreateBucketResponse};
|
use s3::{
|
||||||
|
Bucket, Region, creds::Credentials, BucketConfiguration, request::ResponseData,
|
||||||
|
bucket_ops::CreateBucketResponse,
|
||||||
|
};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use crate::diesel_migrations::MigrationHarness;
|
use crate::diesel_migrations::MigrationHarness;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
@@ -23,9 +26,15 @@ lazy_static! {
|
|||||||
let host = env::var("DATABASE_HOST").unwrap_or("localhost".to_string());
|
let host = env::var("DATABASE_HOST").unwrap_or("localhost".to_string());
|
||||||
let name = env::var("DATABASE_NAME").expect("Database name is not set");
|
let name = env::var("DATABASE_NAME").expect("Database name is not set");
|
||||||
let port = env::var("DATABASE_PORT").unwrap_or("5432".to_string());
|
let port = env::var("DATABASE_PORT").unwrap_or("5432".to_string());
|
||||||
let url = format!("postgres://{}:{}@{}:{}/{}", username, password, host, port, name);
|
let url = format!(
|
||||||
|
"postgres://{}:{}@{}:{}/{}",
|
||||||
|
username, password, host, port, name
|
||||||
|
);
|
||||||
let manager = ConnectionManager::<PgConnection>::new(url);
|
let manager = ConnectionManager::<PgConnection>::new(url);
|
||||||
Pool::builder().test_on_check_out(true).build(manager).expect("Failed to create db pool")
|
Pool::builder()
|
||||||
|
.test_on_check_out(true)
|
||||||
|
.build(manager)
|
||||||
|
.expect("Failed to create db pool")
|
||||||
};
|
};
|
||||||
static ref REDIS: RedisClient = {
|
static ref REDIS: RedisClient = {
|
||||||
let host = env::var("REDIS_HOST").unwrap_or("localhost".to_string());
|
let host = env::var("REDIS_HOST").unwrap_or("localhost".to_string());
|
||||||
@@ -50,10 +59,12 @@ lazy_static! {
|
|||||||
secret_key: Some(password),
|
secret_key: Some(password),
|
||||||
security_token: None,
|
security_token: None,
|
||||||
session_token: None,
|
session_token: None,
|
||||||
expiration: None
|
expiration: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
Bucket::new("aviation", region.clone(), credentials.clone()).expect("Failed to create S3 Bucket").with_path_style()
|
Bucket::new("aviation", region.clone(), credentials.clone())
|
||||||
|
.expect("Failed to create S3 Bucket")
|
||||||
|
.with_path_style()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,22 +74,21 @@ pub async fn init() {
|
|||||||
lazy_static::initialize(&BUCKET);
|
lazy_static::initialize(&BUCKET);
|
||||||
match create_bucket().await {
|
match create_bucket().await {
|
||||||
Ok(_) => info!("Bucket initialized"),
|
Ok(_) => info!("Bucket initialized"),
|
||||||
Err(err) => {
|
Err(err) => match err.status {
|
||||||
match err.status {
|
|
||||||
409 => warn!("Bucket already exists"),
|
409 => warn!("Bucket already exists"),
|
||||||
_ => error!("Failed to initialize bucket; {}", err.message)
|
_ => error!("Failed to initialize bucket; {}", err.message),
|
||||||
}
|
},
|
||||||
}
|
|
||||||
};
|
};
|
||||||
let mut pool: DbConnection = connection().expect("Failed to get db connection");
|
let mut pool: DbConnection = connection().expect("Failed to get db connection");
|
||||||
match pool.run_pending_migrations(MIGRATIONS) {
|
match pool.run_pending_migrations(MIGRATIONS) {
|
||||||
Ok(_) => info!("Database initialized"),
|
Ok(_) => info!("Database initialized"),
|
||||||
Err(err) => error!("Failed to initialize database; {}", err)
|
Err(err) => error!("Failed to initialize database; {}", err),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn connection() -> Result<DbConnection, ServiceError> {
|
pub fn connection() -> Result<DbConnection, ServiceError> {
|
||||||
POOL.get()
|
POOL
|
||||||
|
.get()
|
||||||
.map_err(|e| ServiceError::new(500, format!("Failed getting db connection: {}", e)))
|
.map_err(|e| ServiceError::new(500, format!("Failed getting db connection: {}", e)))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,7 +98,7 @@ pub fn redis_connection() -> Result<redis::Connection, ServiceError> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn redis_async_connection() -> Result<RedisConnection, ServiceError> {
|
pub async fn redis_async_connection() -> Result<RedisConnection, ServiceError> {
|
||||||
let conn = REDIS.get_async_connection().await?;
|
let conn = REDIS.get_multiplexed_async_connection().await?;
|
||||||
Ok(conn)
|
Ok(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,10 +119,16 @@ async fn create_bucket() -> Result<CreateBucketResponse, ServiceError> {
|
|||||||
secret_key: Some(password),
|
secret_key: Some(password),
|
||||||
security_token: None,
|
security_token: None,
|
||||||
session_token: None,
|
session_token: None,
|
||||||
expiration: None
|
expiration: None,
|
||||||
};
|
};
|
||||||
let bucket_name = "aviation";
|
let bucket_name = "aviation";
|
||||||
let response = Bucket::create_with_path_style(bucket_name, region, credentials, BucketConfiguration::default()).await?;
|
let response = Bucket::create_with_path_style(
|
||||||
|
bucket_name,
|
||||||
|
region,
|
||||||
|
credentials,
|
||||||
|
BucketConfiguration::default(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
Ok(response)
|
Ok(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,7 +151,7 @@ pub async fn delete_file(path: &str) -> Result<ResponseData, ServiceError> {
|
|||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct Response<T> {
|
pub struct Response<T> {
|
||||||
pub data: T,
|
pub data: T,
|
||||||
pub meta: Option<Metadata>
|
pub meta: Option<Metadata>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
@@ -149,5 +165,5 @@ pub struct Metadata {
|
|||||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||||
pub struct Coordinate {
|
pub struct Coordinate {
|
||||||
pub lon: f64,
|
pub lon: f64,
|
||||||
pub lat: f64
|
pub lat: f64,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,10 +14,7 @@ pub struct ServiceError {
|
|||||||
|
|
||||||
impl ServiceError {
|
impl ServiceError {
|
||||||
pub fn new(status: u16, message: String) -> ServiceError {
|
pub fn new(status: u16, message: String) -> ServiceError {
|
||||||
ServiceError {
|
ServiceError { status, message }
|
||||||
status,
|
|
||||||
message,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_http_response(&self) -> HttpResponse {
|
pub fn to_http_response(&self) -> HttpResponse {
|
||||||
@@ -46,27 +43,24 @@ impl From<std::io::Error> for ServiceError {
|
|||||||
|
|
||||||
impl From<std::env::VarError> for ServiceError {
|
impl From<std::env::VarError> for ServiceError {
|
||||||
fn from(error: std::env::VarError) -> ServiceError {
|
fn from(error: std::env::VarError) -> ServiceError {
|
||||||
ServiceError::new(500, format!("Unknown environment variable error: {}", error))
|
ServiceError::new(
|
||||||
|
500,
|
||||||
|
format!("Unknown environment variable error: {}", error),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<DieselError> for ServiceError {
|
impl From<DieselError> for ServiceError {
|
||||||
fn from(error: DieselError) -> ServiceError {
|
fn from(error: DieselError) -> ServiceError {
|
||||||
match error {
|
match error {
|
||||||
DieselError::DatabaseError(kind, err) => {
|
DieselError::DatabaseError(kind, err) => match kind {
|
||||||
match kind {
|
|
||||||
diesel::result::DatabaseErrorKind::UniqueViolation => {
|
diesel::result::DatabaseErrorKind::UniqueViolation => {
|
||||||
ServiceError::new(409, err.message().to_string())
|
ServiceError::new(409, err.message().to_string())
|
||||||
},
|
|
||||||
_ => ServiceError::new(500, err.message().to_string())
|
|
||||||
}
|
}
|
||||||
|
_ => ServiceError::new(500, err.message().to_string()),
|
||||||
},
|
},
|
||||||
DieselError::NotFound => {
|
DieselError::NotFound => ServiceError::new(404, "The record was not found".to_string()),
|
||||||
ServiceError::new(404, "The record was not found".to_string())
|
DieselError::SerializationError(err) => ServiceError::new(422, err.to_string()),
|
||||||
},
|
|
||||||
DieselError::SerializationError(err) => {
|
|
||||||
ServiceError::new(422, err.to_string())
|
|
||||||
},
|
|
||||||
err => ServiceError::new(500, format!("Unknown Diesel error: {}", err)),
|
err => ServiceError::new(500, format!("Unknown Diesel error: {}", err)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -105,13 +99,24 @@ impl From<redis::RedisError> for ServiceError {
|
|||||||
impl From<s3::error::S3Error> for ServiceError {
|
impl From<s3::error::S3Error> for ServiceError {
|
||||||
fn from(error: s3::error::S3Error) -> ServiceError {
|
fn from(error: s3::error::S3Error) -> ServiceError {
|
||||||
match error {
|
match error {
|
||||||
s3::error::S3Error::Credentials(err) => ServiceError::new(500, format!("Unknown s3 credentials error: {}", err)),
|
s3::error::S3Error::Credentials(err) => {
|
||||||
s3::error::S3Error::FromUtf8(err) => ServiceError::new(500, format!("Unknown s3 from utf8 error: {}", err)),
|
ServiceError::new(500, format!("Unknown s3 credentials error: {}", err))
|
||||||
s3::error::S3Error::FmtError(err) => ServiceError::new(500, format!("Unknown s3 fmt error: {}", err)),
|
}
|
||||||
s3::error::S3Error::HeaderToStr(err) => ServiceError::new(500, format!("Unknown s3 header to str error: {}", err)),
|
s3::error::S3Error::FromUtf8(err) => {
|
||||||
s3::error::S3Error::HmacInvalidLength(err) => ServiceError::new(500, format!("Unknown s3 hmac invalid length error: {}", err)),
|
ServiceError::new(500, format!("Unknown s3 from utf8 error: {}", err))
|
||||||
s3::error::S3Error::Http(status, msg) => ServiceError::new(status, msg),
|
}
|
||||||
_ => ServiceError::new(500, format!("Unknown s3 error: {}", error))
|
s3::error::S3Error::FmtError(err) => {
|
||||||
|
ServiceError::new(500, format!("Unknown s3 fmt error: {}", err))
|
||||||
|
}
|
||||||
|
s3::error::S3Error::HeaderToStr(err) => {
|
||||||
|
ServiceError::new(500, format!("Unknown s3 header to str error: {}", err))
|
||||||
|
}
|
||||||
|
s3::error::S3Error::HmacInvalidLength(err) => ServiceError::new(
|
||||||
|
500,
|
||||||
|
format!("Unknown s3 hmac invalid length error: {}", err),
|
||||||
|
),
|
||||||
|
s3::error::S3Error::Http(error) => ServiceError::new(error.status_code().as_u16(), error.to_string()),
|
||||||
|
_ => ServiceError::new(500, format!("Unknown s3 error: {}", error)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,15 +14,15 @@ mod auth;
|
|||||||
mod db;
|
mod db;
|
||||||
mod error_handler;
|
mod error_handler;
|
||||||
mod metars;
|
mod metars;
|
||||||
mod users;
|
|
||||||
mod scheduler;
|
mod scheduler;
|
||||||
|
mod users;
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
dotenv().ok();
|
dotenv().ok();
|
||||||
env_logger::init_from_env(env_logger::Env::default().filter_or("RUST_LOG", "warn,service=info"));
|
env_logger::init_from_env(env_logger::Env::default().filter_or("RUST_LOG", "warn,service=info"));
|
||||||
db::init().await;
|
db::init().await;
|
||||||
// scheduler::update_airports();
|
scheduler::update_airports();
|
||||||
|
|
||||||
let host = env::var("SERVICE_HOST").unwrap_or("localhost".to_string());
|
let host = env::var("SERVICE_HOST").unwrap_or("localhost".to_string());
|
||||||
let port = env::var("SERVICE_PORT").unwrap_or("5000".to_string());
|
let port = env::var("SERVICE_PORT").unwrap_or("5000".to_string());
|
||||||
@@ -42,11 +42,12 @@ async fn main() -> std::io::Result<()> {
|
|||||||
.configure(auth::init_routes)
|
.configure(auth::init_routes)
|
||||||
.configure(users::init_routes)
|
.configure(users::init_routes)
|
||||||
})
|
})
|
||||||
.bind(format!("{}:{}", host, port)) {
|
.bind(format!("{}:{}", host, port))
|
||||||
|
{
|
||||||
Ok(b) => {
|
Ok(b) => {
|
||||||
info!("Binding server to {}:{}", host, port);
|
info!("Binding server to {}:{}", host, port);
|
||||||
b
|
b
|
||||||
},
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("Could not bind server: {}", err);
|
error!("Could not bind server: {}", err);
|
||||||
return Err(err);
|
return Err(err);
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ pub struct SkyCondition {
|
|||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub cloud_base_ft_agl: Option<i32>,
|
pub cloud_base_ft_agl: Option<i32>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub significant_convective_clouds: Option<String>
|
pub significant_convective_clouds: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for SkyCondition {
|
impl Default for SkyCondition {
|
||||||
@@ -66,7 +66,7 @@ pub struct RunwayVisualRange {
|
|||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub variable_visibility_high_ft: Option<String>,
|
pub variable_visibility_high_ft: Option<String>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub variable_visibility_low_ft: Option<String>
|
pub variable_visibility_low_ft: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for RunwayVisualRange {
|
impl Default for RunwayVisualRange {
|
||||||
@@ -75,7 +75,7 @@ impl Default for RunwayVisualRange {
|
|||||||
runway: "".to_string(),
|
runway: "".to_string(),
|
||||||
visibility_ft: None,
|
visibility_ft: None,
|
||||||
variable_visibility_high_ft: None,
|
variable_visibility_high_ft: None,
|
||||||
variable_visibility_low_ft: None
|
variable_visibility_low_ft: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -86,7 +86,7 @@ pub enum FlightCategory {
|
|||||||
MVFR,
|
MVFR,
|
||||||
LIFR,
|
LIFR,
|
||||||
IFR,
|
IFR,
|
||||||
UNKN
|
UNKN,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
@@ -132,7 +132,11 @@ impl Default for Metar {
|
|||||||
Metar {
|
Metar {
|
||||||
raw_text: "".to_string(),
|
raw_text: "".to_string(),
|
||||||
station_id: "".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::NaiveDateTime::parse_from_str(
|
||||||
|
"1970-01-01T00:00:00",
|
||||||
|
"%Y-%m-%dT%H:%M:%S",
|
||||||
|
)
|
||||||
|
.unwrap(),
|
||||||
temp_c: None,
|
temp_c: None,
|
||||||
dewpoint_c: None,
|
dewpoint_c: None,
|
||||||
wind_dir_degrees: None,
|
wind_dir_degrees: None,
|
||||||
@@ -164,7 +168,10 @@ impl Metar {
|
|||||||
metar.raw_text = metar_string.to_owned();
|
metar.raw_text = metar_string.to_owned();
|
||||||
let mut metar_parts: Vec<&str> = metar_string.split_whitespace().collect();
|
let mut metar_parts: Vec<&str> = metar_string.split_whitespace().collect();
|
||||||
if metar_parts.len() < 4 {
|
if metar_parts.len() < 4 {
|
||||||
warn!("Unable to parse METAR data in an unexpected format: {}", metar_string);
|
warn!(
|
||||||
|
"Unable to parse METAR data in an unexpected format: {}",
|
||||||
|
metar_string
|
||||||
|
);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -176,7 +183,10 @@ impl Metar {
|
|||||||
let observation_time = metar_parts[0];
|
let observation_time = metar_parts[0];
|
||||||
metar_parts.remove(0);
|
metar_parts.remove(0);
|
||||||
if observation_time.len() != 7 {
|
if observation_time.len() != 7 {
|
||||||
warn!("Unable to parse observation time in {}: {}", observation_time, metar_string);
|
warn!(
|
||||||
|
"Unable to parse observation time in {}: {}",
|
||||||
|
observation_time, metar_string
|
||||||
|
);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let observation_time_day = &observation_time[0..2];
|
let observation_time_day = &observation_time[0..2];
|
||||||
@@ -184,7 +194,8 @@ impl Metar {
|
|||||||
let observation_time_minute = &observation_time[4..6];
|
let observation_time_minute = &observation_time[4..6];
|
||||||
let current_time = chrono::Utc::now().naive_utc();
|
let current_time = chrono::Utc::now().naive_utc();
|
||||||
// Check if the observation time is from the previous month
|
// 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.parse::<u32>().unwrap() {
|
||||||
current_time.month() - 1
|
current_time.month() - 1
|
||||||
} else {
|
} else {
|
||||||
current_time.month()
|
current_time.month()
|
||||||
@@ -196,13 +207,22 @@ impl Metar {
|
|||||||
current_time.year()
|
current_time.year()
|
||||||
};
|
};
|
||||||
// Handle Daylight Savings Time
|
// Handle Daylight Savings Time
|
||||||
let observation_time_hour = if observation_time_month == 3 && observation_time_day.parse::<u32>().unwrap() < 14 {
|
let observation_time_hour =
|
||||||
|
if observation_time_month == 3 && observation_time_day.parse::<u32>().unwrap() < 14 {
|
||||||
observation_time_hour.parse::<u32>().unwrap() - 1
|
observation_time_hour.parse::<u32>().unwrap() - 1
|
||||||
} else {
|
} else {
|
||||||
observation_time_hour.parse::<u32>().unwrap()
|
observation_time_hour.parse::<u32>().unwrap()
|
||||||
};
|
};
|
||||||
let observation_time = format!("{}-{}-{}T{}:{}:00Z", observation_time_year, observation_time_month, observation_time_day, observation_time_hour, observation_time_minute);
|
let observation_time = format!(
|
||||||
metar.observation_time = chrono::NaiveDateTime::parse_from_str(&observation_time, "%Y-%m-%dT%H:%M:%SZ").unwrap();
|
"{}-{}-{}T{}:{}: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();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if metar_parts.is_empty() {
|
if metar_parts.is_empty() {
|
||||||
@@ -224,14 +244,22 @@ impl Metar {
|
|||||||
|
|
||||||
// Wind Direction and Speed
|
// Wind Direction and Speed
|
||||||
let wind_re = regex::Regex::new(r"^(?:[0-9]{3}|VRB)[0-9]{2}(?:KT|MPS)$").unwrap();
|
let wind_re = regex::Regex::new(r"^(?:[0-9]{3}|VRB)[0-9]{2}(?:KT|MPS)$").unwrap();
|
||||||
let wind_gust_re = regex::Regex::new(r"^(?:[0-9]{3}|VRB)[0-9]{2}G[0-9]{2}(?:KT|MPS)$").unwrap();
|
let wind_gust_re =
|
||||||
|
regex::Regex::new(r"^(?:[0-9]{3}|VRB)[0-9]{2}G[0-9]{2}(?:KT|MPS)$").unwrap();
|
||||||
// Handle input error where there is a space between the numbers and units
|
// Handle input error where there is a space between the numbers and units
|
||||||
let mut value: Option<String> = None;
|
let mut value: Option<String> = None;
|
||||||
if metar_parts.len() >= 2 && metar_parts[0].len() == 5 && (metar_parts[1] == "KT" || metar_parts[1] == "MPS") {
|
if metar_parts.len() >= 2
|
||||||
|
&& metar_parts[0].len() == 5
|
||||||
|
&& (metar_parts[1] == "KT" || metar_parts[1] == "MPS")
|
||||||
|
{
|
||||||
value = Some(format!("{}{}", metar_parts[0], metar_parts[1]));
|
value = Some(format!("{}{}", metar_parts[0], metar_parts[1]));
|
||||||
metar_parts.remove(0);
|
metar_parts.remove(0);
|
||||||
metar_parts.remove(0);
|
metar_parts.remove(0);
|
||||||
} else if metar_parts.len() >= 2 && metar_parts[0].len() == 7 && metar_parts[0].contains("G") && (metar_parts[1] == "KT" || metar_parts[1] == "MPS") {
|
} else if metar_parts.len() >= 2
|
||||||
|
&& metar_parts[0].len() == 7
|
||||||
|
&& metar_parts[0].contains("G")
|
||||||
|
&& (metar_parts[1] == "KT" || metar_parts[1] == "MPS")
|
||||||
|
{
|
||||||
value = Some(format!("{}{}", metar_parts[0], metar_parts[1]));
|
value = Some(format!("{}{}", metar_parts[0], metar_parts[1]));
|
||||||
metar_parts.remove(0);
|
metar_parts.remove(0);
|
||||||
metar_parts.remove(0);
|
metar_parts.remove(0);
|
||||||
@@ -267,7 +295,7 @@ impl Metar {
|
|||||||
metar.wind_speed_kt = Some(wind_speed_kt.parse::<f64>().unwrap());
|
metar.wind_speed_kt = Some(wind_speed_kt.parse::<f64>().unwrap());
|
||||||
metar.wind_gust_kt = Some(wind_gust_kt.parse::<f64>().unwrap());
|
metar.wind_gust_kt = Some(wind_gust_kt.parse::<f64>().unwrap());
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
None => {}
|
None => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -289,29 +317,67 @@ impl Metar {
|
|||||||
let visibility_left = visibility_parts[0];
|
let visibility_left = visibility_parts[0];
|
||||||
let visibility_right = visibility_parts[1].parse::<f64>().unwrap();
|
let visibility_right = visibility_parts[1].parse::<f64>().unwrap();
|
||||||
if visibility_left.starts_with("M") {
|
if visibility_left.starts_with("M") {
|
||||||
format!("M{}", visibility_left[1..visibility_left.len()].parse::<f64>().unwrap() / visibility_right)
|
format!(
|
||||||
|
"M{}",
|
||||||
|
visibility_left[1..visibility_left.len()]
|
||||||
|
.parse::<f64>()
|
||||||
|
.unwrap()
|
||||||
|
/ visibility_right
|
||||||
|
)
|
||||||
} else if visibility_left.starts_with("P") {
|
} else if visibility_left.starts_with("P") {
|
||||||
format!("P{}", visibility_left[1..visibility_left.len()].parse::<f64>().unwrap() / visibility_right)
|
format!(
|
||||||
|
"P{}",
|
||||||
|
visibility_left[1..visibility_left.len()]
|
||||||
|
.parse::<f64>()
|
||||||
|
.unwrap()
|
||||||
|
/ visibility_right
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
format!("{}", visibility_left.parse::<f64>().unwrap() / visibility_right)
|
format!(
|
||||||
|
"{}",
|
||||||
|
visibility_left.parse::<f64>().unwrap() / visibility_right
|
||||||
|
)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
visibility_str.to_string()
|
visibility_str.to_string()
|
||||||
};
|
};
|
||||||
metar.visibility_statute_mi = Some(visibility);
|
metar.visibility_statute_mi = Some(visibility);
|
||||||
} else if !metar_parts.is_empty() && metar_parts[0].parse::<f64>().is_ok() && metar_parts.len() > 1 && visibility_re.is_match(metar_parts[1]) {
|
} else if !metar_parts.is_empty()
|
||||||
|
&& metar_parts[0].parse::<f64>().is_ok()
|
||||||
|
&& metar_parts.len() > 1
|
||||||
|
&& visibility_re.is_match(metar_parts[1])
|
||||||
|
{
|
||||||
let visibility_whole = metar_parts[0].parse::<f64>().unwrap();
|
let visibility_whole = metar_parts[0].parse::<f64>().unwrap();
|
||||||
metar_parts.remove(0);
|
metar_parts.remove(0);
|
||||||
let visibility_parts: Vec<&str> = metar_parts[0].split("/").collect();
|
let visibility_parts: Vec<&str> = metar_parts[0].split("/").collect();
|
||||||
metar_parts.remove(0);
|
metar_parts.remove(0);
|
||||||
let visibility_left = visibility_parts[0];
|
let visibility_left = visibility_parts[0];
|
||||||
let visibility_right = visibility_parts[1][0..visibility_parts[1].len() - 2].parse::<f64>().unwrap();
|
let visibility_right = visibility_parts[1][0..visibility_parts[1].len() - 2]
|
||||||
|
.parse::<f64>()
|
||||||
|
.unwrap();
|
||||||
let visibility = if visibility_left.starts_with("M") {
|
let visibility = if visibility_left.starts_with("M") {
|
||||||
format!("M{}", visibility_whole + (visibility_left[1..visibility_left.len()].parse::<f64>().unwrap() / visibility_right))
|
format!(
|
||||||
|
"M{}",
|
||||||
|
visibility_whole
|
||||||
|
+ (visibility_left[1..visibility_left.len()]
|
||||||
|
.parse::<f64>()
|
||||||
|
.unwrap()
|
||||||
|
/ visibility_right)
|
||||||
|
)
|
||||||
} else if visibility_left.starts_with("P") {
|
} else if visibility_left.starts_with("P") {
|
||||||
format!("P{}", visibility_whole + (visibility_left[1..visibility_left.len()].parse::<f64>().unwrap() / visibility_right))
|
format!(
|
||||||
|
"P{}",
|
||||||
|
visibility_whole
|
||||||
|
+ (visibility_left[1..visibility_left.len()]
|
||||||
|
.parse::<f64>()
|
||||||
|
.unwrap()
|
||||||
|
/ visibility_right)
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
format!("{}", visibility_whole + (visibility_left.parse::<f64>().unwrap() / visibility_right))
|
format!(
|
||||||
|
"{}",
|
||||||
|
visibility_whole + (visibility_left.parse::<f64>().unwrap() / visibility_right)
|
||||||
|
)
|
||||||
};
|
};
|
||||||
metar.visibility_statute_mi = Some(visibility);
|
metar.visibility_statute_mi = Some(visibility);
|
||||||
} else if !metar_parts.is_empty() && visibility_re_m.is_match(metar_parts[0]) {
|
} else if !metar_parts.is_empty() && visibility_re_m.is_match(metar_parts[0]) {
|
||||||
@@ -328,8 +394,11 @@ impl Metar {
|
|||||||
|
|
||||||
// Runway Visual Range
|
// Runway Visual Range
|
||||||
let rvr_re = regex::Regex::new(r"^R[0-9]{1,3}(?:L|R|C)?/[PM]?[0-9]{4}FT$").unwrap();
|
let rvr_re = regex::Regex::new(r"^R[0-9]{1,3}(?:L|R|C)?/[PM]?[0-9]{4}FT$").unwrap();
|
||||||
let variable_rvr_re = regex::Regex::new(r"^R[0-9]{1,3}(?:L|R|C)?/[PM]?[0-9]{4}V[PM]?[0-9]{4}FT$").unwrap();
|
let variable_rvr_re =
|
||||||
while !metar_parts.is_empty() && (rvr_re.is_match(metar_parts[0]) || variable_rvr_re.is_match(metar_parts[0])) {
|
regex::Regex::new(r"^R[0-9]{1,3}(?:L|R|C)?/[PM]?[0-9]{4}V[PM]?[0-9]{4}FT$").unwrap();
|
||||||
|
while !metar_parts.is_empty()
|
||||||
|
&& (rvr_re.is_match(metar_parts[0]) || variable_rvr_re.is_match(metar_parts[0]))
|
||||||
|
{
|
||||||
let rvr_string = metar_parts[0];
|
let rvr_string = metar_parts[0];
|
||||||
metar_parts.remove(0);
|
metar_parts.remove(0);
|
||||||
let mut rvr = RunwayVisualRange::default();
|
let mut rvr = RunwayVisualRange::default();
|
||||||
@@ -340,7 +409,10 @@ impl Metar {
|
|||||||
} else {
|
} else {
|
||||||
let rvr_variable_parts: Vec<&str> = rvr_parts[1].split("V").collect();
|
let rvr_variable_parts: Vec<&str> = rvr_parts[1].split("V").collect();
|
||||||
if rvr_variable_parts.len() != 2 {
|
if rvr_variable_parts.len() != 2 {
|
||||||
warn!("Unable to parse runway visual range in {}: {}", rvr_string, metar_string);
|
warn!(
|
||||||
|
"Unable to parse runway visual range in {}: {}",
|
||||||
|
rvr_string, metar_string
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
rvr.variable_visibility_low_ft = Some(rvr_variable_parts[0].to_string());
|
rvr.variable_visibility_low_ft = Some(rvr_variable_parts[0].to_string());
|
||||||
rvr.variable_visibility_high_ft = Some(rvr_variable_parts[1].to_string());
|
rvr.variable_visibility_high_ft = Some(rvr_variable_parts[1].to_string());
|
||||||
@@ -351,8 +423,13 @@ impl Metar {
|
|||||||
// Weather Phenomena
|
// Weather Phenomena
|
||||||
let wx_intensity = "(?:[+-]|VC)?";
|
let wx_intensity = "(?:[+-]|VC)?";
|
||||||
let wx_descriptor = "(?:MI|PR|BC|DR|BL|SH|TS|FZ)?";
|
let wx_descriptor = "(?:MI|PR|BC|DR|BL|SH|TS|FZ)?";
|
||||||
let wx_precipitation = "(?:DZ|RA|SN|SG|IC|PL|GR|GS|UP|BR|FG|FU|VA|DU|SA|HZ|PY|PO|SQ|FC|SS|DS)?";
|
let wx_precipitation =
|
||||||
let wx_re = regex::Regex::new(&format!(r"^{}{}{}$", wx_intensity, wx_descriptor, wx_precipitation)).unwrap();
|
"(?:DZ|RA|SN|SG|IC|PL|GR|GS|UP|BR|FG|FU|VA|DU|SA|HZ|PY|PO|SQ|FC|SS|DS)?";
|
||||||
|
let wx_re = regex::Regex::new(&format!(
|
||||||
|
r"^{}{}{}$",
|
||||||
|
wx_intensity, wx_descriptor, wx_precipitation
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
while !metar_parts.is_empty() && wx_re.is_match(metar_parts[0]) {
|
while !metar_parts.is_empty() && wx_re.is_match(metar_parts[0]) {
|
||||||
metar.weather_phenomena.push(metar_parts[0].to_string());
|
metar.weather_phenomena.push(metar_parts[0].to_string());
|
||||||
metar_parts.remove(0);
|
metar_parts.remove(0);
|
||||||
@@ -363,11 +440,13 @@ impl Metar {
|
|||||||
metar.sky_condition.push(SkyCondition {
|
metar.sky_condition.push(SkyCondition {
|
||||||
sky_cover: "CLR".to_string(),
|
sky_cover: "CLR".to_string(),
|
||||||
cloud_base_ft_agl: None,
|
cloud_base_ft_agl: None,
|
||||||
significant_convective_clouds: None
|
significant_convective_clouds: None,
|
||||||
});
|
});
|
||||||
metar_parts.remove(0);
|
metar_parts.remove(0);
|
||||||
}
|
}
|
||||||
let sky_condition_re = regex::Regex::new(r"^(?:CLR|SKC|NSC|NCD|(?:FEW|SCT|BKN|OVC|VV)([0-9/]{3})?(?:CB|TCU)?)$").unwrap();
|
let sky_condition_re =
|
||||||
|
regex::Regex::new(r"^(?:CLR|SKC|NSC|NCD|(?:FEW|SCT|BKN|OVC|VV)([0-9/]{3})?(?:CB|TCU)?)$")
|
||||||
|
.unwrap();
|
||||||
while !metar_parts.is_empty() && sky_condition_re.is_match(metar_parts[0]) {
|
while !metar_parts.is_empty() && sky_condition_re.is_match(metar_parts[0]) {
|
||||||
let sky_condition_string = metar_parts[0];
|
let sky_condition_string = metar_parts[0];
|
||||||
metar_parts.remove(0);
|
metar_parts.remove(0);
|
||||||
@@ -388,7 +467,10 @@ impl Metar {
|
|||||||
sky_condition.cloud_base_ft_agl = match cloud_base_ft_agl.parse::<i32>() {
|
sky_condition.cloud_base_ft_agl = match cloud_base_ft_agl.parse::<i32>() {
|
||||||
Ok(c) => Some(c * 100),
|
Ok(c) => Some(c * 100),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
warn!("Unable to parse cloud base in {}: {}", sky_condition_string, err);
|
warn!(
|
||||||
|
"Unable to parse cloud base in {}: {}",
|
||||||
|
sky_condition_string, err
|
||||||
|
);
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -478,7 +560,9 @@ impl Metar {
|
|||||||
let remark = metar_parts[0];
|
let remark = metar_parts[0];
|
||||||
metar_parts.remove(0);
|
metar_parts.remove(0);
|
||||||
if remark == "AO1" {
|
if remark == "AO1" {
|
||||||
metar.quality_control_flags.auto_station_without_precipication = Some(true);
|
metar
|
||||||
|
.quality_control_flags
|
||||||
|
.auto_station_without_precipication = Some(true);
|
||||||
} else if remark == "AO2" {
|
} else if remark == "AO2" {
|
||||||
metar.quality_control_flags.auto_station_with_precipication = Some(true);
|
metar.quality_control_flags.auto_station_with_precipication = Some(true);
|
||||||
} else if remark == "$" {
|
} else if remark == "$" {
|
||||||
@@ -516,7 +600,10 @@ impl Metar {
|
|||||||
|
|
||||||
// Skip unexpected fields
|
// Skip unexpected fields
|
||||||
if !metar_parts.is_empty() {
|
if !metar_parts.is_empty() {
|
||||||
warn!("Skipping unexpected field: '{}' ({})", metar_parts[0], metar_string);
|
warn!(
|
||||||
|
"Skipping unexpected field: '{}' ({})",
|
||||||
|
metar_parts[0], metar_string
|
||||||
|
);
|
||||||
metar_parts.remove(0);
|
metar_parts.remove(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -533,7 +620,7 @@ impl Metar {
|
|||||||
v.parse::<f64>().unwrap()
|
v.parse::<f64>().unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => 5.0 // Assume VFR if no visibility is present
|
None => 5.0, // Assume VFR if no visibility is present
|
||||||
};
|
};
|
||||||
// Ceiling is the lowest cloud base that is BKN or OVC
|
// Ceiling is the lowest cloud base that is BKN or OVC
|
||||||
let ceiling = match metar.sky_condition.first() {
|
let ceiling = match metar.sky_condition.first() {
|
||||||
@@ -543,13 +630,13 @@ impl Metar {
|
|||||||
} else if s.sky_cover == "BKN" || s.sky_cover == "OVC" {
|
} else if s.sky_cover == "BKN" || s.sky_cover == "OVC" {
|
||||||
match s.cloud_base_ft_agl {
|
match s.cloud_base_ft_agl {
|
||||||
Some(c) => c as f64,
|
Some(c) => c as f64,
|
||||||
None => 0.0
|
None => 0.0,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
3000.0 // Assume VFR if no BKN or OVC sky condition is present
|
3000.0 // Assume VFR if no BKN or OVC sky condition is present
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
None => 3000.0 // Assume VFR if no sky condition is present
|
None => 3000.0, // Assume VFR if no sky condition is present
|
||||||
};
|
};
|
||||||
if visibility >= 5.0 && ceiling >= 3000.0 {
|
if visibility >= 5.0 && ceiling >= 3000.0 {
|
||||||
metar.flight_category = FlightCategory::VFR;
|
metar.flight_category = FlightCategory::VFR;
|
||||||
@@ -564,19 +651,22 @@ impl Metar {
|
|||||||
|
|
||||||
metars.push(metar);
|
metars.push(metar);
|
||||||
}
|
}
|
||||||
return Ok(metars)
|
return Ok(metars);
|
||||||
}
|
}
|
||||||
|
|
||||||
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: &Vec<&str>) -> Vec<String> {
|
||||||
let mut missing_metar_icaos: Vec<String> = vec![];
|
let mut missing_metar_icaos: Vec<String> = vec![];
|
||||||
let current_time = chrono::Local::now().naive_local().timestamp();
|
let current_time = chrono::Local::now().naive_local().and_utc().timestamp();
|
||||||
let db_metars_set: HashSet<&str> = db_metars.iter().map(|icao| icao.station_id.as_str()).collect();
|
let db_metars_set: HashSet<&str> = db_metars
|
||||||
|
.iter()
|
||||||
|
.map(|icao| icao.station_id.as_str())
|
||||||
|
.collect();
|
||||||
let station_icaos_set: HashSet<&str> = station_icaos.to_owned().into_iter().collect();
|
let station_icaos_set: HashSet<&str> = station_icaos.to_owned().into_iter().collect();
|
||||||
for difference in db_metars_set.symmetric_difference(&station_icaos_set) {
|
for difference in db_metars_set.symmetric_difference(&station_icaos_set) {
|
||||||
missing_metar_icaos.push(difference.to_string());
|
missing_metar_icaos.push(difference.to_string());
|
||||||
}
|
}
|
||||||
for metar in db_metars {
|
for metar in db_metars {
|
||||||
if current_time > (metar.observation_time.timestamp() + 3600) {
|
if current_time > (metar.observation_time.and_utc().timestamp() + 3600) {
|
||||||
trace!("{} METAR data is outdated", metar.station_id);
|
trace!("{} METAR data is outdated", metar.station_id);
|
||||||
missing_metar_icaos.push(metar.station_id.to_string());
|
missing_metar_icaos.push(metar.station_id.to_string());
|
||||||
}
|
}
|
||||||
@@ -587,7 +677,10 @@ impl Metar {
|
|||||||
async fn get_remote_metars(icaos: Vec<String>) -> Result<Vec<Metar>, ServiceError> {
|
async fn get_remote_metars(icaos: Vec<String>) -> Result<Vec<Metar>, ServiceError> {
|
||||||
let gov_api_url = std::env::var("GOV_API_URL").expect("GOV_API_URL must be set");
|
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
|
// Query the remote API for the missing METAR data 10 at a time
|
||||||
let icao_chunks = icaos.chunks(10).map(|chunk| chunk.join(",")).collect::<Vec<String>>();
|
let icao_chunks = icaos
|
||||||
|
.chunks(10)
|
||||||
|
.map(|chunk| chunk.join(","))
|
||||||
|
.collect::<Vec<String>>();
|
||||||
let mut metars: Vec<Metar> = vec![];
|
let mut metars: Vec<Metar> = vec![];
|
||||||
for icao_chunk in icao_chunks {
|
for icao_chunk in icao_chunks {
|
||||||
let url = format!("{}/metar.php?ids={}", gov_api_url, icao_chunk);
|
let url = format!("{}/metar.php?ids={}", gov_api_url, icao_chunk);
|
||||||
@@ -595,20 +688,37 @@ impl Metar {
|
|||||||
Ok(r) => {
|
Ok(r) => {
|
||||||
// Check if the status code is 200
|
// Check if the status code is 200
|
||||||
if r.status() != 200 {
|
if r.status() != 200 {
|
||||||
return Err(ServiceError::new(500, format!("Unable to get METAR request: {}", r.status())));
|
return Err(ServiceError::new(
|
||||||
|
500,
|
||||||
|
format!("Unable to get METAR request: {}", r.status()),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
match r.text().await {
|
match r.text().await {
|
||||||
Ok(r) => {
|
Ok(r) => {
|
||||||
let metar_chunk = r.trim().split("\n").filter(|m| !m.trim().is_empty()).collect();
|
let metar_chunk = r
|
||||||
|
.trim()
|
||||||
|
.split("\n")
|
||||||
|
.filter(|m| !m.trim().is_empty())
|
||||||
|
.collect();
|
||||||
match Metar::parse(metar_chunk) {
|
match Metar::parse(metar_chunk) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(err) => return Err(err)
|
Err(err) => return Err(err),
|
||||||
}
|
}
|
||||||
},
|
|
||||||
Err(err) => return Err(ServiceError::new(500, format!("Unable to parse METAR request: {}", err)))
|
|
||||||
}
|
}
|
||||||
},
|
Err(err) => {
|
||||||
Err(err) => return Err(ServiceError::new(500, format!("Unable to get METAR request: {}", err)))
|
return Err(ServiceError::new(
|
||||||
|
500,
|
||||||
|
format!("Unable to parse METAR request: {}", err),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
return Err(ServiceError::new(
|
||||||
|
500,
|
||||||
|
format!("Unable to get METAR request: {}", err),
|
||||||
|
))
|
||||||
|
}
|
||||||
};
|
};
|
||||||
metars.append(&mut m);
|
metars.append(&mut m);
|
||||||
}
|
}
|
||||||
@@ -633,7 +743,7 @@ impl Metar {
|
|||||||
icao: metar.station_id.to_string(),
|
icao: metar.station_id.to_string(),
|
||||||
observation_time: metar.observation_time,
|
observation_time: metar.observation_time,
|
||||||
raw_text: metar.raw_text.to_string(),
|
raw_text: metar.raw_text.to_string(),
|
||||||
data: serde_json::to_value(metar).unwrap()
|
data: serde_json::to_value(metar).unwrap(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return insert_metars;
|
return insert_metars;
|
||||||
@@ -648,7 +758,7 @@ impl Metar {
|
|||||||
|
|
||||||
let mut db_metars = match QueryMetar::get_all(&icaos) {
|
let mut db_metars = match QueryMetar::get_all(&icaos) {
|
||||||
Ok(m) => Self::from_query(m),
|
Ok(m) => Self::from_query(m),
|
||||||
Err(err) => return Err(err)
|
Err(err) => return Err(err),
|
||||||
};
|
};
|
||||||
|
|
||||||
let missing_icaos = Self::get_missing_metar_icaos(&db_metars, &icaos);
|
let missing_icaos = Self::get_missing_metar_icaos(&db_metars, &icaos);
|
||||||
@@ -656,13 +766,17 @@ impl Metar {
|
|||||||
return Ok(db_metars);
|
return Ok(db_metars);
|
||||||
}
|
}
|
||||||
trace!("Retrieving missing METAR data for {:?}", missing_icaos);
|
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 missing_icaos_string: Vec<String> = missing_icaos
|
||||||
|
.iter()
|
||||||
|
.map(|icao| format!("{}", icao.to_string()))
|
||||||
|
.collect();
|
||||||
let mut airports: Vec<QueryAirport> = vec![];
|
let mut airports: Vec<QueryAirport> = vec![];
|
||||||
missing_icaos_string.clone().iter().for_each(|icao| {
|
missing_icaos_string
|
||||||
match QueryAirport::get(icao) {
|
.clone()
|
||||||
|
.iter()
|
||||||
|
.for_each(|icao| match QueryAirport::get(icao) {
|
||||||
Ok(a) => airports.push(a),
|
Ok(a) => airports.push(a),
|
||||||
Err(_) => {}
|
Err(_) => {}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
let missing_result = Self::get_remote_metars(missing_icaos_string).await;
|
let missing_result = Self::get_remote_metars(missing_icaos_string).await;
|
||||||
let mut missing_metars = match missing_result {
|
let mut missing_metars = match missing_result {
|
||||||
@@ -676,11 +790,14 @@ impl Metar {
|
|||||||
let insert_metars = Self::to_insert(&missing_metars);
|
let insert_metars = Self::to_insert(&missing_metars);
|
||||||
match InsertMetar::insert(&insert_metars) {
|
match InsertMetar::insert(&insert_metars) {
|
||||||
Ok(rows) => trace!("Inserted {} metar rows", rows),
|
Ok(rows) => trace!("Inserted {} metar rows", rows),
|
||||||
Err(err) => warn!("Unable to insert metar data; {}", err)
|
Err(err) => warn!("Unable to insert metar data; {}", err),
|
||||||
};
|
};
|
||||||
// Update airports with the appropriate has_metar flag
|
// Update airports with the appropriate has_metar flag
|
||||||
airports.iter().for_each(|airport| {
|
airports.iter().for_each(|airport| {
|
||||||
if missing_metars.iter().any(|metar| metar.station_id == airport.icao) {
|
if missing_metars
|
||||||
|
.iter()
|
||||||
|
.any(|metar| metar.station_id == airport.icao)
|
||||||
|
{
|
||||||
let updated = QueryAirport {
|
let updated = QueryAirport {
|
||||||
icao: airport.icao.to_string(),
|
icao: airport.icao.to_string(),
|
||||||
category: airport.category.to_string(),
|
category: airport.category.to_string(),
|
||||||
@@ -691,11 +808,11 @@ impl Metar {
|
|||||||
municipality: airport.municipality.to_string(),
|
municipality: airport.municipality.to_string(),
|
||||||
has_metar: true,
|
has_metar: true,
|
||||||
point: airport.point,
|
point: airport.point,
|
||||||
data: airport.data.to_owned()
|
data: airport.data.to_owned(),
|
||||||
};
|
};
|
||||||
match QueryAirport::update(updated) {
|
match QueryAirport::update(updated) {
|
||||||
Ok(_) => {},
|
Ok(_) => {}
|
||||||
Err(err) => warn!("Unable to update airport with has_metar flag; {}", err)
|
Err(err) => warn!("Unable to update airport with has_metar flag; {}", err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -713,15 +830,21 @@ struct InsertMetar {
|
|||||||
icao: String,
|
icao: String,
|
||||||
observation_time: chrono::NaiveDateTime,
|
observation_time: chrono::NaiveDateTime,
|
||||||
raw_text: String,
|
raw_text: String,
|
||||||
data: serde_json::Value
|
data: serde_json::Value,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InsertMetar {
|
impl InsertMetar {
|
||||||
fn insert(metars: &Vec<Self>) -> Result<usize, ServiceError> {
|
fn insert(metars: &Vec<Self>) -> Result<usize, ServiceError> {
|
||||||
let mut conn = db::connection()?;
|
let mut conn = db::connection()?;
|
||||||
match diesel::insert_into(metars::table).values(metars).execute(&mut conn) {
|
match diesel::insert_into(metars::table)
|
||||||
|
.values(metars)
|
||||||
|
.execute(&mut conn)
|
||||||
|
{
|
||||||
Ok(rows) => Ok(rows),
|
Ok(rows) => Ok(rows),
|
||||||
Err(err) => Err(ServiceError { status: 500, message: format!("{}", err) })
|
Err(err) => Err(ServiceError {
|
||||||
|
status: 500,
|
||||||
|
message: format!("{}", err),
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -733,14 +856,25 @@ struct QueryMetar {
|
|||||||
icao: String,
|
icao: String,
|
||||||
observation_time: chrono::NaiveDateTime,
|
observation_time: chrono::NaiveDateTime,
|
||||||
raw_text: String,
|
raw_text: String,
|
||||||
data: serde_json::Value
|
data: serde_json::Value,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl QueryMetar {
|
impl QueryMetar {
|
||||||
fn get_all(icaos: &Vec<&str>) -> Result<Vec<QueryMetar>, ServiceError> {
|
fn get_all(icaos: &Vec<&str>) -> Result<Vec<QueryMetar>, ServiceError> {
|
||||||
// Sanitize search to only allow [a-zA-Z0-9]
|
// Sanitize search to only allow [a-zA-Z0-9]
|
||||||
let icaos = icaos.iter().map(|icao| icao.chars().filter(|c| c.is_alphanumeric()).collect::<String>()).collect::<Vec<String>>();
|
let icaos = icaos
|
||||||
let station_query: Vec<String> = icaos.iter().map(|icao| format!("'{}'", icao.to_string())).collect();
|
.iter()
|
||||||
|
.map(|icao| {
|
||||||
|
icao
|
||||||
|
.chars()
|
||||||
|
.filter(|c| c.is_alphanumeric())
|
||||||
|
.collect::<String>()
|
||||||
|
})
|
||||||
|
.collect::<Vec<String>>();
|
||||||
|
let station_query: Vec<String> = icaos
|
||||||
|
.iter()
|
||||||
|
.map(|icao| format!("'{}'", icao.to_string()))
|
||||||
|
.collect();
|
||||||
let mut conn = db::connection()?;
|
let mut conn = db::connection()?;
|
||||||
let db_metars: Vec<Self> = match sql_query(
|
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(","))
|
format!("SELECT DISTINCT ON (icao) * FROM metars WHERE icao IN ({}) ORDER BY icao, observation_time DESC", station_query.join(","))
|
||||||
|
|||||||
@@ -7,12 +7,12 @@ use serde::{Deserialize, Serialize};
|
|||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct MetarsResponse {
|
pub struct MetarsResponse {
|
||||||
pub data: Vec<Metar>,
|
pub data: Vec<Metar>,
|
||||||
pub meta: Metadata
|
pub meta: Metadata,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
struct GetAllParameters {
|
struct GetAllParameters {
|
||||||
icaos: Option<String>
|
icaos: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("metars")]
|
#[get("metars")]
|
||||||
@@ -21,14 +21,16 @@ async fn get_all(req: HttpRequest) -> HttpResponse {
|
|||||||
let icao_option = params.icaos.clone();
|
let icao_option = params.icaos.clone();
|
||||||
let icao_string = match icao_option {
|
let icao_string = match icao_option {
|
||||||
Some(i) => i,
|
Some(i) => i,
|
||||||
None => return HttpResponse::UnprocessableEntity().body("Missing icaos parameter")
|
None => return HttpResponse::UnprocessableEntity().body("Missing icaos parameter"),
|
||||||
};
|
};
|
||||||
|
|
||||||
let airports = match web::block(|| Ok::<_, ServiceError>(async {Metar::get_all(icao_string).await}))
|
let airports =
|
||||||
|
match web::block(|| Ok::<_, ServiceError>(async { Metar::get_all(icao_string).await }))
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.await {
|
.await
|
||||||
|
{
|
||||||
Ok(a) => a,
|
Ok(a) => a,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("{}", err);
|
error!("{}", err);
|
||||||
@@ -37,7 +39,12 @@ async fn get_all(req: HttpRequest) -> HttpResponse {
|
|||||||
};
|
};
|
||||||
HttpResponse::Ok().json(MetarsResponse {
|
HttpResponse::Ok().json(MetarsResponse {
|
||||||
data: airports,
|
data: airports,
|
||||||
meta: Metadata { page: 0, limit: 0, pages: 0, total: 0 }
|
meta: Metadata {
|
||||||
|
page: 0,
|
||||||
|
limit: 0,
|
||||||
|
pages: 0,
|
||||||
|
total: 0,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
use tokio::time::{sleep, Duration};
|
use tokio::time::{sleep, Duration};
|
||||||
use log::{warn, debug, trace};
|
|
||||||
|
|
||||||
use crate::airports::{QueryAirport, QueryFilters};
|
use crate::airports::{QueryAirport, QueryFilters};
|
||||||
use crate::metars::Metar;
|
use crate::metars::Metar;
|
||||||
@@ -9,38 +8,36 @@ pub fn update_airports() {
|
|||||||
let mut airports: Vec<QueryAirport> = vec![];
|
let mut airports: Vec<QueryAirport> = vec![];
|
||||||
let limit = 100;
|
let limit = 100;
|
||||||
loop {
|
loop {
|
||||||
debug!("METAR update start");
|
log::debug!("METAR update start");
|
||||||
let total = match QueryAirport::get_count(&QueryFilters::default()) {
|
let total = match QueryAirport::get_count(&QueryFilters::default()) {
|
||||||
Ok(t) => t,
|
Ok(t) => t,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
warn!("{}", err);
|
log::warn!("{}", err);
|
||||||
break
|
break;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if total != airports.len() as i64 {
|
if total != airports.len() as i64 {
|
||||||
debug!("{} cached airports, expected {}", airports.len(), total);
|
log::debug!("{} cached airports, expected {}", airports.len(), total);
|
||||||
airports = vec![];
|
airports = vec![];
|
||||||
let pages = ((total as f32) / (if limit <= 0 { 1 } else { limit } as f32)).ceil() as i32;
|
let pages = ((total as f32) / (if limit <= 0 { 1 } else { limit } as f32)).ceil() as i32;
|
||||||
for page in 1..(pages + 1) {
|
for page in 1..(pages + 1) {
|
||||||
match QueryAirport::get_all(&QueryFilters::default(), limit, page) {
|
match QueryAirport::get_all(&QueryFilters::default(), limit, page) {
|
||||||
Ok(mut a) => {
|
Ok(mut a) => airports.append(&mut a),
|
||||||
airports.append(&mut a)
|
|
||||||
},
|
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
warn!("{}", err);
|
log::warn!("{}", err);
|
||||||
break
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
debug!("Updating {} airport METARS", airports.len());
|
log::debug!("Updating {} airport METARS", airports.len());
|
||||||
|
|
||||||
let airport_icaos: Vec<String> = airports.iter().map(|a| a.icao.to_string()).collect();
|
let airport_icaos: Vec<String> = airports.iter().map(|a| a.icao.to_string()).collect();
|
||||||
let mut peekable = airport_icaos.into_iter().peekable();
|
let mut peekable = airport_icaos.into_iter().peekable();
|
||||||
let mut observation_time = chrono::Utc::now().timestamp();
|
let mut observation_time = chrono::Utc::now().timestamp();
|
||||||
|
|
||||||
if peekable.peek().is_none() {
|
if peekable.peek().is_none() {
|
||||||
debug!("No airports to update, sleeping for 1 hour");
|
log::debug!("No airports to update, sleeping for 1 hour");
|
||||||
sleep(Duration::from_secs(3600)).await;
|
sleep(Duration::from_secs(3600)).await;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -48,29 +45,29 @@ pub fn update_airports() {
|
|||||||
while peekable.peek().is_some() {
|
while peekable.peek().is_some() {
|
||||||
let chunk: Vec<String> = peekable.by_ref().take(limit as usize).collect();
|
let chunk: Vec<String> = peekable.by_ref().take(limit as usize).collect();
|
||||||
let icao_string = chunk.join(",");
|
let icao_string = chunk.join(",");
|
||||||
trace!("Updating METARS for: {}", icao_string);
|
log::trace!("Updating METARS for: {}", icao_string);
|
||||||
match Metar::get_all(icao_string).await {
|
match Metar::get_all(icao_string).await {
|
||||||
Ok(metars) => {
|
Ok(metars) => {
|
||||||
// Find the oldest observation time
|
// Find the oldest observation time
|
||||||
for metar in metars {
|
for metar in metars {
|
||||||
if metar.observation_time.timestamp() < observation_time {
|
if metar.observation_time.and_utc().timestamp() < observation_time {
|
||||||
observation_time = metar.observation_time.timestamp();
|
observation_time = metar.observation_time.and_utc().timestamp();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
warn!("{}", err);
|
log::warn!("{}", err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Sleep for 100ms between chunks to avoid rate limiting
|
// Sleep for 100ms between chunks to avoid rate limiting
|
||||||
sleep(Duration::from_millis(100)).await;
|
sleep(Duration::from_millis(100)).await;
|
||||||
}
|
}
|
||||||
debug!("METAR update complete");
|
log::debug!("METAR update complete");
|
||||||
// Sleep until the earliest observation time is 1 hour old
|
// Sleep until the earliest observation time is 1 hour old
|
||||||
// Bounded by 1 and 3600 seconds
|
// Bounded by 1 and 3600 seconds
|
||||||
let now = chrono::Utc::now().timestamp();
|
let now = chrono::Utc::now().timestamp();
|
||||||
let sleep_time = std::cmp::min(std::cmp::max(1, now - (observation_time + 3600)), 3600);
|
let sleep_time = std::cmp::min(std::cmp::max(1, now - (observation_time + 3600)), 3600);
|
||||||
debug!("Next update in {} seconds", sleep_time);
|
log::debug!("Next update in {} seconds", sleep_time);
|
||||||
sleep(Duration::from_secs(sleep_time as u64)).await;
|
sleep(Duration::from_secs(sleep_time as u64)).await;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -2,15 +2,17 @@ use actix_multipart::Multipart;
|
|||||||
use actix_web::{get, post, delete, web, HttpResponse, ResponseError};
|
use actix_web::{get, post, delete, web, HttpResponse, ResponseError};
|
||||||
use futures_util::StreamExt;
|
use futures_util::StreamExt;
|
||||||
|
|
||||||
use crate::{auth::{JwtAuth, QueryUser, InsertUser}, error_handler::ServiceError, db::{upload_file, get_file, delete_file}};
|
use crate::{
|
||||||
|
auth::{JwtAuth, QueryUser, InsertUser},
|
||||||
|
error_handler::ServiceError,
|
||||||
|
db::{upload_file, get_file, delete_file},
|
||||||
|
};
|
||||||
|
|
||||||
#[get("/favorites")]
|
#[get("/favorites")]
|
||||||
async fn get_favorites(auth: JwtAuth) -> HttpResponse {
|
async fn get_favorites(auth: JwtAuth) -> HttpResponse {
|
||||||
match QueryUser::get_by_email(&auth.user.email) {
|
match QueryUser::get_by_email(&auth.user.email) {
|
||||||
Ok(user) => {
|
Ok(user) => return HttpResponse::Ok().json(user.favorites),
|
||||||
return HttpResponse::Ok().json(user.favorites)
|
Err(err) => return ResponseError::error_response(&err),
|
||||||
},
|
|
||||||
Err(err) => return ResponseError::error_response(&err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -20,18 +22,18 @@ async fn add_favorite(icao: web::Path<String>, auth: JwtAuth) -> HttpResponse {
|
|||||||
Ok(user) => {
|
Ok(user) => {
|
||||||
if user.favorites.contains(&icao) {
|
if user.favorites.contains(&icao) {
|
||||||
// Check if the airport ICAO is already in the user's favorites
|
// Check if the airport ICAO is already in the user's favorites
|
||||||
return HttpResponse::Conflict().finish()
|
return HttpResponse::Conflict().finish();
|
||||||
} else {
|
} else {
|
||||||
// Add the airport ICAO to the user's favorites
|
// Add the airport ICAO to the user's favorites
|
||||||
let mut favorites = user.favorites;
|
let mut favorites = user.favorites;
|
||||||
favorites.push(icao.into_inner());
|
favorites.push(icao.into_inner());
|
||||||
match InsertUser::update_favorites(&user.email, favorites) {
|
match InsertUser::update_favorites(&user.email, favorites) {
|
||||||
Ok(_) => return HttpResponse::Ok().finish(),
|
Ok(_) => return HttpResponse::Ok().finish(),
|
||||||
Err(err) => return ResponseError::error_response(&err)
|
Err(err) => return ResponseError::error_response(&err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Err(err) => return ResponseError::error_response(&err)
|
Err(err) => return ResponseError::error_response(&err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,14 +48,14 @@ async fn delete_favorite(icao: web::Path<String>, auth: JwtAuth) -> HttpResponse
|
|||||||
favorites.retain(|x| x != &icao);
|
favorites.retain(|x| x != &icao);
|
||||||
match InsertUser::update_favorites(&user.email, favorites) {
|
match InsertUser::update_favorites(&user.email, favorites) {
|
||||||
Ok(_) => return HttpResponse::Ok().finish(),
|
Ok(_) => return HttpResponse::Ok().finish(),
|
||||||
Err(err) => return ResponseError::error_response(&err)
|
Err(err) => return ResponseError::error_response(&err),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Remove the airport ICAO from the user's favorites
|
// Remove the airport ICAO from the user's favorites
|
||||||
return HttpResponse::Conflict().finish()
|
return HttpResponse::Conflict().finish();
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Err(err) => return ResponseError::error_response(&err)
|
Err(err) => return ResponseError::error_response(&err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,40 +65,52 @@ async fn set_picture(mut payload: Multipart, auth: JwtAuth) -> HttpResponse {
|
|||||||
let mut bytes = web::BytesMut::new();
|
let mut bytes = web::BytesMut::new();
|
||||||
let mut field = match item {
|
let mut field = match item {
|
||||||
Ok(field) => field,
|
Ok(field) => field,
|
||||||
Err(err) => return ResponseError::error_response(&err)
|
Err(err) => return ResponseError::error_response(&err),
|
||||||
};
|
};
|
||||||
let content_type = field.content_disposition();
|
let content_type = field.content_disposition();
|
||||||
let filename = match content_type.get_filename() {
|
let filename = match content_type.unwrap().get_filename() {
|
||||||
Some(name) => {
|
Some(name) => match name.split(".").last() {
|
||||||
match name.split(".").last() {
|
Some(ext) => match ext {
|
||||||
Some(ext) => {
|
"apng" | "avif" | "gif" | "jpg" | "jpeg" | "jfif" | "pjpeg" | "pjp" | "png" | "svg"
|
||||||
match ext {
|
| "webp" => name,
|
||||||
"apng" | "avif" | "gif" | "jpg" | "jpeg" | "jfif" | "pjpeg" | "pjp" | "png" | "svg" | "webp" => name,
|
_ => {
|
||||||
_ => return ResponseError::error_response(&ServiceError::new(400, "File extension is not supported".to_string()))
|
return ResponseError::error_response(&ServiceError::new(
|
||||||
|
400,
|
||||||
|
"File extension is not supported".to_string(),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None => return ResponseError::error_response(&ServiceError::new(400, "Unknown file extension".to_string()))
|
None => {
|
||||||
|
return ResponseError::error_response(&ServiceError::new(
|
||||||
|
400,
|
||||||
|
"Unknown file extension".to_string(),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None => return ResponseError::error_response(&ServiceError::new(400, "File name is not provided".to_string()))
|
None => {
|
||||||
|
return ResponseError::error_response(&ServiceError::new(
|
||||||
|
400,
|
||||||
|
"File name is not provided".to_string(),
|
||||||
|
))
|
||||||
|
}
|
||||||
};
|
};
|
||||||
let path = format!("users/{}/{}", auth.user.email, filename);
|
let path = format!("users/{}/{}", auth.user.email, filename);
|
||||||
|
|
||||||
while let Some(chunk) = field.next().await {
|
while let Some(chunk) = field.next().await {
|
||||||
let data = match chunk {
|
let data = match chunk {
|
||||||
Ok(data) => data,
|
Ok(data) => data,
|
||||||
Err(err) => return ResponseError::error_response(&err)
|
Err(err) => return ResponseError::error_response(&err),
|
||||||
};
|
};
|
||||||
bytes.extend_from_slice(&data);
|
bytes.extend_from_slice(&data);
|
||||||
}
|
}
|
||||||
match upload_file(&path, &bytes).await {
|
match upload_file(&path, &bytes).await {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
match InsertUser::update_profile_picture(&auth.user.email, Some(&path)) {
|
match InsertUser::update_profile_picture(&auth.user.email, Some(&path)) {
|
||||||
Ok(_) => {},
|
Ok(_) => {}
|
||||||
Err(err) => return ResponseError::error_response(&err)
|
Err(err) => return ResponseError::error_response(&err),
|
||||||
};
|
};
|
||||||
},
|
}
|
||||||
Err(err) => return ResponseError::error_response(&err)
|
Err(err) => return ResponseError::error_response(&err),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
HttpResponse::Ok().finish()
|
HttpResponse::Ok().finish()
|
||||||
@@ -106,12 +120,12 @@ async fn set_picture(mut payload: Multipart, auth: JwtAuth) -> HttpResponse {
|
|||||||
async fn get_picture(auth: JwtAuth) -> HttpResponse {
|
async fn get_picture(auth: JwtAuth) -> HttpResponse {
|
||||||
let user = match QueryUser::get_by_email(&auth.user.email) {
|
let user = match QueryUser::get_by_email(&auth.user.email) {
|
||||||
Ok(user) => user,
|
Ok(user) => user,
|
||||||
Err(err) => return ResponseError::error_response(&err)
|
Err(err) => return ResponseError::error_response(&err),
|
||||||
};
|
};
|
||||||
if let Some(path) = user.profile_picture {
|
if let Some(path) = user.profile_picture {
|
||||||
match get_file(&path).await {
|
match get_file(&path).await {
|
||||||
Ok(bytes) => HttpResponse::Ok().body(bytes),
|
Ok(bytes) => HttpResponse::Ok().body(bytes),
|
||||||
Err(err) => ResponseError::error_response(&err)
|
Err(err) => ResponseError::error_response(&err),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
HttpResponse::NotFound().finish()
|
HttpResponse::NotFound().finish()
|
||||||
@@ -122,17 +136,15 @@ async fn get_picture(auth: JwtAuth) -> HttpResponse {
|
|||||||
async fn delete_picture(auth: JwtAuth) -> HttpResponse {
|
async fn delete_picture(auth: JwtAuth) -> HttpResponse {
|
||||||
let user = match QueryUser::get_by_email(&auth.user.email) {
|
let user = match QueryUser::get_by_email(&auth.user.email) {
|
||||||
Ok(user) => user,
|
Ok(user) => user,
|
||||||
Err(err) => return ResponseError::error_response(&err)
|
Err(err) => return ResponseError::error_response(&err),
|
||||||
};
|
};
|
||||||
if let Some(path) = user.profile_picture {
|
if let Some(path) = user.profile_picture {
|
||||||
match delete_file(&path).await {
|
match delete_file(&path).await {
|
||||||
Ok(_) => {
|
Ok(_) => match InsertUser::update_profile_picture(&auth.user.email, None) {
|
||||||
match InsertUser::update_profile_picture(&auth.user.email, None) {
|
|
||||||
Ok(_) => HttpResponse::Ok().finish(),
|
Ok(_) => HttpResponse::Ok().finish(),
|
||||||
Err(err) => ResponseError::error_response(&err)
|
Err(err) => ResponseError::error_response(&err),
|
||||||
}
|
|
||||||
},
|
},
|
||||||
Err(err) => ResponseError::error_response(&err)
|
Err(err) => ResponseError::error_response(&err),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
HttpResponse::NotFound().finish()
|
HttpResponse::NotFound().finish()
|
||||||
@@ -140,12 +152,13 @@ async fn delete_picture(auth: JwtAuth) -> HttpResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn init_routes(config: &mut web::ServiceConfig) {
|
pub fn init_routes(config: &mut web::ServiceConfig) {
|
||||||
config.service(web::scope("users")
|
config.service(
|
||||||
|
web::scope("users")
|
||||||
.service(get_favorites)
|
.service(get_favorites)
|
||||||
.service(add_favorite)
|
.service(add_favorite)
|
||||||
.service(delete_favorite)
|
.service(delete_favorite)
|
||||||
.service(set_picture)
|
.service(set_picture)
|
||||||
.service(get_picture)
|
.service(get_picture)
|
||||||
.service(delete_picture)
|
.service(delete_picture),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -4,6 +4,8 @@ SHELL := /bin/bash
|
|||||||
GIT_HASH ?= $(shell git log --format="%h" -n 1)
|
GIT_HASH ?= $(shell git log --format="%h" -n 1)
|
||||||
|
|
||||||
include .env
|
include .env
|
||||||
|
-include .env.local
|
||||||
|
export
|
||||||
|
|
||||||
.PHONY: help build start stop lint
|
.PHONY: help build start stop lint
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ interface GetAirportsProps {
|
|||||||
name?: string;
|
name?: string;
|
||||||
order_field?: AirportOrderField;
|
order_field?: AirportOrderField;
|
||||||
order_by?: 'asc' | 'desc';
|
order_by?: 'asc' | 'desc';
|
||||||
|
has_metar?: boolean;
|
||||||
page?: number;
|
page?: number;
|
||||||
limit?: number;
|
limit?: number;
|
||||||
}
|
}
|
||||||
@@ -28,6 +29,7 @@ export async function getAirports({
|
|||||||
name,
|
name,
|
||||||
order_field,
|
order_field,
|
||||||
order_by,
|
order_by,
|
||||||
|
has_metar,
|
||||||
limit = 10,
|
limit = 10,
|
||||||
page = 1
|
page = 1
|
||||||
}: GetAirportsProps): Promise<GetAirportsResponse> {
|
}: GetAirportsProps): Promise<GetAirportsResponse> {
|
||||||
@@ -40,6 +42,7 @@ export async function getAirports({
|
|||||||
name: name ?? undefined,
|
name: name ?? undefined,
|
||||||
order_field: order_field ?? undefined,
|
order_field: order_field ?? undefined,
|
||||||
order_by: order_by ?? undefined,
|
order_by: order_by ?? undefined,
|
||||||
|
has_metar: has_metar ?? undefined,
|
||||||
limit,
|
limit,
|
||||||
page
|
page
|
||||||
});
|
});
|
||||||
|
|||||||
114
ui/src/components/Header/UserMenu.tsx
Normal file
114
ui/src/components/Header/UserMenu.tsx
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
import { User } from "@/api/auth.types";
|
||||||
|
import { setPicture } from "@/api/users";
|
||||||
|
import {
|
||||||
|
Menu,
|
||||||
|
UnstyledButton,
|
||||||
|
Group,
|
||||||
|
Avatar,
|
||||||
|
Card,
|
||||||
|
FileButton,
|
||||||
|
Grid,
|
||||||
|
Button,
|
||||||
|
Text
|
||||||
|
} from "@mantine/core";
|
||||||
|
import Link from "next/link";
|
||||||
|
import { SetterOrUpdater } from "recoil";
|
||||||
|
import './styles.css';
|
||||||
|
|
||||||
|
interface UserMenuProps {
|
||||||
|
user: User;
|
||||||
|
profilePicture: File | undefined;
|
||||||
|
setProfilePicture: SetterOrUpdater<File | undefined>;
|
||||||
|
toggle: (type: string) => void;
|
||||||
|
logout: () => Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function UserMenu({ user, profilePicture, setProfilePicture, logout, toggle }: UserMenuProps) {
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Menu shadow='md' width={200} openDelay={100} closeDelay={400}>
|
||||||
|
<Menu.Target>
|
||||||
|
<UnstyledButton>
|
||||||
|
<Group>
|
||||||
|
<Avatar src={profilePicture ? URL.createObjectURL(profilePicture) : undefined} />
|
||||||
|
<div style={{ flex: 1 }}>
|
||||||
|
<Text size='sm' fw={500}>
|
||||||
|
{user.first_name} {user.last_name}
|
||||||
|
</Text>
|
||||||
|
<Text c='dimmed' size='xs' style={{ textTransform: 'uppercase' }}>
|
||||||
|
{user.role}
|
||||||
|
</Text>
|
||||||
|
</div>
|
||||||
|
</Group>
|
||||||
|
</UnstyledButton>
|
||||||
|
</Menu.Target>
|
||||||
|
<Menu.Dropdown p={0}>
|
||||||
|
<Card>
|
||||||
|
<Card.Section h={140} style={{ backgroundColor: '#4481e3' }} />
|
||||||
|
<FileButton
|
||||||
|
onChange={(payload) => {
|
||||||
|
if (payload) {
|
||||||
|
setPicture(payload).then((response) => {
|
||||||
|
if (response) {
|
||||||
|
setProfilePicture(payload);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
accept='image/png,image/jpeg,image/svg+xml,image/webp,image/gif,image/apng,image/avif'
|
||||||
|
multiple={false}
|
||||||
|
>
|
||||||
|
{(props) => (
|
||||||
|
<Avatar
|
||||||
|
{...props}
|
||||||
|
component='button'
|
||||||
|
size={80}
|
||||||
|
radius={80}
|
||||||
|
mx={'auto'}
|
||||||
|
mt={-30}
|
||||||
|
style={{ cursor: 'pointer' }}
|
||||||
|
bg={profilePicture ? 'transparent' : 'white'}
|
||||||
|
src={profilePicture ? URL.createObjectURL(profilePicture) : undefined}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</FileButton>
|
||||||
|
<Text ta='center' fz='lg' fw={500} mt='sm'>
|
||||||
|
{user.first_name} {user.last_name}
|
||||||
|
</Text>
|
||||||
|
<Text ta='center' fz='sm' c='dimmed' style={{ textTransform: 'uppercase' }}>
|
||||||
|
{user.role}
|
||||||
|
</Text>
|
||||||
|
<Grid mt='xl'>
|
||||||
|
<Grid.Col span={6}>
|
||||||
|
<Link href='/profile'>
|
||||||
|
<Button fullWidth radius='md' size='xs' variant='default'>
|
||||||
|
Profile
|
||||||
|
</Button>
|
||||||
|
</Link>
|
||||||
|
</Grid.Col>
|
||||||
|
<Grid.Col span={6}>
|
||||||
|
<Button
|
||||||
|
fullWidth
|
||||||
|
radius='md'
|
||||||
|
size='xs'
|
||||||
|
variant='default'
|
||||||
|
onClick={logout}
|
||||||
|
>
|
||||||
|
Logout
|
||||||
|
</Button>
|
||||||
|
</Grid.Col>
|
||||||
|
{user.role == 'admin' && (
|
||||||
|
<Grid.Col span={12}>
|
||||||
|
<Link href='/admin'>
|
||||||
|
<Button fullWidth radius='md' size='xs' variant='default'>
|
||||||
|
Administration
|
||||||
|
</Button>
|
||||||
|
</Link>
|
||||||
|
</Grid.Col>
|
||||||
|
)}
|
||||||
|
</Grid>
|
||||||
|
</Card>
|
||||||
|
</Menu.Dropdown>
|
||||||
|
</Menu>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -3,10 +3,8 @@
|
|||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { getAirport, getAirports } from '@/api/airport';
|
import { getAirport, getAirports } from '@/api/airport';
|
||||||
import { Autocomplete, Avatar, Button, Card, FileButton, Grid, Group, Menu, Text, UnstyledButton } from '@mantine/core';
|
import { Autocomplete, Button, Group, UnstyledButton } from '@mantine/core';
|
||||||
import './header.css';
|
|
||||||
import { SetterOrUpdater, useRecoilState } from 'recoil';
|
import { SetterOrUpdater, useRecoilState } from 'recoil';
|
||||||
import { setPicture } from '@/api/users';
|
|
||||||
import { useToggle } from '@mantine/hooks';
|
import { useToggle } from '@mantine/hooks';
|
||||||
import { HeaderModal } from './HeaderModal';
|
import { HeaderModal } from './HeaderModal';
|
||||||
import { coordinatesState } from '@/state/map';
|
import { coordinatesState } from '@/state/map';
|
||||||
@@ -14,6 +12,8 @@ import { User } from '@/api/auth.types';
|
|||||||
import { usePathname, useRouter } from 'next/navigation';
|
import { usePathname, useRouter } from 'next/navigation';
|
||||||
import { FaMoon } from "react-icons/fa6";
|
import { FaMoon } from "react-icons/fa6";
|
||||||
import { FaSun } from "react-icons/fa6";
|
import { FaSun } from "react-icons/fa6";
|
||||||
|
import UserMenu from './UserMenu';
|
||||||
|
import './styles.css';
|
||||||
|
|
||||||
interface HeaderProps {
|
interface HeaderProps {
|
||||||
user: User | undefined;
|
user: User | undefined;
|
||||||
@@ -77,13 +77,28 @@ export default function Header({ user, profilePicture, setProfilePicture, login,
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<UserSection
|
<div className='right'>
|
||||||
|
<UnstyledButton style={{ paddingRight: '1em', margin: 'auto' }}>
|
||||||
|
<FaMoon />
|
||||||
|
{/* <FaSun /> */}
|
||||||
|
</UnstyledButton>
|
||||||
|
{user ? (
|
||||||
|
<UserMenu
|
||||||
user={user}
|
user={user}
|
||||||
profilePicture={profilePicture}
|
profilePicture={profilePicture}
|
||||||
setProfilePicture={setProfilePicture}
|
setProfilePicture={setProfilePicture}
|
||||||
toggle={toggle}
|
toggle={toggle}
|
||||||
logout={logout}
|
logout={logout}
|
||||||
/>
|
/>
|
||||||
|
) : (
|
||||||
|
<Group className='user'>
|
||||||
|
<Button onClick={() => toggle('login')}>Login</Button>
|
||||||
|
<Button variant='outline' onClick={() => toggle('register')}>
|
||||||
|
Sign up
|
||||||
|
</Button>
|
||||||
|
</Group>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</nav>
|
</nav>
|
||||||
<HeaderModal
|
<HeaderModal
|
||||||
type={modalType}
|
type={modalType}
|
||||||
@@ -94,118 +109,3 @@ export default function Header({ user, profilePicture, setProfilePicture, login,
|
|||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
interface UserSectionProps {
|
|
||||||
user: User | undefined;
|
|
||||||
profilePicture: File | undefined;
|
|
||||||
setProfilePicture: SetterOrUpdater<File | undefined>;
|
|
||||||
toggle: (type: string) => void;
|
|
||||||
logout: () => Promise<void>;
|
|
||||||
}
|
|
||||||
|
|
||||||
function UserSection({ user, profilePicture, setProfilePicture, logout, toggle }: UserSectionProps) {
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className='user-section'>
|
|
||||||
<>
|
|
||||||
{/* <UnstyledButton> */}
|
|
||||||
{/* <FaMoon /> */}
|
|
||||||
{/* <FaSun /> */}
|
|
||||||
{/* </UnstyledButton> */}
|
|
||||||
{user ? (
|
|
||||||
<Menu shadow='md' width={200} openDelay={100} closeDelay={400}>
|
|
||||||
<Menu.Target>
|
|
||||||
<UnstyledButton className='user user-button'>
|
|
||||||
<Group>
|
|
||||||
<Avatar src={profilePicture ? URL.createObjectURL(profilePicture) : undefined} />
|
|
||||||
<div style={{ flex: 1 }}>
|
|
||||||
<Text size='sm' fw={500}>
|
|
||||||
{user.first_name} {user.last_name}
|
|
||||||
</Text>
|
|
||||||
<Text c='dimmed' size='xs' style={{ textTransform: 'uppercase' }}>
|
|
||||||
{user.role}
|
|
||||||
</Text>
|
|
||||||
</div>
|
|
||||||
</Group>
|
|
||||||
</UnstyledButton>
|
|
||||||
</Menu.Target>
|
|
||||||
<Menu.Dropdown p={0}>
|
|
||||||
<Card>
|
|
||||||
<Card.Section h={140} style={{ backgroundColor: '#4481e3' }} />
|
|
||||||
<FileButton
|
|
||||||
onChange={(payload) => {
|
|
||||||
if (payload) {
|
|
||||||
setPicture(payload).then((response) => {
|
|
||||||
if (response) {
|
|
||||||
setProfilePicture(payload);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
accept='image/png,image/jpeg,image/svg+xml,image/webp,image/gif,image/apng,image/avif'
|
|
||||||
multiple={false}
|
|
||||||
>
|
|
||||||
{(props) => (
|
|
||||||
<Avatar
|
|
||||||
{...props}
|
|
||||||
component='button'
|
|
||||||
size={80}
|
|
||||||
radius={80}
|
|
||||||
mx={'auto'}
|
|
||||||
mt={-30}
|
|
||||||
style={{ cursor: 'pointer' }}
|
|
||||||
bg={profilePicture ? 'transparent' : 'white'}
|
|
||||||
src={profilePicture ? URL.createObjectURL(profilePicture) : undefined}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
</FileButton>
|
|
||||||
<Text ta='center' fz='lg' fw={500} mt='sm'>
|
|
||||||
{user.first_name} {user.last_name}
|
|
||||||
</Text>
|
|
||||||
<Text ta='center' fz='sm' c='dimmed' style={{ textTransform: 'uppercase' }}>
|
|
||||||
{user.role}
|
|
||||||
</Text>
|
|
||||||
<Grid mt='xl'>
|
|
||||||
<Grid.Col span={6}>
|
|
||||||
<Link href='/profile'>
|
|
||||||
<Button fullWidth radius='md' size='xs' variant='default'>
|
|
||||||
Profile
|
|
||||||
</Button>
|
|
||||||
</Link>
|
|
||||||
</Grid.Col>
|
|
||||||
<Grid.Col span={6}>
|
|
||||||
<Button
|
|
||||||
fullWidth
|
|
||||||
radius='md'
|
|
||||||
size='xs'
|
|
||||||
variant='default'
|
|
||||||
onClick={logout}
|
|
||||||
>
|
|
||||||
Logout
|
|
||||||
</Button>
|
|
||||||
</Grid.Col>
|
|
||||||
{user.role == 'admin' && (
|
|
||||||
<Grid.Col span={12}>
|
|
||||||
<Link href='/admin'>
|
|
||||||
<Button fullWidth radius='md' size='xs' variant='default'>
|
|
||||||
Administration
|
|
||||||
</Button>
|
|
||||||
</Link>
|
|
||||||
</Grid.Col>
|
|
||||||
)}
|
|
||||||
</Grid>
|
|
||||||
</Card>
|
|
||||||
</Menu.Dropdown>
|
|
||||||
</Menu>
|
|
||||||
) : (
|
|
||||||
<Group className='user'>
|
|
||||||
<Button onClick={() => toggle('login')}>Login</Button>
|
|
||||||
<Button variant='outline' onClick={() => toggle('register')}>
|
|
||||||
Sign up
|
|
||||||
</Button>
|
|
||||||
</Group>
|
|
||||||
)}
|
|
||||||
</>
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -6,27 +6,21 @@
|
|||||||
color: white;
|
color: white;
|
||||||
}
|
}
|
||||||
|
|
||||||
.navbar .left {
|
.left {
|
||||||
display: flex;
|
display: flex;
|
||||||
}
|
}
|
||||||
|
|
||||||
.navbar .title {
|
.title {
|
||||||
padding-left: 2em;
|
padding-left: 2em;
|
||||||
padding-right: 2em;
|
padding-right: 2em;
|
||||||
margin: auto;
|
margin: auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
.navbar .left .search {
|
.search {
|
||||||
margin: auto;
|
margin: auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
.navbar .avatar {
|
.right {
|
||||||
padding-right: 2em;
|
|
||||||
margin-top: auto;
|
|
||||||
margin-bottom: auto;
|
|
||||||
}
|
|
||||||
|
|
||||||
.navbar .user-section {
|
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
padding-right: 2em;
|
padding-right: 2em;
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
import { getAirports, updateAirport } from '@/api/airport';
|
import { getAirports, updateAirport } from '@/api/airport';
|
||||||
import { Airport, AirportOrderField } from '@/api/airport.types';
|
import { Airport, AirportOrderField } from '@/api/airport.types';
|
||||||
import { getMetars } from '@/api/metar';
|
import { getMetars } from '@/api/metar';
|
||||||
import { LatLngBounds, icon } from 'leaflet';
|
import { LatLngBounds, PointTuple, icon } from 'leaflet';
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { Marker, TileLayer, Tooltip, useMap, useMapEvents } from 'react-leaflet';
|
import { Marker, TileLayer, Tooltip, useMap, useMapEvents } from 'react-leaflet';
|
||||||
import MetarModal from './MetarModal';
|
import MetarModal from './MetarModal';
|
||||||
@@ -70,8 +70,10 @@ export default function MapTiles() {
|
|||||||
|
|
||||||
function metarIcon(airport: Airport) {
|
function metarIcon(airport: Airport) {
|
||||||
let iconUrl = '/icons/unkn.svg';
|
let iconUrl = '/icons/unkn.svg';
|
||||||
|
let iconSize: PointTuple = [20, 20];
|
||||||
if (!airport.has_metar && airport.latest_metar == undefined) {
|
if (!airport.has_metar && airport.latest_metar == undefined) {
|
||||||
iconUrl = '/icons/nometar.svg';
|
iconUrl = '/icons/nometar.svg';
|
||||||
|
iconSize = [10, 10];
|
||||||
} else if (airport.latest_metar?.flight_category == 'VFR') {
|
} else if (airport.latest_metar?.flight_category == 'VFR') {
|
||||||
iconUrl = '/icons/vfr.svg';
|
iconUrl = '/icons/vfr.svg';
|
||||||
} else if (airport.latest_metar?.flight_category == 'MVFR') {
|
} else if (airport.latest_metar?.flight_category == 'MVFR') {
|
||||||
@@ -81,10 +83,7 @@ export default function MapTiles() {
|
|||||||
} else if (airport.latest_metar?.flight_category == 'LIFR') {
|
} else if (airport.latest_metar?.flight_category == 'LIFR') {
|
||||||
iconUrl = '/icons/lifr.svg';
|
iconUrl = '/icons/lifr.svg';
|
||||||
}
|
}
|
||||||
return icon({
|
return icon({ iconUrl, iconSize })
|
||||||
iconUrl: iconUrl,
|
|
||||||
iconSize: [20, 20]
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user