Skip to content

Commit

Permalink
clean up config handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nsheff committed Oct 17, 2023
1 parent 7c58672 commit f6810dd
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 136 deletions.
11 changes: 2 additions & 9 deletions bedhost/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from bedhost import PKG_NAME
from bedhost._version import __version__
from . import PKG_NAME
from ._version import __version__
from ubiquerg import VersionInHelpParser
from yacman import select_config

Expand Down Expand Up @@ -47,11 +47,4 @@ def add_subparser(cmd, description):
f"the first available environment variable among: {', '.join(CFG_ENV_VARS)} will be used if set."
f" Currently: {env_var_val}",
)
sps[cmd].add_argument(
"-d",
"--dbg",
action="store_true",
dest="debug",
help="Set logger verbosity to debug",
)
return parser
2 changes: 1 addition & 1 deletion bedhost/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
CFG_SERVER_PORT_KEY,
)

from bedhost._version import __version__ as SERVER_VERSION
from ._version import __version__ as SERVER_VERSION

PKG_NAME = "bedhost"
LOG_FORMAT = "%(levelname)s in %(funcName)s: %(message)s"
Expand Down
14 changes: 10 additions & 4 deletions bedhost/data_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
from pydantic import BaseModel
from enum import Enum

from bedhost.const import CFG_REMOTE_KEY
from .const import CFG_REMOTE_KEY

# from bedhost.main import bbc
from bedhost.dependencies import get_bbconf
# from bedhost.dependencies import get_bbconf

bbc = get_bbconf()
# bbc = get_bbconf()

# from .main import bbc

class DBResponse(BaseModel):
"""
Expand All @@ -20,9 +21,14 @@ class DBResponse(BaseModel):
data: Union[List[List], List[Dict], Tuple, Dict]


# RemoteClassEnum = Enum(
# "RemoteClassEnum",
# {r: r for r in bbc.config[CFG_REMOTE_KEY]} if bbc.is_remote else {"http": "http"},
# )

RemoteClassEnum = Enum(
"RemoteClassEnum",
{r: r for r in bbc.config[CFG_REMOTE_KEY]} if bbc.is_remote else {"http": "http"},
{"http": "http"},
)

BedsetDigest = Path(
Expand Down
14 changes: 0 additions & 14 deletions bedhost/dependencies.py
Original file line number Diff line number Diff line change
@@ -1,14 +0,0 @@
from bedhost.helpers import BedHostConf
import os
from bedhost.exceptions import BedHostException

bb_conf_file_path = os.environ.get("BEDBASE_CONFIG") or None

try:
bedbase_conf = BedHostConf(bb_conf_file_path)
except Exception as e:
raise BedHostException(f"Bedbase config was not provided or is incorrect: {e}")


def get_bbconf() -> BedHostConf:
return bedbase_conf
55 changes: 48 additions & 7 deletions bedhost/helpers.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import os
from urllib import parse
from typing import List, Union

from bbconf import BedBaseConf
from starlette.responses import FileResponse, RedirectResponse
from typing import List, Union
from urllib import parse

from bedhost import _LOGGER

from bedhost.const import (
from . import _LOGGER
from .const import (
CFG_PATH_KEY,
CFG_PATH_PIPELINE_OUTPUT_KEY,
CFG_REMOTE_KEY,
TYPES_MAPPING,
VALIDATIONS_MAPPING,
OPERATORS_MAPPING,
)
from bedhost.exceptions import IncorrectSchemaException

from .exceptions import BedHostException

class BedHostConf(BedBaseConf):
"""
Expand Down Expand Up @@ -194,6 +195,46 @@ def get_openapi_version(app):
return "3.0.2"


def attach_routers(app):
_LOGGER.info("Mounting routers...")
from .routers import bed_api, bedset_api, base, search_api
app.include_router(base.router)
app.include_router(bed_api.router)
app.include_router(bedset_api.router)
app.include_router(search_api.search_router)
return app


