diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml new file mode 100644 index 0000000..0a2e6ba --- /dev/null +++ b/.github/workflows/pytest.yml @@ -0,0 +1,38 @@ +# This workflow will install Python dependencies, run tests and lint with a single version of Python +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python + +name: Run automated Tests + +on: + push: + workflow_dispatch: + pull_request: + +permissions: + contents: read + +jobs: + test: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Set up Python 3.11 + uses: actions/setup-python@v3 + with: + python-version: "3.11" + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install flake8 pytest + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + - name: Lint with flake8 + run: | + # stop the build if there are Python syntax errors or undefined names + flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide + flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics + - name: Test with pytest + run: | + pytest diff --git a/main.py b/main.py index 94fe9f1..648b2e7 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,3 @@ -# pylint: disable=invalid-name import uvicorn from fastapi import FastAPI from fastapi.responses import RedirectResponse, JSONResponse @@ -11,6 +10,28 @@ load_dotenv() port = os.environ["PORT"] +# Set up logs export to Azure Application Insights +logger_provider = LoggerProvider() +set_logger_provider(logger_provider) +exporter = AzureMonitorLogExporter( + connection_string=os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"] +) +logger_provider.add_log_record_processor(BatchLogRecordProcessor(exporter)) + +# Attach LoggingHandler to root logger +handler = LoggingHandler() +logging.getLogger().addHandler(handler) +logging.getLogger().setLevel(logging.NOTSET) +logger = logging.getLogger(__name__) + +# Silence noisy loggers +logging.getLogger("requests").setLevel(logging.WARNING) +logging.getLogger("urllib3").setLevel(logging.WARNING) +logging.getLogger("azure").setLevel(logging.WARNING) +logging.getLogger("requests_oauthlib").setLevel(logging.WARNING) +logging.getLogger("asyncio").setLevel(logging.WARNING) +logging.getLogger("opentelemetry").setLevel(logging.ERROR) + # initialize FastAPI app = FastAPI( title="kobo-connect", @@ -25,6 +46,16 @@ }, ) +# initialize CosmosDB +client_ = cosmos_client.CosmosClient( + os.getenv("COSMOS_URL"), + {"masterKey": os.getenv("COSMOS_KEY")}, + user_agent="kobo-connect", + user_agent_overwrite=True, +) +cosmos_db = client_.get_database_client("kobo-connect") +cosmos_container_client = cosmos_db.get_container_client("kobo-submissions") + @app.get("/", include_in_schema=False) async def docs_redirect(): diff --git a/routes/__init__.py b/routes/__init__.py deleted file mode 100644 index e69de29..0000000