Skip to content

Commit

Permalink
#275 final working example. App is started as usual in the background…
Browse files Browse the repository at this point in the history
…, and the FastAPI server is running also.
  • Loading branch information
kozaka-tv committed Jul 2, 2024
1 parent b5725bd commit 79d66bb
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 115 deletions.
Empty file added common/__init__.py
Empty file.
File renamed without changes.
6 changes: 6 additions & 0 deletions common/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from enum import Enum


class Tags(Enum):
GETTERS = "get methods"
USERS = "users"
Empty file added modules/api/__init__.py
Empty file.
21 changes: 21 additions & 0 deletions modules/api/users_api_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from fastapi import APIRouter

from common.enums import Tags

router = APIRouter()
router.tags.append(Tags.USERS)


@router.get("/users/")
async def read_users():
return [{"username": "Foo"}, {"username": "Bar"}]


@router.get("/users/me")
async def read_user_me():
return {"username": "fakecurrentuser"}


@router.get("/users/{username}")
async def read_user(username: str):
return {"username": username}
2 changes: 1 addition & 1 deletion modules/file_manager/cdlc_file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
import math

from definitions import TMP_DIR
from common.definitions import TMP_DIR
from utils import file_utils, collection_utils
from utils.collection_utils import repr_in_multi_line, is_not_empty

Expand Down
2 changes: 1 addition & 1 deletion modules/song_loader/song_data.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dataclasses import dataclass, field
from typing import Optional

from definitions import KEY_VALUES_OF_AN_OFFICIAL_CDLC
from common.definitions import KEY_VALUES_OF_AN_OFFICIAL_CDLC
from utils.string_utils import normalize


Expand Down
2 changes: 1 addition & 1 deletion modules/song_loader/song_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from dacite import from_dict

from config.config_data import ConfigData
from definitions import PSARC_INFO_FILE_CACHE_DIR, TMP_DIR
from common.definitions import PSARC_INFO_FILE_CACHE_DIR, TMP_DIR
from modules.database.db_manager import DBManager
from modules.song_loader.rs_playlist_data import RsPlaylist
from modules.song_loader.song_data import SongData, ArtistTitle
Expand Down
2 changes: 1 addition & 1 deletion modules/song_loader/song_loader_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from deepdiff import DeepDiff

from definitions import KEY_VALUES_OF_AN_OFFICIAL_CDLC
from common.definitions import KEY_VALUES_OF_AN_OFFICIAL_CDLC
from utils import collection_utils
from utils.exceptions import ConfigError

Expand Down
8 changes: 5 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ twine==5.1.0
ordered-set
zstandard==0.22.0
pycryptodome==3.20.0
dacite

fastapi==0.111.0
dacite~=1.8.1
uvicorn~=0.30.1
starlette~=0.37.2

pytest==8.2.2
pylint==3.2.3

fuzzywuzzy~=0.18.0
watchdog==4.0.1

fastapi==0.111.0
85 changes: 70 additions & 15 deletions run.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import asyncio
import logging
import os
import sys
import threading
from contextlib import asynccontextmanager
from time import sleep

import uvicorn
from fastapi import FastAPI

import config.log_config
from common.enums import Tags
from config.config_data import ConfigData
from config.config_reader import ConfigReader
from modules.api import users_api_example
from modules.database.db_manager import DBManager
from modules.file_manager.cdlc_file_manager import FileManager
from modules.scene_switcher.scene_switcher import SceneSwitcher
Expand Down Expand Up @@ -162,23 +169,71 @@ def update_game_info_and_setlist():
log.exception(ex)


manage_songs_thread = threading.Thread(target=manage_songs, args=(db_file_path,))
manage_songs_thread.daemon = True
manage_songs_thread.start()
async def run_servant():
manage_songs_thread = threading.Thread(target=manage_songs, args=(db_file_path,))
manage_songs_thread.daemon = True
manage_songs_thread.start()

update_game_info_and_setlist_thread = threading.Thread(target=update_game_info_and_setlist)
update_game_info_and_setlist_thread.daemon = True
update_game_info_and_setlist_thread.start()
update_game_info_and_setlist_thread = threading.Thread(target=update_game_info_and_setlist)
update_game_info_and_setlist_thread.daemon = True
update_game_info_and_setlist_thread.start()

while True:
while True:

try:
# Sleep a bit to avoid too fast processing
sleep(HEARTBEAT)
try:
# Sleep a bit to avoid too fast processing
await asyncio.sleep(HEARTBEAT)

update_config()
update_config()

# Catch all unchecked Exceptions, but keep app alive.
# pylint: disable=broad-exception-caught
except Exception as e:
log.exception(e)
# Catch all unchecked Exceptions, but keep app alive.
# pylint: disable=broad-exception-caught
except Exception as e:
log.exception(e)


@asynccontextmanager
async def lifespan(app: FastAPI):
asyncio.create_task(run_servant())
yield
# Add any logs or commands before shutting down.
print('It is shutting down...')


tags_metadata = [
{"name": Tags.USERS, "description": "Some user endpoint examples...fake as f"},
{"name": Tags.GETTERS, "description": "One other way around"},
{"name": "post methods", "description": "Keep doing this"},
{"name": "delete methods", "description": "KILL 'EM ALL"},
{"name": "put methods", "description": "Boring"},
]

app = FastAPI(lifespan=lifespan, openapi_tags=tags_metadata)
# app.add_middleware(
# CORSMiddleware,
# allow_origins=["*"],
# allow_credentials=True,
# allow_methods=["*"],
# allow_headers=["*"],
# )
app.include_router(users_api_example.router)


@app.get("/", tags=[Tags.GETTERS])
async def root():
return {"message": "Hello World"}


@app.get("/hello/{name}", tags=[Tags.GETTERS])
async def say_hello(name: str):
return {"message": f"Hello {name}"}


if __name__ == "__main__":
uvicorn.run(
"run:app",
host="127.0.0.1",
port=8000,
log_level="debug",
reload=True,
)
18 changes: 0 additions & 18 deletions run_configs/FastAPI Hello World example.run.xml

This file was deleted.

25 changes: 0 additions & 25 deletions run_configs/run DEV.run.xml

This file was deleted.

48 changes: 0 additions & 48 deletions run_configs/run.run.xml

This file was deleted.

2 changes: 1 addition & 1 deletion utils/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from datetime import datetime, timedelta
from typing import Tuple

from definitions import PATTERN_CDLC_FILE_EXT, PATTERN_CDLC_INFO_FILE_EXT, \
from common.definitions import PATTERN_CDLC_FILE_EXT, PATTERN_CDLC_INFO_FILE_EXT, \
EXT_PSARC_INFO_JSON
from utils.string_utils import is_not_blank

Expand Down
2 changes: 1 addition & 1 deletion utils/psarc_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from Crypto.Cipher import AES

from definitions import PSARC_INFO_FILE_CACHE_DIR, EXT_PSARC_INFO_JSON
from common.definitions import PSARC_INFO_FILE_CACHE_DIR, EXT_PSARC_INFO_JSON
from modules.song_loader.song_data import ArtistTitle, SongData
from utils import file_utils
from utils.exceptions import ExtractError
Expand Down

0 comments on commit 79d66bb

Please sign in to comment.