From d29c40e907d66f775c6553916e729da3f3f4c64a Mon Sep 17 00:00:00 2001 From: kompotkot Date: Thu, 31 Aug 2023 10:46:48 +0000 Subject: [PATCH] CORS origins synchronize service --- engineapi/deploy/deploy.bash | 13 ++++++++++++- engineapi/deploy/engine-origins-sync.service | 9 +++++++++ engineapi/deploy/engine-origins-sync.timer | 9 +++++++++ engineapi/engineapi/routes/configs.py | 15 ++++++++++----- 4 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 engineapi/deploy/engine-origins-sync.service create mode 100644 engineapi/deploy/engine-origins-sync.timer diff --git a/engineapi/deploy/deploy.bash b/engineapi/deploy/deploy.bash index 0f0d6a062..429c9e59b 100755 --- a/engineapi/deploy/deploy.bash +++ b/engineapi/deploy/deploy.bash @@ -23,8 +23,10 @@ SCRIPT_DIR="$(realpath $(dirname $0))" SECRETS_DIR="${SECRETS_DIR:-/home/ubuntu/engineapi-secrets}" PARAMETERS_ENV_PATH="${SECRETS_DIR}/app.env" -# API server service file +# Engine API service files ENGINE_SERVICE_FILE="engine.service" +ENGINE_ORIGINS_SYNC_SERVICE_FILE="engine-origins-sync.service" +ENGINE_ORIGINS_SYNC_TIMER_FILE="engine-origins-sync.timer" set -eu @@ -70,3 +72,12 @@ chmod 644 "${SCRIPT_DIR}/${ENGINE_SERVICE_FILE}" cp "${SCRIPT_DIR}/${ENGINE_SERVICE_FILE}" "/home/ubuntu/.config/systemd/user/${ENGINE_SERVICE_FILE}" XDG_RUNTIME_DIR="/run/user/1000" systemctl --user daemon-reload XDG_RUNTIME_DIR="/run/user/1000" systemctl --user restart --no-block "${ENGINE_SERVICE_FILE}" + +echo +echo +echo -e "${PREFIX_INFO} Replacing existing CORS origins synchronize service and timer with: ${ENGINE_ORIGINS_SYNC_SERVICE_FILE}, ${ENGINE_ORIGINS_SYNC_TIMER_FILE}" +chmod 644 "${SCRIPT_DIR}/${ENGINE_ORIGINS_SYNC_SERVICE_FILE}" "${SCRIPT_DIR}/${ENGINE_ORIGINS_SYNC_TIMER_FILE}" +cp "${SCRIPT_DIR}/${ENGINE_ORIGINS_SYNC_SERVICE_FILE}" "/home/ubuntu/.config/systemd/user/${ENGINE_ORIGINS_SYNC_SERVICE_FILE}" +cp "${SCRIPT_DIR}/${ENGINE_ORIGINS_SYNC_TIMER_FILE}" "/home/ubuntu/.config/systemd/user/${ENGINE_ORIGINS_SYNC_TIMER_FILE}" +XDG_RUNTIME_DIR="/run/user/1000" systemctl --user daemon-reload +XDG_RUNTIME_DIR="/run/user/1000" systemctl --user restart --no-block "${ENGINE_ORIGINS_SYNC_TIMER_FILE}" diff --git a/engineapi/deploy/engine-origins-sync.service b/engineapi/deploy/engine-origins-sync.service new file mode 100644 index 000000000..bffe7f65f --- /dev/null +++ b/engineapi/deploy/engine-origins-sync.service @@ -0,0 +1,9 @@ +[Unit] +Description=Synchronize CORS origins for server +After=network.target + +[Service] +Type=oneshot +ExecStart=/usr/bin/curl http://127.0.0.1:7191/configs/origins/sync +CPUWeight=50 +SyslogIdentifier=engine-origins-sync diff --git a/engineapi/deploy/engine-origins-sync.timer b/engineapi/deploy/engine-origins-sync.timer new file mode 100644 index 000000000..03bd03db9 --- /dev/null +++ b/engineapi/deploy/engine-origins-sync.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Synchronize CORS origins for server + +[Timer] +OnBootSec=120s +OnUnitActiveSec=5m + +[Install] +WantedBy=timers.target diff --git a/engineapi/engineapi/routes/configs.py b/engineapi/engineapi/routes/configs.py index 1c9c6c61d..39d6df19f 100644 --- a/engineapi/engineapi/routes/configs.py +++ b/engineapi/engineapi/routes/configs.py @@ -1,14 +1,10 @@ import logging -from typing import Any, Dict, List, Set +from typing import Any, Dict -from bugout.data import BugoutResource, BugoutResources from fastapi import ( BackgroundTasks, - Body, - Depends, FastAPI, Form, - HTTPException, Query, Request, ) @@ -46,6 +42,7 @@ "/configs/docs": "GET", "/configs/openapi.json": "GET", "/configs/is_origin": "GET", + "/configs/origins/sync": "GET", } ) @@ -126,6 +123,14 @@ async def get_cors_origins( return cors_origins +@app.get("/origins/sync", response_model=int) +async def synchronize_cors_origins() -> int: + cors_origins = fetch_and_set_cors_origins_cache() + cors_origins_len = len(cors_origins.origins_set) + + return cors_origins_len + + @app.post("/origin", response_model=data.CORSOrigins) async def add_cors_origin( request: Request,