Skip to content

Commit

Permalink
start refactor:
Browse files Browse the repository at this point in the history
- remove graphql stuff
- start to adopt new bbconf
- start new way to run with uvicorn for dev
  • Loading branch information
nsheff committed Aug 31, 2023
1 parent f6848ca commit 8c60b89
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 30 deletions.
1 change: 1 addition & 0 deletions bedhost/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from bbconf._version import __version__ as bbconf_v
from bbconf.const import *
from bbconf.const import CFG_REMOTE_KEY # nice to have this explicit, rather than *

from bedhost._version import __version__ as server_v

Expand Down
7 changes: 3 additions & 4 deletions bedhost/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def assert_table_columns_match(bbc, table_name, columns):
msg = f"Could not determine columns for table: {table_name}"
_LOGGER.warning(msg)
raise HTTPException(status_code=404, detail=msg)
diff = set(columns).difference(list(schema.keys()))
diff = set(columns).difference(list(schema.sample_level_data.keys()))
if diff:
msg = f"Columns not found in '{table_name}' table: {', '.join(diff)}"
_LOGGER.warning(msg)
Expand Down Expand Up @@ -357,7 +357,7 @@ def get_id_map(bbc, table_name, file_type):
id_map = {}

schema = serve_schema_for_table(bbc=bbc, table_name=table_name)
for key, value in schema.items():
for key, value in schema.sample_level_data.items():
if value["type"] == file_type:
id_map[value["label"]] = key

Expand All @@ -374,9 +374,8 @@ def get_enum_map(bbc, table_name, file_type):
"""

enum_map = {}

schema = serve_schema_for_table(bbc=bbc, table_name=table_name)
for key, value in schema.items():
for key, value in schema.sample_level_data.items():
if value["type"] == file_type:
enum_map[value["label"]] = value["label"]

Expand Down
46 changes: 28 additions & 18 deletions bedhost/main.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import sys
from logging import DEBUG, INFO
from typing import Dict, List, Optional

import bbconf
import logmuse
import logging
import sys
import uvicorn

from bbconf import BedBaseConf
from logging import DEBUG, INFO
from typing import Dict, List, Optional
from fastapi import FastAPI, HTTPException, Path, Query
from pipestat_reader import PipestatReader
from starlette_graphene3 import GraphQLApp, make_graphiql_handler
from starlette.middleware.cors import CORSMiddleware
from starlette.responses import FileResponse, RedirectResponse
from starlette.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware

from .const import *
from .data_models import DBResponse
from .helpers import *

global _LOGGER

_LOGGER = logging.getLogger(__name__)
app = FastAPI(
title=PKG_NAME,
description="BED file/sets statistics and image server API",
Expand Down Expand Up @@ -50,7 +47,7 @@ async def index():
"""
Display the index UI page
"""
return FileResponse(os.path.join(UI_PATH, "index.html"))
return FileResponse(os.path.join(STATIC_PATH, "index.html"))


def main():
Expand Down Expand Up @@ -98,18 +95,31 @@ def main():
else:
raise FileNotFoundError(f"React UI path to mount not found: {UI_PATH}")

app.mount("/ui", StaticFiles(directory=UI_PATH))
# app.mount("/ui", StaticFiles(directory=UI_PATH))


psr = PipestatReader(pipestat_managers=[bbc.bed, bbc.bedset, bbc.dist])
_LOGGER.info("Generating GraphQL schema")
graphql_schema = psr.generate_graphql_schema()
app.mount(
"/graphql", GraphQLApp(graphql_schema, on_get=make_graphiql_handler())
)

_LOGGER.info(f"running {PKG_NAME} app")
uvicorn.run(
app,
host=bbc.config[CFG_SERVER_KEY][CFG_HOST_KEY],
port=bbc.config[CFG_SERVER_KEY][CFG_PORT_KEY],
)


def register_globals(cfg):
global bbc
bbc = BedBaseConf(bbconf.get_bedbase_cfg(cfg))

if __name__ != "__main__":
if os.environ.get("BEDBASE_CONFIG"):
# Establish global config when running through uvicorn CLI
register_globals(os.environ.get("BEDBASE_CONFIG"))
from .routers import api, private_api

app.include_router(api.router, prefix="/api")
app.include_router(private_api.router, prefix="/_private_api")

else:
# Warn
_LOGGER.warning("No BEDBASE_CONFIG found. Can't configure server.")
16 changes: 8 additions & 8 deletions bedhost/routers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@

img_map_bed = get_id_map(bbc, BED_TABLE, "image")

ex_bed_digest = serve_columns_for_table(
bbc=bbc, table_name=BED_TABLE, columns=["md5sum"], limit=1
).get("data")[0][0]
# ex_bed_digest = serve_columns_for_table(
# bbc=bbc, table_name=BED_TABLE, columns=["md5sum"], limit=1
# ).get("data")[0][0]

ex_bedset_digest = serve_columns_for_table(
bbc=bbc, table_name=BEDSET_TABLE, columns=["md5sum"], limit=1
).get("data")[0][0]
# ex_bedset_digest = serve_columns_for_table(
# bbc=bbc, table_name=BEDSET_TABLE, columns=["md5sum"], limit=1
# ).get("data")[0][0]

ex_chr = "chr1"

Expand All @@ -63,7 +63,7 @@
regex=r"^\w+$",
max_length=32,
min_length=32,
example=ex_bed_digest,
# example=ex_bed_digest,
)

bsd = Path(
Expand All @@ -72,7 +72,7 @@
regex=r"^\w+$",
max_length=32,
min_length=32,
example=ex_bedset_digest,
# example=ex_bedset_digest,
)

c = Path(
Expand Down
1 change: 1 addition & 0 deletions bedhost/static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test.

0 comments on commit 8c60b89

Please sign in to comment.