Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CORS origins synchronize service #914

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion engineapi/deploy/deploy.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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}"
9 changes: 9 additions & 0 deletions engineapi/deploy/engine-origins-sync.service
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions engineapi/deploy/engine-origins-sync.timer
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Unit]
Description=Synchronize CORS origins for server

[Timer]
OnBootSec=120s
OnUnitActiveSec=5m

[Install]
WantedBy=timers.target
15 changes: 10 additions & 5 deletions engineapi/engineapi/routes/configs.py
Original file line number Diff line number Diff line change
@@ -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,
)
Expand Down Expand Up @@ -46,6 +42,7 @@
"/configs/docs": "GET",
"/configs/openapi.json": "GET",
"/configs/is_origin": "GET",
"/configs/origins/sync": "GET",
}
)

Expand Down Expand Up @@ -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,
Expand Down
Loading