diff --git a/Makefile b/Makefile index 2ab9c52..b5eb1f4 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ #!make SHELL := /bin/bash -export API_VERSION = $(shell awk -F ' = ' '$$1 ~ /package.version/ { gsub(/[\"]/, "", $$2); printf("%s",$$2) }' api/Cargo.toml) -export UI_VERSION := $(shell awk -F'"' '/"version"/ { print $$4 }' ui/package.json) +export API_VERSION = $(shell sed -n 's/^version *= *"\([^"]*\)".*/\1/p' api/Cargo.toml) +export UI_VERSION=$(shell sed -n 's/.*"version": *"\([^"]*\)".*/\1/p' ui/package.json) include .env -include .env.local @@ -128,3 +128,6 @@ push: ## Build and push a specific docker image (`make push f=httpd`) cert: domain=$(if $(d),$(d),${NGINX_HOST}) cert: ## Generate a cert for the given domain ./scripts/generate_cert.sh ${domain} + +tag: ## Tag the commits based on versions + @./scripts/tag.sh diff --git a/api/Cargo.lock b/api/Cargo.lock index d8a78b9..6126900 100644 --- a/api/Cargo.lock +++ b/api/Cargo.lock @@ -366,7 +366,7 @@ dependencies = [ [[package]] name = "api" -version = "0.1.1" +version = "0.1.2" dependencies = [ "actix-cors", "actix-multipart", diff --git a/api/Cargo.toml b/api/Cargo.toml index d998357..bfe3cff 100644 --- a/api/Cargo.toml +++ b/api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "api" -version = "0.1.1" +version = "0.1.2" edition = "2021" authors = ["Ben Sherriff "] repository = "https://github.com/bensherriff/aviation-weather" diff --git a/api/src/auth/model.rs b/api/src/account/auth.rs similarity index 100% rename from api/src/auth/model.rs rename to api/src/account/auth.rs diff --git a/api/src/auth/mod.rs b/api/src/account/mod.rs similarity index 98% rename from api/src/auth/mod.rs rename to api/src/account/mod.rs index 265bd3e..bfff22c 100644 --- a/api/src/auth/mod.rs +++ b/api/src/account/mod.rs @@ -6,11 +6,11 @@ use rand::distr::Alphanumeric; use rand::prelude::*; use rand_chacha::ChaCha20Rng; -mod model; +mod auth; mod routes; mod session; -pub use model::*; +pub use auth::*; pub use session::*; pub use routes::init_routes; diff --git a/api/src/auth/routes.rs b/api/src/account/routes.rs similarity index 98% rename from api/src/auth/routes.rs rename to api/src/account/routes.rs index c9df942..9d6ba7b 100644 --- a/api/src/auth/routes.rs +++ b/api/src/account/routes.rs @@ -1,11 +1,11 @@ use actix_web::{post, web, HttpResponse, ResponseError, HttpRequest, put, get}; use crate::{ - auth::{verify_hash, Session, SESSION_COOKIE_NAME}, + account::{verify_hash, Session, SESSION_COOKIE_NAME}, error::Error, users::{LoginRequest, RegisterRequest, User, UserResponse}, }; -use crate::auth::Auth; +use crate::account::Auth; use crate::users::UpdateUser; #[post("/register")] diff --git a/api/src/auth/session.rs b/api/src/account/session.rs similarity index 100% rename from api/src/auth/session.rs rename to api/src/account/session.rs diff --git a/api/src/airports/routes.rs b/api/src/airports/routes.rs index dd010d7..b4594c5 100644 --- a/api/src/airports/routes.rs +++ b/api/src/airports/routes.rs @@ -3,7 +3,7 @@ use futures_util::stream::StreamExt as _; use crate::{ airports::Airport, db::Paged, - auth::{Auth, verify_role}, + account::{Auth, verify_role}, AppState, }; use actix_multipart::Multipart; diff --git a/api/src/main.rs b/api/src/main.rs index 24d6481..e981d26 100644 --- a/api/src/main.rs +++ b/api/src/main.rs @@ -4,15 +4,16 @@ use actix_cors::Cors; use actix_web::{App, HttpServer, middleware::Logger, web}; use dotenv::from_filename; use reqwest::Certificate; -use crate::auth::hash; +use crate::account::hash; use crate::users::{User, ADMIN_ROLE}; +mod account; mod airports; -mod auth; mod db; mod error; mod metars; mod scheduler; +mod system; mod users; #[derive(Debug, Clone)] @@ -71,7 +72,7 @@ async fn main() -> Result<(), Box> { let state = AppState { client }; let host = "0.0.0.0"; - let port = env::var("API_PORT").unwrap_or("5000".to_string()); + let port = "5000"; let server = match HttpServer::new(move || { let cors = Cors::default() @@ -88,8 +89,9 @@ async fn main() -> Result<(), Box> { web::scope("api") .configure(airports::init_routes) .configure(metars::init_routes) - .configure(auth::init_routes) - .configure(users::init_routes), + .configure(account::init_routes) + .configure(users::init_routes) + .configure(system::init_routes), ) }) .bind(format!("{}:{}", host, port)) diff --git a/api/src/system/mod.rs b/api/src/system/mod.rs new file mode 100644 index 0000000..b8e8565 --- /dev/null +++ b/api/src/system/mod.rs @@ -0,0 +1,31 @@ +use std::env; +use actix_web::{get, web, HttpResponse}; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize)] +pub struct SystemInfo { + version: String, + healthy: bool, +} + +#[get("/info")] +async fn info() -> HttpResponse { + let mut healthy = true; + let version = match env::var("API_VERSION") { + Ok(v) => v, + Err(_) => { + healthy = false; + String::from("unknown") + } + }; + + dbg!(&version); + + let info = SystemInfo { version, healthy }; + + HttpResponse::Ok().json(info) +} + +pub fn init_routes(config: &mut web::ServiceConfig) { + config.service(web::scope("/system").service(info)); +} diff --git a/api/src/users/model.rs b/api/src/users/model.rs index d6f7752..213bd9a 100644 --- a/api/src/users/model.rs +++ b/api/src/users/model.rs @@ -1,7 +1,7 @@ use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use sqlx::{Postgres, QueryBuilder}; -use crate::{auth::hash, error::ApiResult}; +use crate::{account::hash, error::ApiResult}; use crate::db; pub const ADMIN_ROLE: &str = "ADMIN"; diff --git a/bruno/System/Info.bru b/bruno/System/Info.bru new file mode 100644 index 0000000..7e1d768 --- /dev/null +++ b/bruno/System/Info.bru @@ -0,0 +1,11 @@ +meta { + name: Info + type: http + seq: 1 +} + +get { + url: {{API_URL}}/system/info + body: none + auth: inherit +} diff --git a/bruno/System/folder.bru b/bruno/System/folder.bru new file mode 100644 index 0000000..a3dc1c3 --- /dev/null +++ b/bruno/System/folder.bru @@ -0,0 +1,3 @@ +meta { + name: System +} diff --git a/docker-compose.yml b/docker-compose.yml index 43a711f..96355bc 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -87,7 +87,7 @@ services: <<: *default_restart api: - image: gitea.bensherriff.com/bsherriff/aviation-api:latest + image: gitea.bensherriff.com/bsherriff/aviation-api:0.1.2 container_name: aviation-api build: context: ./api diff --git a/nginx/templates/nossl.conf.template b/nginx/templates/nossl.conf.template index 327b849..7f55269 100644 --- a/nginx/templates/nossl.conf.template +++ b/nginx/templates/nossl.conf.template @@ -5,7 +5,7 @@ server { server_name ${NGINX_HOST}; location /api/ { - proxy_pass http://${NGINX_INTERNAL_HOST}:${API_PORT}/api/; + proxy_pass http://${NGINX_INTERNAL_HOST}:5000/api/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; @@ -13,7 +13,7 @@ server { } location /minio/ { - proxy_pass http://${NGINX_INTERNAL_HOST}:${MINIO_PORT_INTERNAL}/; + proxy_pass http://${NGINX_INTERNAL_HOST}:9001/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; @@ -22,7 +22,7 @@ server { # Reverse proxy for the UI and default catch-all location / { - proxy_pass http://${NGINX_INTERNAL_HOST}:${UI_PORT}/; + proxy_pass http://${NGINX_INTERNAL_HOST}:3000/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; diff --git a/nginx/templates/ssl.conf.template b/nginx/templates/ssl.conf.template index fa9b31f..635d0e7 100644 --- a/nginx/templates/ssl.conf.template +++ b/nginx/templates/ssl.conf.template @@ -25,7 +25,7 @@ server { # ssl_prefer_server_ciphers on; location /api/ { - proxy_pass http://${NGINX_INTERNAL_HOST}:${API_PORT}/api/; + proxy_pass http://${NGINX_INTERNAL_HOST}:5000/api/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; @@ -33,7 +33,7 @@ server { } location /minio/ { - proxy_pass http://${NGINX_INTERNAL_HOST}:${MINIO_PORT_INTERNAL}/; + proxy_pass http://${NGINX_INTERNAL_HOST}:9001/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; @@ -42,7 +42,7 @@ server { # Reverse proxy for the UI and default catch-all location / { - proxy_pass http://${NGINX_INTERNAL_HOST}:${UI_PORT}/; + proxy_pass http://${NGINX_INTERNAL_HOST}:3000/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; diff --git a/scripts/tag.sh b/scripts/tag.sh new file mode 100755 index 0000000..8459c67 --- /dev/null +++ b/scripts/tag.sh @@ -0,0 +1,71 @@ +#!/bin/bash + +force=0 +push=0 + +API_VERSION=$(sed -n 's/^version *= *"\([^"]*\)".*/\1/p' "$(pwd)"/api/Cargo.toml) +UI_VERSION=$(sed -n 's/.*"version": *"\([^"]*\)".*/\1/p' "$(pwd)"/ui/package.json) + +# Parse arguments to detect the force (-f/--force) and push (-p/--push) flags +for arg in "$@"; do + case $arg in + -f|--force) + force=1 + shift + ;; + -p|--push) + push=1 + shift + ;; + esac +done + +changed_files=$(git diff --name-only "$(git rev-parse HEAD^)") + +# Processing UI changes +if echo "$changed_files" | grep -q "^ui/"; then + ui_tag="ui-${UI_VERSION}" + if git rev-parse "$ui_tag" >/dev/null 2>&1; then + if [ $force -eq 1 ]; then + echo "Force updating tag ${ui_tag} for UI to commit $(git rev-parse HEAD)" + git tag -fa "${ui_tag}" -m "UI changes in commit $(git rev-parse HEAD)" + if [ $push -eq 1 ]; then + echo "Force pushing tag ${ui_tag} to remote" + git push -f origin "${ui_tag}" + fi + else + echo "Tag ${ui_tag} already exists, skipping UI tagging" + fi + else + echo "Tagging UI with ${ui_tag}" + git tag -a "${ui_tag}" -m "UI changes in commit $(git rev-parse HEAD)" + if [ $push -eq 1 ]; then + echo "Pushing tag ${ui_tag} to remote" + git push origin "${ui_tag}" + fi + fi +fi + +# Processing API changes +if echo "$changed_files" | grep -q "^api/"; then + api_tag="api-${API_VERSION}" + if git rev-parse "$api_tag" >/dev/null 2>&1; then + if [ $force -eq 1 ]; then + echo "Force updating tag ${api_tag} for API to commit $(git rev-parse HEAD)" + git tag -fa "${api_tag}" -m "API changes in commit $(git rev-parse HEAD)" + if [ $push -eq 1 ]; then + echo "Force pushing tag ${api_tag} to remote" + git push -f origin "${api_tag}" + fi + else + echo "Tag ${api_tag} already exists, skipping API tagging" + fi + else + echo "Tagging API with ${api_tag}" + git tag -a "${api_tag}" -m "API changes in commit $(git rev-parse HEAD)" + if [ $push -eq 1 ]; then + echo "Pushing tag ${api_tag} to remote" + git push origin "${api_tag}" + fi + fi +fi