-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(optimize): optimize project template
- Loading branch information
1 parent
c59b432
commit ded9e49
Showing
17 changed files
with
362 additions
and
134 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from typing import Any | ||
|
||
from fastapi import FastAPI | ||
|
||
from src.app import app | ||
from src.core.config import settings | ||
from src.loggers import LOGGING, configure_logger | ||
|
||
|
||
def gunicorn_options() -> dict[str, Any]: | ||
from gunicorn import glogging | ||
|
||
class GunicornLogger(glogging.Logger): | ||
def setup(self, cfg: Any) -> None: # noqa: ARG002 | ||
configure_logger() | ||
|
||
return { | ||
"bind": f"{settings.LISTENING_HOST}:{settings.LISTENING_PORT}", | ||
"workers": settings.WORKERS, | ||
"worker_class": "uvicorn.workers.UvicornWorker", | ||
"preload": "-", | ||
"forwarded_allow_ips": "*", | ||
"accesslog": "-", | ||
"errorlog": "-", | ||
"logger_class": GunicornLogger, | ||
} | ||
|
||
|
||
if __name__ == "__main__": | ||
if settings.RUNNING_MODE == "uvicorn": | ||
import uvicorn | ||
|
||
uvicorn.run( | ||
app, | ||
host=settings.LISTENING_HOST, | ||
port=settings.LISTENING_PORT, | ||
log_config=LOGGING, | ||
proxy_headers=True, | ||
forwarded_allow_ips="*", | ||
loop="uvloop", | ||
http="httptools", | ||
) | ||
|
||
else: | ||
from gunicorn.app.base import BaseApplication | ||
|
||
class StandaloneApplication(BaseApplication): | ||
def __init__(self, app: FastAPI, options: dict | None = None) -> None: | ||
self.options = options or {} | ||
self.application: FastAPI = app | ||
super().__init__() | ||
|
||
def load_config(self) -> None: | ||
assert self.cfg is not None # noqa: S101 | ||
# Filter out options that are not recognized by gunicorn | ||
filtered_options = {key: value for key, value in self.options.items() if key in self.cfg.settings} | ||
|
||
# Set the filtered options | ||
for key, value in filtered_options.items(): | ||
self.cfg.set(key.lower(), value) | ||
|
||
def load(self) -> FastAPI: | ||
return self.application | ||
|
||
StandaloneApplication(app, gunicorn_options()).run() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import io | ||
from pathlib import Path | ||
|
||
import pandas as pd | ||
|
||
|
||
def bytes_to_human_readable(bytes_: int, format_string: str = "%(value).1f%(symbol)s") -> str: | ||
symbols = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB") | ||
prefix = {} | ||
for i, s in enumerate(symbols[1:]): | ||
prefix[s] = 1 << (i + 1) * 10 | ||
for symbol in reversed(symbols[1:]): | ||
if bytes_ >= prefix[symbol]: | ||
value = float(bytes_) / prefix[symbol] | ||
return format_string % locals() | ||
return format_string % {"symbol": symbols[0], "value": bytes_} | ||
|
||
|
||
def export_csv(data: list[dict]) -> str: | ||
df = pd.DataFrame(data) | ||
binary_data = io.StringIO() | ||
df.to_csv(binary_data, index=False, encoding="utf-8") | ||
return binary_data.getvalue() | ||
|
||
|
||
def get_file_size(path: str) -> str: | ||
return bytes_to_human_readable(Path(path).stat().st_size) | ||
|
||
|
||
def format_duration(seconds: float) -> str: | ||
duration_units = [ | ||
("week", 60 * 60 * 24 * 7), | ||
("day", 60 * 60 * 24), | ||
("hour", 60 * 60), | ||
("minute", 60), | ||
("second", 1), | ||
] | ||
|
||
parts = [] | ||
for unit, duration in duration_units: | ||
count, seconds = divmod(int(seconds), duration) | ||
if count: | ||
parts.append(f'{count} {unit}{"s" if count > 1 else ""}') | ||
|
||
return ", ".join(parts) if parts else "0 seconds" |
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,4 @@ | ||
translations = { | ||
"en_US": {}, | ||
"zh_CN": {}, | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.