def configure(bbconf_file_path):
try:
# bbconf_file_path = os.environ.get("BEDBASE_CONFIG") or None
_LOGGER.info(f"Loading config...{bbconf_file_path}")
bbc = BedHostConf(bbconf_file_path)
except Exception as e:
raise BedHostException(f"Bedbase config was not provided or is incorrect: {e}")

if not CFG_REMOTE_KEY in bbc.config:
_LOGGER.debug(
f"Using local files for serving: "
f"{bbc.config[CFG_PATH_KEY][CFG_PATH_PIPELINE_OUTPUT_KEY]}"
)
app.mount(
bbc.get_bedstat_output_path(),
StaticFiles(directory=bbc.get_bedstat_output_path()),
name="bedfile",
)
app.mount(
bbc.get_bedbuncher_output_path(),
StaticFiles(directory=bbc.get_bedbuncher_output_path()),
name="bedset",
)
else:
_LOGGER.debug(
f"Using remote files for serving. Prefix: {bbc.config[CFG_REMOTE_KEY]['http']['prefix']}"
)
return bbc


# def get_id_map(bbc, table_name, file_type):
# """
# Get a dict for avalible file/figure ids
Expand Down
93 changes: 25 additions & 68 deletions bedhost/main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import coloredlogs
import logging
import sys
import os

import coloredlogs
import sys
import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles

