-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
181be93
commit 7acaa09
Showing
20 changed files
with
190 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,17 @@ | ||
import os | ||
import logging | ||
import uvicorn | ||
|
||
from fastapi import FastAPI, Request, status | ||
from fastapi.middleware.cors import CORSMiddleware | ||
from fastapi.responses import PlainTextResponse, FileResponse | ||
from fastapi.staticfiles import StaticFiles | ||
from src.app import app | ||
from src.config import settings | ||
|
||
from slowapi import _rate_limit_exceeded_handler | ||
from slowapi.errors import RateLimitExceeded | ||
from slowapi.middleware import SlowAPIMiddleware | ||
|
||
from src.limiter import limiter, get_real_ipaddr | ||
from src.chatgpt_router import router as chatgpt_router | ||
from src.divination_router import router as divination_router | ||
from src.user_router import router as user_router | ||
|
||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
app = FastAPI(title="Chatgpt Divination API") | ||
app.state.limiter = limiter | ||
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler) | ||
app.add_middleware(SlowAPIMiddleware) | ||
|
||
app.add_middleware( | ||
CORSMiddleware, | ||
allow_origins=[ | ||
"http://localhost:5173", | ||
"http://localhost", | ||
"http://127.0.0.1" | ||
], | ||
allow_credentials=True, | ||
allow_methods=["*"], | ||
allow_headers=["*"], | ||
logging.basicConfig( | ||
format="%(asctime)s: %(levelname)s: %(name)s: %(message)s", | ||
level=logging.INFO | ||
) | ||
_logger = logging.getLogger(__name__) | ||
|
||
app.include_router(chatgpt_router) | ||
app.include_router(divination_router) | ||
app.include_router(user_router) | ||
|
||
if os.path.exists("dist"): | ||
@app.get("/") | ||
@app.get("/login/{path}") | ||
async def read_index(request: Request): | ||
_logger.info(f"Request from {get_real_ipaddr(request)}") | ||
return FileResponse( | ||
"dist/index.html", | ||
headers={"Cache-Control": "no-cache"} | ||
) | ||
|
||
app.mount("/", StaticFiles(directory="dist"), name="static") | ||
|
||
_logger.info(f"settings: {settings.model_dump_json(indent=2)}") | ||
|
||
@app.exception_handler(Exception) | ||
async def exception_handler(request: Request, exc: Exception): | ||
return PlainTextResponse( | ||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
content=f"Internal Server Error: {exc}", | ||
) | ||
|
||
if __name__ == "__main__": | ||
uvicorn.run(app, host="0.0.0.0", port=8000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
fastapi==0.95.0 | ||
python-dotenv==1.0.0 | ||
uvicorn==0.21.1 | ||
openai==0.27.4 | ||
python-multipart==0.0.6 | ||
slowapi==0.1.8 | ||
pyjwt==2.7.0 | ||
fastapi==0.110.2 | ||
pydantic-settings==2.2.1 | ||
pydantic==2.7.1 | ||
uvicorn==0.29.0 | ||
openai==1.23.6 | ||
pyjwt==2.8.0 | ||
requests==2.31.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import os | ||
import logging | ||
|
||
from fastapi import FastAPI, Request, status | ||
from fastapi.middleware.cors import CORSMiddleware | ||
from fastapi.responses import PlainTextResponse, FileResponse | ||
from fastapi.staticfiles import StaticFiles | ||
|
||
from src.limiter import get_real_ipaddr | ||
from src.chatgpt_router import router as chatgpt_router | ||
from src.divination_router import router as divination_router | ||
from src.user_router import router as user_router | ||
|
||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
app = FastAPI(title="Chatgpt Divination API") | ||
app.add_middleware( | ||
CORSMiddleware, | ||
allow_origins=[ | ||
"http://localhost:5173", | ||
"http://localhost", | ||
"http://127.0.0.1" | ||
], | ||
allow_credentials=True, | ||
allow_methods=["*"], | ||
allow_headers=["*"], | ||
) | ||
|
||
app.include_router(chatgpt_router) | ||
app.include_router(divination_router) | ||
app.include_router(user_router) | ||
|
||
if os.path.exists("dist"): | ||
@app.get("/") | ||
@app.get("/login/{path}") | ||
async def read_index(request: Request): | ||
_logger.info(f"Request from {get_real_ipaddr(request)}") | ||
return FileResponse( | ||
"dist/index.html", | ||
headers={"Cache-Control": "no-cache"} | ||
) | ||
|
||
app.mount("/", StaticFiles(directory="dist"), name="static") | ||
|
||
|
||
@app.exception_handler(Exception) | ||
async def exception_handler(request: Request, exc: Exception): | ||
return PlainTextResponse( | ||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
content=f"Internal Server Error: {exc}", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import logging | ||
from typing import Tuple | ||
|
||
from pydantic import Field | ||
from pydantic_settings import BaseSettings | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
class Settings(BaseSettings): | ||
api_key: str = Field(default="sk-xxx", exclude=True) | ||
api_base: str = "https://api.openai.com/v1" | ||
model: str = "gpt-3.5-turbo" | ||
# rate limit xxx request per xx seconds | ||
rate_limit: Tuple[int, int] = (60, 60 * 60) | ||
user_rate_limit: Tuple[int, int] = (600, 60 * 60) | ||
github_client_id: str = "" | ||
github_client_secret: str = Field(default="", exclude=True) | ||
jwt_secret: str = Field(default="secret", exclude=True) | ||
ad_client: str = "" | ||
ad_slot: str = "" | ||
|
||
def get_human_rate_limit(self) -> str: | ||
max_reqs, time_window_seconds = self.rate_limit | ||
# convert to human readable format | ||
return f"{max_reqs}req/{time_window_seconds}seconds" | ||
|
||
def get_human_user_rate_limit(self) -> str: | ||
max_reqs, time_window_seconds = self.user_rate_limit | ||
# convert to human readable format | ||
return f"{max_reqs}req/{time_window_seconds}seconds" | ||
|
||
class Config: | ||
env_file = ".env" | ||
|
||
|
||
settings = Settings() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
|
||
from models import DivinationBody | ||
from src.models import DivinationBody | ||
from typing import Optional | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.