forked from fastapi/full-stack-fastapi-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
👷 Add continuous deployment and refactors needed for it (fastapi#667)
- Loading branch information
Showing
12 changed files
with
331 additions
and
102 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
name: Deploy to Production | ||
|
||
on: | ||
release: | ||
types: | ||
- published | ||
|
||
jobs: | ||
deploy: | ||
runs-on: | ||
- self-hosted | ||
- production | ||
env: | ||
ENVIRONMENT: production | ||
DOMAIN: ${{ secrets.DOMAIN_PRODUCTION }} | ||
SECRET_KEY: ${{ secrets.SECRET_KEY }} | ||
FIRST_SUPERUSER: ${{ secrets.FIRST_SUPERUSER }} | ||
FIRST_SUPERUSER_PASSWORD: ${{ secrets.FIRST_SUPERUSER_PASSWORD }} | ||
SMTP_HOST: ${{ secrets.SMTP_HOST }} | ||
SMTP_USER: ${{ secrets.SMTP_USER }} | ||
SMTP_PASSWORD: ${{ secrets.SMTP_PASSWORD }} | ||
EMAILS_FROM_EMAIL: ${{ secrets.EMAILS_FROM_EMAIL }} | ||
POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }} | ||
PGADMIN_DEFAULT_EMAIL: ${{ secrets.PGADMIN_DEFAULT_EMAIL }} | ||
PGADMIN_DEFAULT_PASSWORD: ${{ secrets.PGADMIN_DEFAULT_PASSWORD }} | ||
SENTRY_DSN: ${{ secrets.SENTRY_DSN }} | ||
FLOWER_BASIC_AUTH: ${{ secrets.FLOWER_BASIC_AUTH }} | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
- run: docker compose -f docker-compose.yml build | ||
- run: docker compose -f docker-compose.yml up -d |
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,32 @@ | ||
name: Deploy to Staging | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
deploy: | ||
runs-on: | ||
- self-hosted | ||
- staging | ||
env: | ||
ENVIRONMENT: staging | ||
DOMAIN: ${{ secrets.DOMAIN_STAGING }} | ||
SECRET_KEY: ${{ secrets.SECRET_KEY }} | ||
FIRST_SUPERUSER: ${{ secrets.FIRST_SUPERUSER }} | ||
FIRST_SUPERUSER_PASSWORD: ${{ secrets.FIRST_SUPERUSER_PASSWORD }} | ||
SMTP_HOST: ${{ secrets.SMTP_HOST }} | ||
SMTP_USER: ${{ secrets.SMTP_USER }} | ||
SMTP_PASSWORD: ${{ secrets.SMTP_PASSWORD }} | ||
EMAILS_FROM_EMAIL: ${{ secrets.EMAILS_FROM_EMAIL }} | ||
POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }} | ||
PGADMIN_DEFAULT_EMAIL: ${{ secrets.PGADMIN_DEFAULT_EMAIL }} | ||
PGADMIN_DEFAULT_PASSWORD: ${{ secrets.PGADMIN_DEFAULT_PASSWORD }} | ||
SENTRY_DSN: ${{ secrets.SENTRY_DSN }} | ||
FLOWER_BASIC_AUTH: ${{ secrets.FLOWER_BASIC_AUTH }} | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
- run: docker compose -f docker-compose.yml build | ||
- run: docker compose -f docker-compose.yml up -d |
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 |
---|---|---|
@@ -1,95 +1,95 @@ | ||
import secrets | ||
from typing import Any | ||
from typing import Annotated, Any, Literal | ||
|
||
from pydantic import ( | ||
AnyHttpUrl, | ||
AnyUrl, | ||
BeforeValidator, | ||
HttpUrl, | ||
PostgresDsn, | ||
ValidationInfo, | ||
field_validator, | ||
computed_field, | ||
model_validator, | ||
) | ||
from pydantic_core import MultiHostUrl | ||
from pydantic_settings import BaseSettings, SettingsConfigDict | ||
from typing_extensions import Self | ||
|
||
|
||
def parse_cors(v: Any) -> list[str] | str: | ||
if isinstance(v, str) and not v.startswith("["): | ||
return [i.strip() for i in v.split(",")] | ||
elif isinstance(v, list | str): | ||
return v | ||
raise ValueError(v) | ||
|
||
|
||
class Settings(BaseSettings): | ||
model_config = SettingsConfigDict(env_file=".env", env_ignore_empty=True) | ||
API_V1_STR: str = "/api/v1" | ||
SECRET_KEY: str = secrets.token_urlsafe(32) | ||
# 60 minutes * 24 hours * 8 days = 8 days | ||
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8 | ||
SERVER_HOST: AnyHttpUrl | ||
BACKEND_CORS_ORIGINS: list[AnyHttpUrl] | str = [] | ||
|
||
@field_validator("BACKEND_CORS_ORIGINS", mode="before") | ||
@classmethod | ||
def assemble_cors_origins(cls, v: str | list[str]) -> list[str] | str: | ||
if isinstance(v, str) and not v.startswith("["): | ||
return [i.strip() for i in v.split(",")] | ||
elif isinstance(v, list | str): | ||
return v | ||
raise ValueError(v) | ||
DOMAIN: str = "localhost" | ||
ENVIRONMENT: Literal["local", "staging", "production"] = "local" | ||
|
||
PROJECT_NAME: str | ||
SENTRY_DSN: HttpUrl | None = None | ||
@computed_field # type: ignore[misc] | ||
@property | ||
def server_host(self) -> str: | ||
# Use HTTPS for anything other than local development | ||
if self.ENVIRONMENT == "local": | ||
return f"http://{self.DOMAIN}" | ||
return f"https://{self.DOMAIN}" | ||
|
||
@field_validator("SENTRY_DSN", mode="before") | ||
@classmethod | ||
def sentry_dsn_can_be_blank(cls, v: str) -> str | None: | ||
if not v: | ||
return None | ||
return v | ||
BACKEND_CORS_ORIGINS: Annotated[ | ||
list[AnyUrl] | str, BeforeValidator(parse_cors) | ||
] = [] | ||
|
||
PROJECT_NAME: str | ||
SENTRY_DSN: HttpUrl | None = None | ||
POSTGRES_SERVER: str | ||
POSTGRES_USER: str | ||
POSTGRES_PASSWORD: str | ||
POSTGRES_DB: str | ||
SQLALCHEMY_DATABASE_URI: PostgresDsn | None = None | ||
|
||
@field_validator("SQLALCHEMY_DATABASE_URI", mode="before") | ||
def assemble_db_connection(cls, v: str | None, info: ValidationInfo) -> Any: | ||
if isinstance(v, str): | ||
return v | ||
return PostgresDsn.build( | ||
POSTGRES_DB: str = "" | ||
|
||
@computed_field # type: ignore[misc] | ||
@property | ||
def SQLALCHEMY_DATABASE_URI(self) -> PostgresDsn: | ||
return MultiHostUrl.build( | ||
scheme="postgresql+psycopg", | ||
username=info.data.get("POSTGRES_USER"), | ||
password=info.data.get("POSTGRES_PASSWORD"), | ||
host=info.data.get("POSTGRES_SERVER"), | ||
path=f"{info.data.get('POSTGRES_DB') or ''}", | ||
username=self.POSTGRES_USER, | ||
password=self.POSTGRES_PASSWORD, | ||
host=self.POSTGRES_SERVER, | ||
path=self.POSTGRES_DB, | ||
) | ||
|
||
SMTP_TLS: bool = True | ||
SMTP_PORT: int | None = None | ||
SMTP_PORT: int = 587 | ||
SMTP_HOST: str | None = None | ||
SMTP_USER: str | None = None | ||
SMTP_PASSWORD: str | None = None | ||
# TODO: update type to EmailStr when sqlmodel supports it | ||
EMAILS_FROM_EMAIL: str | None = None | ||
EMAILS_FROM_NAME: str | None = None | ||
|
||
@field_validator("EMAILS_FROM_NAME") | ||
def get_project_name(cls, v: str | None, info: ValidationInfo) -> str: | ||
if not v: | ||
return str(info.data["PROJECT_NAME"]) | ||
return v | ||
@model_validator(mode="after") | ||
def set_default_emails_from(self) -> Self: | ||
if not self.EMAILS_FROM_NAME: | ||
self.EMAILS_FROM_NAME = self.PROJECT_NAME | ||
return self | ||
|
||
EMAIL_RESET_TOKEN_EXPIRE_HOURS: int = 48 | ||
EMAIL_TEMPLATES_DIR: str = "/app/app/email-templates/build" | ||
EMAILS_ENABLED: bool = False | ||
|
||
@field_validator("EMAILS_ENABLED", mode="before") | ||
def get_emails_enabled(cls, v: bool, info: ValidationInfo) -> bool: | ||
return bool( | ||
info.data.get("SMTP_HOST") | ||
and info.data.get("SMTP_PORT") | ||
and info.data.get("EMAILS_FROM_EMAIL") | ||
) | ||
|
||
@computed_field # type: ignore[misc] | ||
@property | ||
def emails_enabled(self) -> bool: | ||
return bool(self.SMTP_HOST and self.EMAILS_FROM_EMAIL) | ||
|
||
# TODO: update type to EmailStr when sqlmodel supports it | ||
EMAIL_TEST_USER: str = "test@example.com" | ||
# TODO: update type to EmailStr when sqlmodel supports it | ||
FIRST_SUPERUSER: str | ||
FIRST_SUPERUSER_PASSWORD: str | ||
USERS_OPEN_REGISTRATION: bool = False | ||
model_config = SettingsConfigDict(env_file=".env") | ||
|
||
|
||
settings = Settings() # type: ignore |
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.