from bedhost import _LOGGER
from bedhost.cli import build_parser
from bedhost.const import (
from . import _LOGGER
import bedhost.dependencies as dependencies
from .helpers import FileResponse, configure, attach_routers

from .cli import build_parser
from .const import (
CFG_PATH_KEY,
CFG_PATH_PIPELINE_OUTPUT_KEY,
CFG_REMOTE_KEY,
Expand All @@ -21,30 +20,9 @@
STATIC_PATH,
SERVER_VERSION,
)

from bedhost.helpers import FileResponse

from bedhost.dependencies import get_bbconf
from bedhost.routers import bed_api, bedset_api, base, search_api


_LOGGER_UVICORN = logging.getLogger("uvicorn.access")
coloredlogs.install(
logger=_LOGGER_UVICORN,
level=logging.INFO,
datefmt="%b %d %Y %H:%M:%S",
fmt="[%(levelname)s] [%(asctime)s] [BEDHOST] %(message)s",
)


_LOGGER_BEDHOST = logging.getLogger("bedhost")
coloredlogs.install(
logger=_LOGGER_BEDHOST,
level=logging.INFO,
datefmt="%b %d %Y %H:%M:%S",
fmt="[%(levelname)s] [%(asctime)s] [BEDHOST] %(message)s",
)

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles

app = FastAPI(
title=PKG_NAME,
Expand All @@ -58,10 +36,9 @@
"http://localhost:8000",
"http://localhost:5173",
"https://bedbase.org",
"*",
"*", # allow cross origin resource sharing, since this is a public API
]

# uncomment below for development, to allow cross origin resource sharing
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
Expand All @@ -78,37 +55,12 @@ async def index():
"""
return FileResponse(os.path.join(STATIC_PATH, "index.html"))


def attach_routers(app):
_LOGGER.debug("Mounting routers")

app.include_router(base.router)
app.include_router(bed_api.router)
app.include_router(bedset_api.router)
app.include_router(search_api.search_router)

bbc = get_bbconf()

if not CFG_REMOTE_KEY in bbc.config:
_LOGGER.debug(
f"Using local files for serving: "
f"{bbc.config[CFG_PATH_KEY][CFG_PATH_PIPELINE_OUTPUT_KEY]}"
)
app.mount(
bbc.get_bedstat_output_path(),
StaticFiles(directory=bbc.get_bedstat_output_path()),
name="bedfile",
)
app.mount(
bbc.get_bedbuncher_output_path(),
StaticFiles(directory=bbc.get_bedbuncher_output_path()),
name="bedset",
)
else:
_LOGGER.debug(
f"Using remote files for serving. Prefix: {bbc.config[CFG_REMOTE_KEY]['http']['prefix']}"
)

@app.get("/test", response_model=int)
async def get_bedfile_count():
"""
Returns the number of bedfiles available in the database
"""
return int(bbc['conf'].bed.record_count)

def main():
parser = build_parser()
Expand All @@ -119,9 +71,11 @@ def main():
sys.exit(1)

if args.command == "serve":
attach_routers(app)
_LOGGER.info(f"Running {PKG_NAME} app...")
bbc = get_bbconf()
bbconf_file_path = args.config or os.environ.get("BEDBASE_CONFIG") or None
global bbc
bbc = configure(bbconf_file_path)
attach_routers(app)
uvicorn.run(
app,
host=bbc.config[CFG_SERVER_KEY][CFG_SERVER_HOST_KEY],
Expand All @@ -131,6 +85,9 @@ def main():

if __name__ != "__main__":
if os.environ.get("BEDBASE_CONFIG"):
bbconf_file_path = os.environ.get("BEDBASE_CONFIG") or None
global bbc
bbc = configure(bbconf_file_path)
attach_routers(app)
else:
raise EnvironmentError(
Expand Down
15 changes: 7 additions & 8 deletions bedhost/routers/base.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import os

from fastapi import APIRouter
from fastapi.responses import FileResponse
from typing import Dict

from bedhost.const import (

from ..const import (
STATIC_PATH,
ALL_VERSIONS,
)
from fastapi import APIRouter
from fastapi.responses import FileResponse
from bedhost.main import _LOGGER
from bedhost.helpers import get_openapi_version
from bedhost.dependencies import get_bbconf

bbc = get_bbconf()
from ..helpers import get_openapi_version
from .. import _LOGGER
from ..main import bbc

router = APIRouter(prefix="/api", tags=["base"])

Expand Down
27 changes: 15 additions & 12 deletions bedhost/routers/bed_api.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
import subprocess
import os
from typing import Annotated, Dict, Optional, List
try:
from typing import Annotated, Dict, Optional, List
except:
from typing_extensions import Annotated
from typing import Dict, Optional, List

import tempfile

from fastapi import APIRouter, HTTPException, Query, Response, Path, Depends
from fastapi.responses import PlainTextResponse, StreamingResponse
from pipestat.exceptions import RecordNotFoundError

from bedhost.main import _LOGGER

from bedhost.data_models import (
DBResponse,
RemoteClassEnum,
BedDigest,
chromosome_number,
)
from bedhost.const import (
from .. import _LOGGER
from ..main import bbc
from ..const import (
CFG_PATH_PIPELINE_OUTPUT_KEY,
CFG_REMOTE_KEY,
CFG_PATH_KEY,
FIG_FORMAT,
)
from bedhost.dependencies import get_bbconf
from ..data_models import (
DBResponse,
RemoteClassEnum,
BedDigest,
chromosome_number,
)


router = APIRouter(prefix="/api/bed", tags=["bed"])
bbc = get_bbconf()


@router.get("/genomes")
Expand Down
12 changes: 4 additions & 8 deletions bedhost/routers/bedset_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@
import os


from bedhost import _LOGGER
from bedhost.const import (
from .. import _LOGGER
from ..const import (
CFG_REMOTE_KEY,
CFG_PATH_KEY,
CFG_PATH_PIPELINE_OUTPUT_KEY,
FIG_FORMAT,
)

# from bedhost.helpers import
from bedhost.data_models import DBResponse, RemoteClassEnum, BedsetDigest, BedList
from bedhost.dependencies import get_bbconf

bbc = get_bbconf()
from ..data_models import DBResponse, RemoteClassEnum, BedsetDigest, BedList
from ..main import bbc

router = APIRouter(prefix="/api/bedset", tags=["bedset"])

Expand Down
Loading

0 comments on commit f6810dd

Please sign in to comment.