Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
anders-kiaer committed Sep 26, 2023
1 parent 76c755a commit 85151f0
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
3 changes: 1 addition & 2 deletions backend/src/backend/auth/auth_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ async def _login_route(self, request: Request, redirect_url_after_login: Optiona

cca = _create_msal_confidential_client_app(token_cache=None)
flow_dict = cca.initiate_auth_code_flow(
scopes=all_scopes_list,
redirect_uri=str(request.url_for("_authorized_callback_route")),
scopes=all_scopes_list, redirect_uri="https://anders-kiaer-...-8080.preview.app.github.dev/auth-callback"
)

request.session["flow"] = flow_dict
Expand Down
78 changes: 78 additions & 0 deletions backend/src/fastapi_app/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import datetime
import logging

from fastapi import FastAPI
from fastapi.routing import APIRoute
from starsessions import SessionMiddleware
from starsessions.stores.redis import RedisStore
from uvicorn.middleware.proxy_headers import ProxyHeadersMiddleware

from . import config
from .auth.auth_helper import AuthHelper
from .auth.enforce_logged_in_middleware import EnforceLoggedInMiddleware
from .routers.explore import router as explore_router
from .routers.timeseries.router import router as timeseries_router
from .routers.inplace_volumetrics.router import router as inplace_volumetrics_router
from .routers.general import router as general_router

logging.basicConfig(
level=logging.WARNING, format="%(asctime)s %(levelname)-3s [%(name)s]: %(message)s", datefmt="%H:%M:%S"
)
logging.getLogger("src.services.sumo_access").setLevel(level=logging.DEBUG)


def custom_generate_unique_id(route: APIRoute):
return f"{route.name}"


app = FastAPI(generate_unique_id_function=custom_generate_unique_id, root_path="/api")

import time

from fastapi import FastAPI, Request

app = FastAPI()


@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
request.headers.__dict__["_list"].append(
(
"X-Forwarded-Proto".encode(),
"https".encode(),
)
)
return await call_next(request)

# The tags we add here will determine the name of the frontend api service for our endpoints as well as
# providing some grouping when viewing the openapi documentation.
app.include_router(explore_router, tags=["explore"])
app.include_router(timeseries_router, prefix="/timeseries", tags=["timeseries"])
app.include_router(inplace_volumetrics_router, prefix="/inplace_volumetrics", tags=["inplace_volumetrics"])

authHelper = AuthHelper()
app.include_router(authHelper.router)
app.include_router(general_router)

# Add out custom middleware to enforce that user is logged in
# Also redirects to /login endpoint for some select paths
unprotected_paths = ["/logged_in_user", "/alive", "/openapi.json"]
paths_redirected_to_login = ["/", "/alive_protected"]
app.add_middleware(
ProxyHeadersMiddleware,
trusted_hosts="*"
)
app.add_middleware(
EnforceLoggedInMiddleware,
unprotected_paths=unprotected_paths,
paths_redirected_to_login=paths_redirected_to_login,
)

session_store = RedisStore(config.REDIS_URL)

app.add_middleware(SessionMiddleware, store=session_store)


@app.get("/")
async def root():
return f"Backend is alive at this time: {datetime.datetime.now()}"

0 comments on commit 85151f0

Please sign in to comment.