forked from BrewBlox/brewblox-history
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
proof of concept fastapi scaffolding
- Loading branch information
Showing
14 changed files
with
1,030 additions
and
1,073 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,3 +75,4 @@ victoria/* | |
|
||
redis/* | ||
!redis/.gitkeep | ||
.appenv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
from contextlib import AsyncExitStack, asynccontextmanager | ||
from pprint import pformat | ||
|
||
from fastapi import FastAPI | ||
|
||
from . import datastore_api, redis, settings | ||
|
||
LOGGER = settings.brewblox_logger(__name__) | ||
|
||
|
||
@asynccontextmanager | ||
async def lifespan(app: FastAPI): | ||
LOGGER.info(settings.get_config()) | ||
LOGGER.debug('\n' + pformat(app.routes)) | ||
async with AsyncExitStack() as stack: | ||
await stack.enter_async_context(redis.lifespan()) | ||
yield | ||
|
||
|
||
def create_app(): | ||
settings.get_config.cache_clear() | ||
settings.init_logging() | ||
config = settings.get_config() | ||
|
||
redis.client.set(redis.RedisClient()) | ||
|
||
prefix = f'/{config.name}' | ||
app = FastAPI(lifespan=lifespan, | ||
docs_url=f'{prefix}/api/doc', | ||
redoc_url=f'{prefix}/api/redoc', | ||
openapi_url=f'{prefix}/openapi.json') | ||
|
||
app.include_router(datastore_api.router, prefix=prefix) | ||
|
||
return app | ||
|
||
# def create_parser(): | ||
# parser = service.create_parser('history') | ||
# parser.add_argument('--ranges-interval', | ||
# help='Interval (sec) between updates in live ranges. [%(default)s]', | ||
# default=10, | ||
# type=float) | ||
# parser.add_argument('--metrics-interval', | ||
# help='Interval (sec) between updates in live metrics. [%(default)s]', | ||
# default=5, | ||
# type=float) | ||
# parser.add_argument('--redis-url', | ||
# help='URL for the Redis database', | ||
# default='redis://redis') | ||
# parser.add_argument('--victoria-url', | ||
# help='URL for the Victoria Metrics database', | ||
# default='http://victoria:8428/victoria') | ||
# parser.add_argument('--datastore-topic', | ||
# help='Synchronization topic for datastore updates', | ||
# default='brewcast/datastore') | ||
# parser.add_argument('--minimum-step', | ||
# help='Minimum period (sec) for range data downsampling', | ||
# default=10, | ||
# type=float) | ||
# return parser | ||
|
||
|
||
# def main(): | ||
# parser = create_parser() | ||
# config = service.create_config(parser, model=ServiceConfig) | ||
# app = service.create_app(config) | ||
|
||
# async def setup(): | ||
# scheduler.setup(app) | ||
# http.setup(app) | ||
# mqtt.setup(app) | ||
# socket_closer.setup(app) | ||
# victoria.setup(app) | ||
# timeseries_api.setup(app) | ||
# redis.setup(app) | ||
# datastore_api.setup(app) | ||
# relays.setup(app) | ||
# error_response.setup(app) | ||
|
||
# service.run_app(app, setup()) | ||
|
||
|
||
# if __name__ == '__main__': | ||
# main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.