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

refactor: add cors setting via environent value #27

Merged
merged 2 commits into from
Oct 9, 2024
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
10 changes: 7 additions & 3 deletions backend/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,20 @@ export ENV=local
export DB_HOST=localhost
export DB_NAME=main
export DB_CONNECTOR=mysql+pymysql

export ALLOW_ORIGINS=http://127.0.0.1:8000# pass the allowed origins -> http://localhost:3000,http://localhost:3001
export ALLOW_CREDENTIALS=true
export ALLOW_METHODS=*# pass the allowed methods -> GET,POST,PUT,DELETE
export ALLOW_HEADERS=*# pass the allowed headers -> Content-Type,Authorization
export LOG_LEVEL=DEBUG
run-user: ## Start the User API
@export POWERTOOLS_METRICS_NAMESPACE=user-api && \
export POWERTOOLS_SERVICE_NAME=user-api && \
poetry run uvicorn oqtopus_cloud.user.lambda_function:app --host 0.0.0.0 --port 8080 --reload
poetry run uvicorn oqtopus_cloud.user.lambda_function:app --host 0.0.0.0 --port 8080 --reload --log-level debug

run-provider: ## Start the Provider API
@export POWERTOOLS_METRICS_NAMESPACE=provider-api && \
export POWERTOOLS_SERVICE_NAME=provider-api && \
poetry run uvicorn oqtopus_cloud.provider.lambda_function:app --host 0.0.0.0 --port 8888 --reload
poetry run uvicorn oqtopus_cloud.provider.lambda_function:app --host 0.0.0.0 --port 8888 --reload --log-level debug

up: ## Start the DB
@docker compose up
Expand Down
5 changes: 4 additions & 1 deletion backend/oqtopus_cloud/provider/conf.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import os

from aws_lambda_powertools import Logger, Metrics, Tracer

logger: Logger = Logger()
tracer: Tracer = Tracer()
metrics: Metrics = Metrics()

logger.setLevel("INFO")
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
logger.setLevel(LOG_LEVEL)
17 changes: 14 additions & 3 deletions backend/oqtopus_cloud/provider/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
This module is the entry point of the development application. It creates the FastAPI
"""

import os

import setuptools._distutils.util
from fastapi import FastAPI
from mangum import (
Mangum,
Expand All @@ -26,12 +29,20 @@
app: FastAPI = FastAPI()

app.add_middleware(CustomMiddleware)

ALLOW_ORIGINS = os.getenv("ALLOW_ORIGINS", "").split(",")
ALLOW_CREDENTIALS = setuptools._distutils.util.strtobool(
os.getenv("ALLOW_CREDENTIALS", "false")
)
ALLOW_METHODS = os.getenv("ALLOW_METHODS", "").split(",")
ALLOW_HEADERS = os.getenv("ALLOW_HEADERS", "").split(",")

app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_origins=ALLOW_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
allow_methods=ALLOW_METHODS,
allow_headers=ALLOW_HEADERS,
)

app.include_router(
Expand Down
5 changes: 4 additions & 1 deletion backend/oqtopus_cloud/user/conf.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import os

from aws_lambda_powertools import Logger, Metrics, Tracer

logger: Logger = Logger()
tracer: Tracer = Tracer()
metrics: Metrics = Metrics()

logger.setLevel("INFO")
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
logger.setLevel(LOG_LEVEL)
17 changes: 14 additions & 3 deletions backend/oqtopus_cloud/user/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
This module is the entry point of the development application. It creates the FastAPI
"""

import os

import setuptools._distutils.util
from fastapi import FastAPI
from mangum import (
Mangum,
Expand All @@ -24,12 +27,20 @@
app: FastAPI = FastAPI()

app.add_middleware(CustomMiddleware)

ALLOW_ORIGINS = os.getenv("ALLOW_ORIGINS", "").split(",")
ALLOW_CREDENTIALS = setuptools._distutils.util.strtobool(
os.getenv("ALLOW_CREDENTIALS", "false")
)
ALLOW_METHODS = os.getenv("ALLOW_METHODS", "").split(",")
ALLOW_HEADERS = os.getenv("ALLOW_HEADERS", "").split(",")

app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_origins=ALLOW_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
allow_methods=ALLOW_METHODS,
allow_headers=ALLOW_HEADERS,
)

app.include_router(
Expand Down
33 changes: 32 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ cryptography = "^42.0.5"
pydantic = "^2.6.4"
fastapi = "^0.110.0"
aws-xray-sdk = "^2.13.0"
setuptools = "^75.1.0"
types-setuptools = "^75.1.0.20240917"


[tool.poetry.group.dev.dependencies]
Expand Down
7 changes: 6 additions & 1 deletion terraform/service/modules/api-server/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,19 @@ data "aws_caller_identity" "current" {}
resource "aws_lambda_function" "this" {
architectures = ["x86_64"]

environment {
environment { # TODO :Add module input variables
variables = {
DB_HOST = var.db_proxy_endpoint
DB_NAME = "main"
DB_CONNECTOR = "mysql+pymysql"
SECRET_NAME = var.db_secret_arn
POWERTOOLS_METRICS_NAMESPACE = "SampleAppc"
POWERTOOLS_SERVICE_NAME = "SampleAppc"
ALLOW_ORIGINS = "*"
ALLOW_CREDENTIALS = true
ALLOW_METHODS = "*"
ALLOW_HEADERS = "*"
LOG_LEVEL = "DEBUG"
}
}

Expand Down
Loading