aboutsummaryrefslogtreecommitdiff
path: root/jb/main.py
blob: fa591677f042ac8a9757b9ab8dae09d053fc74fb (plain)
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
from multiprocessing import Process
from typing import Any, Dict

from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from starlette.middleware.cors import CORSMiddleware
from starlette.middleware.trustedhost import TrustedHostMiddleware

from jb.views.common import common_router
from jb.settings import BASE_HTML
from jb.config import settings

app = FastAPI(
    servers=[
        {
            "url": "https://jamesbillings67.com/",
            "description": "Production environment",
        },
    ],
    title="jb",
    version="1.0.0",
)

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

app.add_middleware(TrustedHostMiddleware, allowed_hosts=["*"])
app.include_router(router=common_router)


@app.get("/robots.txt")
@app.get("/sitemap.xml")
@app.get("/favicon.ico")
def return_nothing() -> Dict[str, Any]:
    return {}


@app.get("/{full_path:path}")
def serve_react_app(full_path: str):
    # This serves index.html for any unmatched route
    # React Router will then handle the client-side routing
    return HTMLResponse(BASE_HTML)


def schedule_tasks():
    from jb.flow.events import process_mturk_events_task
    from jb.flow.tasks import refill_hits_task

    Process(target=process_mturk_events_task).start()
    # Process(target=handle_pending_msgs_task).start()
    Process(target=refill_hits_task).start()


if not settings.debug:
    schedule_tasks()