mirror of
https://github.com/hay-kot/mealie.git
synced 2025-08-22 06:23:34 -07:00
refactor ap scheduler and startup process
This commit is contained in:
parent
f87e14c083
commit
534af5a421
12 changed files with 114 additions and 34 deletions
2
.github/workflows/build-docs.yml
vendored
2
.github/workflows/build-docs.yml
vendored
|
@ -2,7 +2,7 @@ name: Publish docs via GitHub Pages
|
|||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
- master
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
|
1
.github/workflows/pytest.yml
vendored
1
.github/workflows/pytest.yml
vendored
|
@ -4,7 +4,6 @@ on:
|
|||
branches:
|
||||
- master
|
||||
- dev
|
||||
- sqlite
|
||||
pull_request:
|
||||
branches:
|
||||
- master
|
||||
|
|
|
@ -14,12 +14,10 @@ from routes import (
|
|||
static_routes,
|
||||
user_routes,
|
||||
)
|
||||
from routes.setting_routes import scheduler # ! This has to be imported for scheduling
|
||||
from settings import PORT, PRODUCTION, docs_url, redoc_url
|
||||
from settings import PORT, PRODUCTION, WEB_PATH, docs_url, redoc_url
|
||||
from utils.logger import logger
|
||||
|
||||
CWD = Path(__file__).parent
|
||||
WEB_PATH = CWD.joinpath("dist")
|
||||
startup.pre_start()
|
||||
|
||||
app = FastAPI(
|
||||
title="Mealie",
|
||||
|
@ -33,7 +31,6 @@ app = FastAPI(
|
|||
if PRODUCTION:
|
||||
app.mount("/static", StaticFiles(directory=WEB_PATH, html=True))
|
||||
|
||||
|
||||
# API Routes
|
||||
app.include_router(recipe_routes.router)
|
||||
app.include_router(meal_routes.router)
|
||||
|
@ -50,8 +47,6 @@ def invalid_api():
|
|||
|
||||
app.include_router(static_routes.router)
|
||||
|
||||
startup.ensure_dirs()
|
||||
startup.generate_default_theme()
|
||||
|
||||
# Generate API Documentation
|
||||
if not PRODUCTION:
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import mongoengine
|
||||
from settings import DB_HOST, DB_PASSWORD, DB_PORT, DB_USERNAME, MEALIE_DB_NAME
|
||||
from utils.logger import logger
|
||||
|
||||
|
||||
def global_init():
|
||||
|
@ -12,3 +13,5 @@ def global_init():
|
|||
password=DB_PASSWORD,
|
||||
authentication_source="admin",
|
||||
)
|
||||
|
||||
logger.info("Mongo Data Initialized")
|
||||
|
|
26
mealie/db/sql/db_session.py
Normal file
26
mealie/db/sql/db_session.py
Normal file
|
@ -0,0 +1,26 @@
|
|||
from pathlib import Path
|
||||
|
||||
import sqlalchemy as sa
|
||||
import sqlalchemy.orm as orm
|
||||
from settings import SQLITE
|
||||
|
||||
|
||||
factory = None
|
||||
|
||||
def global_init(db_file: Path):
|
||||
if not SQLITE:
|
||||
pass
|
||||
|
||||
global factory
|
||||
|
||||
if factory:
|
||||
return
|
||||
|
||||
if not db_file or not db_file.strip:
|
||||
raise Exception("You must Specif a db file")
|
||||
|
||||
conn_str = "sqlite:///" + db_file.absolute()
|
||||
|
||||
engine = sa.create_engine(conn_str, echo=False)
|
||||
|
||||
factory = orm.sessionmaker(bind=engine)
|
4
mealie/db/sql/model_base.py
Normal file
4
mealie/db/sql/model_base.py
Normal file
|
@ -0,0 +1,4 @@
|
|||
import sqlalchemy.ext.declarative as dec
|
||||
|
||||
|
||||
SqlAlchemyBase = dec.declarative_base()
|
|
@ -1 +1,28 @@
|
|||
2
|
||||
import mongoengine
|
||||
import sqlalchemy as sa
|
||||
from db.sql.model_base import SqlAlchemyBase
|
||||
|
||||
|
||||
class RecipeSQLModel(SqlAlchemyBase):
|
||||
__tablename__ = "recipes"
|
||||
|
||||
name = sa.Column(sa.String, primar_key=True)
|
||||
description = sa.Column(sa.String)
|
||||
image = sa.Column(sa.String)
|
||||
recipeYield = sa.Column(sa.String)
|
||||
recipeIngredient = mongoengine.ListField(required=True, default=[])
|
||||
recipeInstructions = mongoengine.ListField(requiredd=True, default=[])
|
||||
totalTime = sa.Column(sa.String)
|
||||
|
||||
# Mealie Specific
|
||||
slug = sa.Column(sa.String)
|
||||
categories = mongoengine.ListField(default=[])
|
||||
tags = mongoengine.ListField(default=[])
|
||||
dateAdded = mongoengine.DateTimeField(binary=True, default=datetime.date.today())
|
||||
notes = mongoengine.ListField(default=[])
|
||||
rating = sa.Column(sa.Integer)
|
||||
orgURL = sa.Column(sa.String)
|
||||
extras = mongoengine.DictField(required=False)
|
||||
|
||||
def __repr__(self):
|
||||
return f"SQL Entry Recipe {self.name}"
|
||||
|
|
|
@ -1,16 +1,10 @@
|
|||
from typing import List
|
||||
|
||||
from db.mongo_setup import global_init
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from services.scheduler_services import Scheduler, post_webhooks
|
||||
from services.scheduler_services import post_webhooks
|
||||
from services.settings_services import SiteSettings, SiteTheme
|
||||
from startup import scheduler
|
||||
from utils.snackbar import SnackResponse
|
||||
|
||||
router = APIRouter()
|
||||
global_init()
|
||||
|
||||
scheduler = Scheduler()
|
||||
scheduler.startup_scheduler()
|
||||
|
||||
|
||||
@router.get("/api/site-settings/", tags=["Settings"])
|
||||
|
@ -42,18 +36,14 @@ async def update_settings(data: SiteSettings):
|
|||
return SnackResponse.success("Settings Updated")
|
||||
|
||||
|
||||
@router.get(
|
||||
"/api/site-settings/themes/", tags=["Themes"]
|
||||
)
|
||||
@router.get("/api/site-settings/themes/", tags=["Themes"])
|
||||
async def get_all_themes():
|
||||
""" Returns all site themes """
|
||||
|
||||
return SiteTheme.get_all()
|
||||
|
||||
|
||||
@router.get(
|
||||
"/api/site-settings/themes/{theme_name}/", tags=["Themes"]
|
||||
)
|
||||
@router.get("/api/site-settings/themes/{theme_name}/", tags=["Themes"])
|
||||
async def get_single_theme(theme_name: str):
|
||||
""" Returns a named theme """
|
||||
return SiteTheme.get_by_name(theme_name)
|
||||
|
|
|
@ -3,9 +3,15 @@ from pathlib import Path
|
|||
|
||||
import dotenv
|
||||
|
||||
# Helpful Globas
|
||||
CWD = Path(__file__).parent
|
||||
|
||||
# Register ENV
|
||||
ENV = CWD.joinpath(".env")
|
||||
dotenv.load_dotenv(ENV)
|
||||
|
||||
# Helpful Globals
|
||||
DATA_DIR = CWD.joinpath("data")
|
||||
WEB_PATH = CWD.joinpath("dist")
|
||||
IMG_DIR = DATA_DIR.joinpath("img")
|
||||
BACKUP_DIR = DATA_DIR.joinpath("backups")
|
||||
DEBUG_DIR = DATA_DIR.joinpath("debug")
|
||||
|
@ -14,10 +20,6 @@ TEMPLATE_DIR = DATA_DIR.joinpath("templates")
|
|||
TEMP_DIR = DATA_DIR.joinpath("temp")
|
||||
|
||||
|
||||
# Env Variables
|
||||
ENV = CWD.joinpath(".env")
|
||||
dotenv.load_dotenv(ENV)
|
||||
|
||||
# General
|
||||
PRODUCTION = os.environ.get("ENV")
|
||||
PORT = int(os.getenv("mealie_port", 9000))
|
||||
|
@ -30,6 +32,21 @@ else:
|
|||
docs_url = None
|
||||
redoc_url = None
|
||||
|
||||
|
||||
# DATABASE ENV
|
||||
DATABASE_TYPE = os.getenv("db_type", "mongo") # mongo, sqlite
|
||||
SQLITE = False
|
||||
MONGO = False
|
||||
if DATABASE_TYPE == "sqlite":
|
||||
SQLITE = True
|
||||
SQLITE_DB_FILE = DATA_DIR.joinpath("mealie.sqlite")
|
||||
elif DATABASE_TYPE == "mongo":
|
||||
MONGO = True
|
||||
else:
|
||||
raise Exception(
|
||||
"Unable to determine database type. Acceptible options are 'mongo' or 'sqlite' "
|
||||
)
|
||||
|
||||
# Mongo Database
|
||||
MEALIE_DB_NAME = os.getenv("mealie_db_name", "mealie")
|
||||
DB_USERNAME = os.getenv("db_username", "root")
|
||||
|
@ -37,6 +54,6 @@ DB_PASSWORD = os.getenv("db_password", "example")
|
|||
DB_HOST = os.getenv("db_host", "mongo")
|
||||
DB_PORT = os.getenv("db_port", 27017)
|
||||
|
||||
# SFTP Email Stuff
|
||||
# SFTP Email Stuff - For use Later down the line!
|
||||
SFTP_USERNAME = os.getenv("sftp_username", None)
|
||||
SFTP_PASSWORD = os.getenv("sftp_password", None)
|
||||
|
|
|
@ -1,12 +1,32 @@
|
|||
import json
|
||||
from pathlib import Path
|
||||
|
||||
from db.mongo_setup import global_init as mongo_global_init
|
||||
from db.sql.db_session import global_init as sql_global_init
|
||||
from services.scheduler_services import Scheduler
|
||||
from services.settings_services import Colors, SiteTheme
|
||||
from settings import DATA_DIR, MONGO, SQLITE
|
||||
from utils.logger import logger
|
||||
|
||||
CWD = Path(__file__).parent
|
||||
DATA_DIR = CWD.joinpath("data")
|
||||
TEMP_DIR = CWD.joinpath("data", "temp")
|
||||
|
||||
scheduler = None
|
||||
|
||||
|
||||
def pre_start():
|
||||
if SQLITE:
|
||||
from settings import SQLITE_DB_FILE
|
||||
|
||||
sql_global_init(SQLITE_DB_FILE)
|
||||
elif MONGO:
|
||||
mongo_global_init()
|
||||
|
||||
global scheduler
|
||||
scheduler = Scheduler()
|
||||
scheduler.startup_scheduler()
|
||||
|
||||
ensure_dirs()
|
||||
generate_default_theme()
|
||||
|
||||
|
||||
def ensure_dirs():
|
||||
|
@ -67,7 +87,6 @@ HTML_TEMPLATE = """<!DOCTYPE html>
|
|||
</html>
|
||||
"""
|
||||
|
||||
CWD = Path(__file__).parent
|
||||
out_path = CWD.joinpath("temp", "index.html")
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue