-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
100 lines (76 loc) · 2.88 KB
/
main.py
File metadata and controls
100 lines (76 loc) · 2.88 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import uvicorn
from ipaddress import ip_address
from typing import Callable
from pathlib import Path
import redis.asyncio as redis
from fastapi import FastAPI, Request, status, Depends, HTTPException
from fastapi_limiter import FastAPILimiter
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from sqlalchemy import text
from sqlalchemy.orm import Session
# from src.routes import contacts, auth, users
from src.routes import tags, images
from src.conf.config import settings
from src.database.db import get_db
from src.routes import auth, user_option, images, comments
from src.routes.transform_image_routes import router as cl_image_router
import src.conf.cloudinary_config
app = FastAPI()
app.include_router(auth.router, prefix="/api")
# app.include_router(user_option.router, prefix="/api")
app.include_router(images.router, prefix="/api")
# app.include_router(cl_image_router, prefix="/images", tags=["images"])
app.include_router(comments.router, prefix="/api")
app.include_router(tags.router, prefix="/api")
banned_ips = [
# ip_address("192.168.1.1"),
# ip_address("192.168.1.2"),
]
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# app.include_router(auth.router)
# app.include_router(users.router)
# app.include_router(contacts.router)
# app.include_router(tags.router)
# app.include_router(images.router)
@app.on_event("startup")
async def startup():
"""
The startup function is called when the application starts up.
It's a good place to initialize things that are needed by your app, like database connections or caches.
:return: A future object, which is a coroutine
:doc-author: Trelent
"""
r = await redis.Redis(host=settings.redis_host, port=settings.redis_port, db=0, password=settings.redis_password)
await FastAPILimiter.init(r)
@app.get("/healthchecker")
def healthchecker(db: Session = Depends(get_db)):
try:
# Make request
result = db.execute(text("SELECT 1")).fetchone()
if result is None:
raise HTTPException(status_code=500, detail="Database is not configured correctly")
return {"message": "Welcome to FastAPI!"}
except Exception as e:
print(e)
raise HTTPException(status_code=500, detail="Error connecting to the database")
@app.get("/")
def main_root():
"""
The main_root function is the root of the API. It returns a JSON object with
a message that says "Contacts application". This function is called when you
visit http://localhost:8000/. The main_root function does not take any
arguments.
:return: A dictionary
:doc-author: Trelent
"""
return {"message": "Pythongram started"}
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)