-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
69 lines (53 loc) · 1.76 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import subprocess
import os
from fastapi import FastAPI, Request
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse
from contextlib import asynccontextmanager
from starlette.middleware.sessions import SessionMiddleware
from redis import asyncio as aioredis
from dotenv import load_dotenv
from db import engine
import models
from routes import auth,linkchecker
# load environment variables
load_dotenv()
# create database tables
models.Base.metadata.create_all(bind=engine)
# Environment variables
SESSION_SECRET = os.environ.get("SESSION_SECRET")
# fastapi instance
app = FastAPI()
app.add_middleware(SessionMiddleware, secret_key=SESSION_SECRET)
# mounting static files and templates
app.mount("/static", StaticFiles(directory="static"), name="static")
app.mount("/templates", StaticFiles(directory="templates"), name="templates")
# set up CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.middleware("http")
async def session_middleware(request: Request, call_next):
response = await call_next(request)
session = request.cookies.get('session')
if session:
response.set_cookie(
key=SESSION_SECRET, value=request.cookies.get('session'), httponly=True)
return response
@app.post("/update-server")
# update toolforge
def webhook():
subprocess.check_output(["git", "pull", "origin", "main"])
return "Updated Toolforge project successfully", 200
#routers
app.include_router(auth.router)
app.include_router(linkchecker.router)
@app.get("/", response_class=HTMLResponse)
# Render the index page
async def index():
return open("templates/index.html").read()