-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.py
86 lines (70 loc) · 2.51 KB
/
main.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from fastapi import APIRouter, FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address
from slowapi.errors import RateLimitExceeded
from slowapi.middleware import SlowAPIMiddleware
from app.api.v1.router import router as api_v1_router
tags_metadata = [
{
"name": "API v1",
"description": "Version 1 endpoints of the MangaReader API. Access manga content, search for specific titles, and retrieve manga information.",
},
]
description = """
A Python-based web scraping tool built with FastAPI that provides easy access to manga content from the [mangareader.to](https://mangareader.to) website.
This API allows users to retrieve up-to-date information.
Enabling developers to create their own manga-related applications and services.
"""
root_router = APIRouter()
app = FastAPI(
openapi_tags=tags_metadata,
title="MangaReader API",
description=description,
summary="A Python-based web scraping API built with FastAPI that provides easy access to manga contents.",
version="1.0.0",
# disable redoc and swagger
redoc_url=None,
docs_url=None,
)
# cors
# https://fastapi.tiangolo.com/tutorial/cors/#use-corsmiddleware
origins = [
# specify allowed origins
"*" # if specified remove this lint
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# rate limiter
# https://loadforge.com/guides/implementing-rate-limits-in-fastapi-a-step-by-step-guide
limiter = Limiter(key_func=get_remote_address, default_limits=["50/hour"])
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
app.add_middleware(SlowAPIMiddleware)
# https://stackoverflow.com/a/61644963/20547892
app.mount(
"/static",
StaticFiles(directory="static"),
name="static",
)
# https://fastapi.tiangolo.com/advanced/templates/
TEMPLATES = Jinja2Templates(directory="templates")
# homepage route
@root_router.get("/", response_class=HTMLResponse, include_in_schema=False)
async def root(request: Request):
return TEMPLATES.TemplateResponse(
"index.html",
context={
"request": request,
},
)
app.include_router(root_router)
app.include_router(api_v1_router, prefix="/api/v1", tags=["API v1"])