Skip to content
Snippets Groups Projects
Unverified Commit dc642196 authored by Pavel Kirilin's avatar Pavel Kirilin :alien:
Browse files

Updated production run mode.

Description:
- Fixed gunicorn;
- Updated default response model for speed.

Signed-off-by: default avatarPavel Kirilin <win10@list.ru>
parent d2761278
No related branches found
No related tags found
No related merge requests found
# Local docker-compose configuration
version: '3.7'
services:
back:
container_name: {{ cookiecutter.project_name }}_backend
build:
context: .
target: production
labels:
{{ cookiecutter.project_name }}.description: {{ cookiecutter.project_description }}
env_file:
- envs/.env
ports:
- 8402:8000
depends_on:
- db
networks:
- {{ cookiecutter.project_name }}_network
db:
container_name: {{ cookiecutter.project_name }}_db
image: postgres:12.4
volumes:
- {{ cookiecutter.project_name }}_db_data:/var/lib/postgresql/data
networks:
- {{ cookiecutter.project_name }}_network
env_file:
- envs/.env
{% if cookiecutter.add_redis == "True" -%}
redis:
container_name: {{ cookiecutter.project_name }}_redis
image: bitnami/redis:6.0.7
volumes:
- {{ cookiecutter.project_name }}_redis_data:/bitnami/redis/data
env_file:
- envs/.env
networks:
- {{ cookiecutter.project_name }}_network
{% endif %}
{% if cookiecutter.add_scheduler == "True" -%}
scheduler:
container_name: {{ cookiecutter.project_name }}_scheduler
build:
context: .
target: scheduler
labels:
scheduler.description: "{{ cookiecutter.project_name }} scheduler image"
env_file:
- envs/.env
volumes:
- ./:/app
depends_on:
- db
networks:
- {{ cookiecutter.project_name }}_network
{% endif %}
volumes:
{{ cookiecutter.project_name }}_db_data:
{{ cookiecutter.project_name }}_redis_data:
networks:
{{ cookiecutter.project_name }}_network:
name: {{ cookiecutter.project_name }}_network
\ No newline at end of file
......@@ -4,12 +4,12 @@ version: '3.7'
services:
back:
container_name: {{ cookiecutter.project_name }}_backend
container_name: {{ cookiecutter.project_name }}_backend_dev
build:
context: .
target: development
labels:
detector.description: {{ cookiecutter.project_description }}
{{ cookiecutter.project_name }}.description: {{ cookiecutter.project_description }}
env_file:
- envs/.env
ports:
......@@ -19,35 +19,37 @@ services:
depends_on:
- db
networks:
- {{ cookiecutter.project_name }}_network
- {{ cookiecutter.project_name }}_network_dev
db:
container_name: {{ cookiecutter.project_name }}_db
container_name: {{ cookiecutter.project_name }}_db_dev
image: postgres:12.4
volumes:
- {{ cookiecutter.project_name }}_db_data:/var/lib/postgresql/data
- {{ cookiecutter.project_name }}_db_data_dev:/var/lib/postgresql/data
ports:
- 5432:5432
networks:
- {{ cookiecutter.project_name }}_network
- {{ cookiecutter.project_name }}_network_dev
env_file:
- envs/.env
{% if cookiecutter.add_redis == "True" -%}
redis:
container_name: {{ cookiecutter.project_name }}_redis
container_name: {{ cookiecutter.project_name }}_redis_dev
image: bitnami/redis:6.0.7
volumes:
- {{ cookiecutter.project_name }}_redis_data:/bitnami/redis/data
- {{ cookiecutter.project_name }}_redis_data_dev:/bitnami/redis/data
env_file:
- envs/.env
ports:
- 6379:6379
networks:
- {{ cookiecutter.project_name }}_network
- {{ cookiecutter.project_name }}_network_dev
{% endif %}
{% if cookiecutter.add_scheduler == "True" -%}
scheduler:
container_name: {{ cookiecutter.project_name }}_scheduler
container_name: {{ cookiecutter.project_name }}_scheduler_dev
build:
context: .
target: scheduler
......@@ -60,13 +62,13 @@ services:
depends_on:
- db
networks:
- {{ cookiecutter.project_name }}_network
- {{ cookiecutter.project_name }}_network_dev
{% endif %}
volumes:
{{ cookiecutter.project_name }}_db_data:
{{ cookiecutter.project_name }}_redis_data:
{{ cookiecutter.project_name }}_db_data_dev:
{{ cookiecutter.project_name }}_redis_data_dev:
networks:
{{ cookiecutter.project_name }}_network:
name: {{ cookiecutter.project_name }}_network
\ No newline at end of file
{{ cookiecutter.project_name }}_network_dev:
name: {{ cookiecutter.project_name }}_network_dev
\ No newline at end of file
import uvicorn
import argparse
from typing import Any, Dict, Optional
from fastapi import FastAPI
from gunicorn.app.base import BaseApplication
from src.server import app
class StandaloneApplication(BaseApplication):
def __init__(self, application_instance: FastAPI, run_options: Optional[Dict[str, Any]] = None):
self.options = run_options or {}
self.application = application_instance
super().__init__()
def load_config(self) -> None:
config = {
key: value
for key, value in self.options.items()
if key in self.cfg.settings and value is not None
}
for key, value in config.items():
self.cfg.set(key.lower(), value)
def load(self) -> FastAPI:
return self.application
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument("--host", type=str, default="0.0.0.0")
parser.add_argument("--port", type=int, default=8000)
parser.add_argument("--pid-file", type=str, default="/tmp/fastapi_service.pid")
return parser.parse_args()
from src.settings import settings
if __name__ == "__main__":
uvicorn.run(
"src.server:app", host="0.0.0.0", port=8000, log_level=settings.log_level
)
args = parse_args()
options = {
"bind": f"{args.host}:{args.port}",
"workers": 1,
"worker_class": "uvicorn.workers.UvicornWorker",
"pidfile": args.pid_file,
}
StandaloneApplication(app, options).run()
......@@ -12,6 +12,9 @@ sqlalchemy = "^1.3.19"
loguru = "^0.5.2"
alembic = "^1.4.3"
httpx = "^0.14.3"
ujson = "^4.0.1"
gunicorn = "^20.0.4"
httptools = "^0.1.1"
{% if cookiecutter.pg_driver == "aiopg" -%}
aiopg = "^1.0.0"
{% else %}
......
......@@ -5,6 +5,7 @@ import alembic.config
from fastapi import FastAPI
from loguru import logger
from starlette.requests import Request
from starlette.responses import UJSONResponse
from src.api import api_router
{% if cookiecutter.pg_driver == "aiopg" -%}
......@@ -31,6 +32,7 @@ logger.configure(
app = FastAPI(
title="{{cookiecutter.project_name}}",
description="{{cookiecutter.project_description}}",
default_response_class=UJSONResponse
)
app.include_router(prefix="", router=api_router)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment