mirror of
https://github.com/flibusta-apps/users_settings_server.git
synced 2025-12-06 06:35:39 +01:00
Init
This commit is contained in:
49
.github/workflows/build_docker_image.yaml
vendored
Normal file
49
.github/workflows/build_docker_image.yaml
vendored
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
name: Build docker image
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- 'main'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
Build-Docker-Image:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
-
|
||||||
|
name: Checkout
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
|
-
|
||||||
|
name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v1
|
||||||
|
|
||||||
|
- id: repository_name
|
||||||
|
uses: ASzc/change-string-case-action@v1
|
||||||
|
with:
|
||||||
|
string: ${{ github.repository }}
|
||||||
|
|
||||||
|
-
|
||||||
|
name: Login to ghcr.io
|
||||||
|
uses: docker/login-action@v1
|
||||||
|
with:
|
||||||
|
registry: ghcr.io
|
||||||
|
username: ${{ github.actor }}
|
||||||
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
-
|
||||||
|
name: Build and push
|
||||||
|
id: docker_build
|
||||||
|
uses: docker/build-push-action@v2
|
||||||
|
env:
|
||||||
|
IMAGE: ${{ steps.repository_name.outputs.lowercase }}
|
||||||
|
with:
|
||||||
|
push: true
|
||||||
|
tags: ghcr.io/${{ env.IMAGE }}:latest
|
||||||
|
context: .
|
||||||
|
file: ./docker/build.dockerfile
|
||||||
|
|
||||||
|
-
|
||||||
|
name: Invoke deployment hook
|
||||||
|
uses: joelwmale/webhook-action@master
|
||||||
|
with:
|
||||||
|
url: ${{ secrets.WEBHOOK_URL }}
|
||||||
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
venv
|
||||||
|
|
||||||
|
.vscode
|
||||||
|
|
||||||
|
__pycache__
|
||||||
8
requirements.txt
Normal file
8
requirements.txt
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
fastapi
|
||||||
|
fastapi-pagination
|
||||||
|
pydantic
|
||||||
|
alembic
|
||||||
|
ormar
|
||||||
|
uvicorn
|
||||||
|
asyncpg
|
||||||
|
psycopg2
|
||||||
98
src/app/alembic.ini
Normal file
98
src/app/alembic.ini
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
# A generic, single database configuration.
|
||||||
|
|
||||||
|
[alembic]
|
||||||
|
# path to migration scripts
|
||||||
|
script_location = ./app/alembic
|
||||||
|
|
||||||
|
# template used to generate migration files
|
||||||
|
# file_template = %%(rev)s_%%(slug)s
|
||||||
|
|
||||||
|
# sys.path path, will be prepended to sys.path if present.
|
||||||
|
# defaults to the current working directory.
|
||||||
|
prepend_sys_path = .
|
||||||
|
|
||||||
|
# timezone to use when rendering the date within the migration file
|
||||||
|
# as well as the filename.
|
||||||
|
# If specified, requires the python-dateutil library that can be
|
||||||
|
# installed by adding `alembic[tz]` to the pip requirements
|
||||||
|
# string value is passed to dateutil.tz.gettz()
|
||||||
|
# leave blank for localtime
|
||||||
|
# timezone =
|
||||||
|
|
||||||
|
# max length of characters to apply to the
|
||||||
|
# "slug" field
|
||||||
|
# truncate_slug_length = 40
|
||||||
|
|
||||||
|
# set to 'true' to run the environment during
|
||||||
|
# the 'revision' command, regardless of autogenerate
|
||||||
|
# revision_environment = false
|
||||||
|
|
||||||
|
# set to 'true' to allow .pyc and .pyo files without
|
||||||
|
# a source .py file to be detected as revisions in the
|
||||||
|
# versions/ directory
|
||||||
|
# sourceless = false
|
||||||
|
|
||||||
|
# version location specification; This defaults
|
||||||
|
# to alembic/versions. When using multiple version
|
||||||
|
# directories, initial revisions must be specified with --version-path.
|
||||||
|
# The path separator used here should be the separator specified by "version_path_separator"
|
||||||
|
# version_locations = %(here)s/bar:%(here)s/bat:alembic/versions
|
||||||
|
|
||||||
|
# version path separator; As mentioned above, this is the character used to split
|
||||||
|
# version_locations. Valid values are:
|
||||||
|
#
|
||||||
|
# version_path_separator = :
|
||||||
|
# version_path_separator = ;
|
||||||
|
# version_path_separator = space
|
||||||
|
version_path_separator = os # default: use os.pathsep
|
||||||
|
|
||||||
|
# the output encoding used when revision files
|
||||||
|
# are written from script.py.mako
|
||||||
|
# output_encoding = utf-8
|
||||||
|
|
||||||
|
|
||||||
|
[post_write_hooks]
|
||||||
|
# post_write_hooks defines scripts or Python functions that are run
|
||||||
|
# on newly generated revision scripts. See the documentation for further
|
||||||
|
# detail and examples
|
||||||
|
|
||||||
|
# format using "black" - use the console_scripts runner, against the "black" entrypoint
|
||||||
|
# hooks = black
|
||||||
|
# black.type = console_scripts
|
||||||
|
# black.entrypoint = black
|
||||||
|
# black.options = -l 79 REVISION_SCRIPT_FILENAME
|
||||||
|
|
||||||
|
# Logging configuration
|
||||||
|
[loggers]
|
||||||
|
keys = root,sqlalchemy,alembic
|
||||||
|
|
||||||
|
[handlers]
|
||||||
|
keys = console
|
||||||
|
|
||||||
|
[formatters]
|
||||||
|
keys = generic
|
||||||
|
|
||||||
|
[logger_root]
|
||||||
|
level = WARN
|
||||||
|
handlers = console
|
||||||
|
qualname =
|
||||||
|
|
||||||
|
[logger_sqlalchemy]
|
||||||
|
level = WARN
|
||||||
|
handlers =
|
||||||
|
qualname = sqlalchemy.engine
|
||||||
|
|
||||||
|
[logger_alembic]
|
||||||
|
level = INFO
|
||||||
|
handlers =
|
||||||
|
qualname = alembic
|
||||||
|
|
||||||
|
[handler_console]
|
||||||
|
class = StreamHandler
|
||||||
|
args = (sys.stderr,)
|
||||||
|
level = NOTSET
|
||||||
|
formatter = generic
|
||||||
|
|
||||||
|
[formatter_generic]
|
||||||
|
format = %(levelname)-5.5s [%(name)s] %(message)s
|
||||||
|
datefmt = %H:%M:%S
|
||||||
1
src/app/alembic/README
Normal file
1
src/app/alembic/README
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Generic single-database configuration.
|
||||||
64
src/app/alembic/env.py
Normal file
64
src/app/alembic/env.py
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
from alembic import context
|
||||||
|
import sys, os
|
||||||
|
|
||||||
|
from sqlalchemy.engine import create_engine
|
||||||
|
|
||||||
|
from core.db import DATABASE_URL
|
||||||
|
|
||||||
|
|
||||||
|
myPath = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
sys.path.insert(0, myPath + '/../../')
|
||||||
|
|
||||||
|
config = context.config
|
||||||
|
|
||||||
|
|
||||||
|
from app.models import BaseMeta
|
||||||
|
target_metadata = BaseMeta.metadata
|
||||||
|
|
||||||
|
|
||||||
|
def run_migrations_offline():
|
||||||
|
"""Run migrations in 'offline' mode.
|
||||||
|
|
||||||
|
This configures the context with just a URL
|
||||||
|
and not an Engine, though an Engine is acceptable
|
||||||
|
here as well. By skipping the Engine creation
|
||||||
|
we don't even need a DBAPI to be available.
|
||||||
|
|
||||||
|
Calls to context.execute() here emit the given string to the
|
||||||
|
script output.
|
||||||
|
|
||||||
|
"""
|
||||||
|
url = config.get_main_option("sqlalchemy.url")
|
||||||
|
context.configure(
|
||||||
|
url=url,
|
||||||
|
target_metadata=target_metadata,
|
||||||
|
literal_binds=True,
|
||||||
|
dialect_opts={"paramstyle": "named"},
|
||||||
|
)
|
||||||
|
|
||||||
|
with context.begin_transaction():
|
||||||
|
context.run_migrations()
|
||||||
|
|
||||||
|
|
||||||
|
def run_migrations_online():
|
||||||
|
"""Run migrations in 'online' mode.
|
||||||
|
|
||||||
|
In this scenario we need to create an Engine
|
||||||
|
and associate a connection with the context.
|
||||||
|
|
||||||
|
"""
|
||||||
|
connectable = create_engine(DATABASE_URL)
|
||||||
|
|
||||||
|
with connectable.connect() as connection:
|
||||||
|
context.configure(
|
||||||
|
connection=connection, target_metadata=target_metadata, compare_type=True
|
||||||
|
)
|
||||||
|
|
||||||
|
with context.begin_transaction():
|
||||||
|
context.run_migrations()
|
||||||
|
|
||||||
|
|
||||||
|
if context.is_offline_mode():
|
||||||
|
run_migrations_offline()
|
||||||
|
else:
|
||||||
|
run_migrations_online()
|
||||||
24
src/app/alembic/script.py.mako
Normal file
24
src/app/alembic/script.py.mako
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
"""${message}
|
||||||
|
|
||||||
|
Revision ID: ${up_revision}
|
||||||
|
Revises: ${down_revision | comma,n}
|
||||||
|
Create Date: ${create_date}
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
${imports if imports else ""}
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = ${repr(up_revision)}
|
||||||
|
down_revision = ${repr(down_revision)}
|
||||||
|
branch_labels = ${repr(branch_labels)}
|
||||||
|
depends_on = ${repr(depends_on)}
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
${upgrades if upgrades else "pass"}
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
${downgrades if downgrades else "pass"}
|
||||||
53
src/app/alembic/versions/7e45f53febe1_.py
Normal file
53
src/app/alembic/versions/7e45f53febe1_.py
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
"""empty message
|
||||||
|
|
||||||
|
Revision ID: 7e45f53febe1
|
||||||
|
Revises:
|
||||||
|
Create Date: 2021-12-03 22:42:22.514771
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '7e45f53febe1'
|
||||||
|
down_revision = None
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.create_table('languages',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('label', sa.String(length=16), nullable=False),
|
||||||
|
sa.Column('code', sa.String(length=4), nullable=False),
|
||||||
|
sa.PrimaryKeyConstraint('id'),
|
||||||
|
sa.UniqueConstraint('code')
|
||||||
|
)
|
||||||
|
op.create_table('user_settings',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('user_id', sa.BigInteger(), nullable=False),
|
||||||
|
sa.Column('last_name', sa.String(length=64), nullable=False),
|
||||||
|
sa.Column('first_name', sa.String(length=64), nullable=False),
|
||||||
|
sa.Column('username', sa.String(length=32), nullable=False),
|
||||||
|
sa.PrimaryKeyConstraint('id'),
|
||||||
|
sa.UniqueConstraint('user_id')
|
||||||
|
)
|
||||||
|
op.create_table('users_languages',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('language', sa.Integer(), nullable=True),
|
||||||
|
sa.Column('user', sa.Integer(), nullable=True),
|
||||||
|
sa.ForeignKeyConstraint(['language'], ['languages.id'], name='fk_users_languages_languages_language_id', onupdate='CASCADE', ondelete='CASCADE'),
|
||||||
|
sa.ForeignKeyConstraint(['user'], ['user_settings.id'], name='fk_users_languages_user_settings_user_id', onupdate='CASCADE', ondelete='CASCADE'),
|
||||||
|
sa.PrimaryKeyConstraint('id')
|
||||||
|
)
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.drop_table('users_languages')
|
||||||
|
op.drop_table('user_settings')
|
||||||
|
op.drop_table('languages')
|
||||||
|
# ### end Alembic commands ###
|
||||||
9
src/app/depends.py
Normal file
9
src/app/depends.py
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
from fastapi import Security, HTTPException, status
|
||||||
|
|
||||||
|
from core.auth import default_security
|
||||||
|
from core.config import env_config
|
||||||
|
|
||||||
|
|
||||||
|
async def check_token(api_key: str = Security(default_security)):
|
||||||
|
if api_key != env_config.API_KEY:
|
||||||
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Wrong api key!")
|
||||||
32
src/app/models.py
Normal file
32
src/app/models.py
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import ormar
|
||||||
|
|
||||||
|
from core.db import metadata, database
|
||||||
|
|
||||||
|
|
||||||
|
class BaseMeta(ormar.ModelMeta):
|
||||||
|
metadata = metadata
|
||||||
|
database = database
|
||||||
|
|
||||||
|
|
||||||
|
class Language(ormar.Model):
|
||||||
|
class Meta(BaseMeta):
|
||||||
|
tablename = "languages"
|
||||||
|
|
||||||
|
id: int = ormar.Integer(primary_key=True) # type: ignore
|
||||||
|
label: str = ormar.String(max_length=16) # type: ignore
|
||||||
|
code: str = ormar.String(max_length=4, unique=True) # type: ignore
|
||||||
|
|
||||||
|
|
||||||
|
class User(ormar.Model):
|
||||||
|
class Meta(BaseMeta):
|
||||||
|
tablename = "user_settings"
|
||||||
|
|
||||||
|
id: int = ormar.Integer(primary_key=True) # type: ignore
|
||||||
|
|
||||||
|
user_id: int = ormar.BigInteger(unique=True) # type: ignore
|
||||||
|
last_name: str = ormar.String(max_length=64) # type: ignore
|
||||||
|
first_name: str = ormar.String(max_length=64) # type: ignore
|
||||||
|
username: str = ormar.String(max_length=32) # type: ignore
|
||||||
|
source: str = ormar.String(max_length=32) # type: ignore
|
||||||
|
|
||||||
|
allowed_langs = ormar.ManyToMany(Language)
|
||||||
22
src/app/serializers.py
Normal file
22
src/app/serializers.py
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
from pydantic import BaseModel, constr
|
||||||
|
|
||||||
|
|
||||||
|
class LanguageDetail(BaseModel):
|
||||||
|
id: int
|
||||||
|
label: constr(max_length=16) # type: ignore
|
||||||
|
code: constr(max_length=4) # type: ignore
|
||||||
|
|
||||||
|
|
||||||
|
class UserBase(BaseModel):
|
||||||
|
user_id: int
|
||||||
|
last_name: constr(max_length=64) # type: ignore
|
||||||
|
first_name: constr(max_length=64) # type: ignore
|
||||||
|
username: constr(max_length=32) # type: ignore
|
||||||
|
|
||||||
|
|
||||||
|
class UserCreateOrUpdate(BaseModel):
|
||||||
|
allowed_langs: list[str]
|
||||||
|
|
||||||
|
|
||||||
|
class UserDetail(UserCreateOrUpdate):
|
||||||
|
allowed_langs: list[LanguageDetail]
|
||||||
72
src/app/views.py
Normal file
72
src/app/views.py
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
from fastapi import APIRouter, HTTPException, status
|
||||||
|
|
||||||
|
from app.serializers import UserCreateOrUpdate, UserDetail, LanguageDetail
|
||||||
|
from app.models import User, Language
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: add redis cache
|
||||||
|
|
||||||
|
|
||||||
|
users_router = APIRouter(
|
||||||
|
prefix="/users",
|
||||||
|
tags=["users"]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@users_router.get("/", response_model=list[UserDetail])
|
||||||
|
async def get_users():
|
||||||
|
return await User.objects.select_related("allowed_langs").all()
|
||||||
|
|
||||||
|
|
||||||
|
@users_router.get("/{user_id}", response_model=UserDetail)
|
||||||
|
async def get_user(user_id: int):
|
||||||
|
user_data = await User.objects.select_related("allowd_langs").get_or_none(user_id=user_id)
|
||||||
|
|
||||||
|
if user_data is None:
|
||||||
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||||
|
|
||||||
|
return user_data
|
||||||
|
|
||||||
|
|
||||||
|
@users_router.post("/{user_id}", response_model=UserDetail)
|
||||||
|
async def get_or_update_user(user_id: int, data: UserCreateOrUpdate):
|
||||||
|
data_dict = data.dict()
|
||||||
|
|
||||||
|
user_data = await User.objects.select_related("allowed_langs").get_or_none(user_id=user_id)
|
||||||
|
|
||||||
|
allowed_langs = data_dict.pop("allowed_langs")
|
||||||
|
|
||||||
|
if user_data is None:
|
||||||
|
return User.objects.create(**data.dict())
|
||||||
|
else:
|
||||||
|
user_data.update_from_dict(data.dict())
|
||||||
|
|
||||||
|
user_data.allowed_langs.clear()
|
||||||
|
|
||||||
|
langs = await Language.objects.filter(code__in=allowed_langs).all()
|
||||||
|
|
||||||
|
for lang in langs:
|
||||||
|
await user_data.allowed_langs.add(lang)
|
||||||
|
|
||||||
|
return user_data
|
||||||
|
|
||||||
|
|
||||||
|
languages_router = APIRouter(
|
||||||
|
prefix="/languages",
|
||||||
|
tags=["languages"]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@languages_router.get("/", response_model=list[LanguageDetail])
|
||||||
|
async def get_languages():
|
||||||
|
return await Language.objects.all()
|
||||||
|
|
||||||
|
|
||||||
|
@languages_router.get("/{code}", response_model=LanguageDetail)
|
||||||
|
async def get_language(code: str):
|
||||||
|
language = await Language.objects.get_or_none(code=code)
|
||||||
|
|
||||||
|
if language is None:
|
||||||
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||||
|
|
||||||
|
return language
|
||||||
27
src/core/app.py
Normal file
27
src/core/app.py
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
from fastapi import FastAPI
|
||||||
|
|
||||||
|
from core.db import database
|
||||||
|
from app.views import users_router, languages_router
|
||||||
|
|
||||||
|
|
||||||
|
def start_app() -> FastAPI:
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
app.include_router(users_router)
|
||||||
|
app.include_router(languages_router)
|
||||||
|
|
||||||
|
app.state.database = database
|
||||||
|
|
||||||
|
@app.on_event('startup')
|
||||||
|
async def startup() -> None:
|
||||||
|
database_ = app.state.database
|
||||||
|
if not database_.is_connected:
|
||||||
|
await database_.connect()
|
||||||
|
|
||||||
|
@app.on_event('shutdown')
|
||||||
|
async def shutdown() -> None:
|
||||||
|
database_ = app.state.database
|
||||||
|
if database_.is_connected:
|
||||||
|
await database_.disconnect()
|
||||||
|
|
||||||
|
return app
|
||||||
4
src/core/auth.py
Normal file
4
src/core/auth.py
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
from fastapi.security import APIKeyHeader
|
||||||
|
|
||||||
|
|
||||||
|
default_security = APIKeyHeader(name="Authorization")
|
||||||
14
src/core/config.py
Normal file
14
src/core/config.py
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
from pydantic import BaseSettings
|
||||||
|
|
||||||
|
|
||||||
|
class EnvConfig(BaseSettings):
|
||||||
|
API_KEY: str
|
||||||
|
|
||||||
|
POSTGRES_USER: str
|
||||||
|
POSTGRES_PASSWORD: str
|
||||||
|
POSTGRES_HOST: str
|
||||||
|
POSTGRES_PORT: int
|
||||||
|
POSTGRES_DB: str
|
||||||
|
|
||||||
|
|
||||||
|
env_config = EnvConfig()
|
||||||
15
src/core/db.py
Normal file
15
src/core/db.py
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
from urllib.parse import quote
|
||||||
|
from databases import Database
|
||||||
|
|
||||||
|
from sqlalchemy import MetaData
|
||||||
|
|
||||||
|
from core.config import env_config
|
||||||
|
|
||||||
|
|
||||||
|
DATABASE_URL = (
|
||||||
|
f"postgresql://{env_config.POSTGRES_USER}:{quote(env_config.POSTGRES_PASSWORD)}@"
|
||||||
|
f"{env_config.POSTGRES_HOST}:{env_config.POSTGRES_PORT}/{env_config.POSTGRES_DB}"
|
||||||
|
)
|
||||||
|
|
||||||
|
metadata = MetaData()
|
||||||
|
database = Database(DATABASE_URL)
|
||||||
4
src/main.py
Normal file
4
src/main.py
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
from core.app import start_app
|
||||||
|
|
||||||
|
|
||||||
|
app = start_app()
|
||||||
Reference in New Issue
Block a user