#!/usr/bin/env python3 # vim: set fileencoding=utf-8 from fastapi import FastAPI from fastapi.middleware import Middleware from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import HTMLResponse from logging import getLogger from os import environ from os.path import realpath, sep, splitext from sfxpkg import rootDir from sfxpkg.version import API_VERSION # Create logger for this module log = getLogger(__name__) # Instantiate REST application with some basic configuration app = FastAPI( title='Welcome to SfaxWare space at Hugging Face', version=API_VERSION, #description=__doc__, openapi_url=None, docs_url=None, redoc_url=None, middleware=[ # CORS origins accepted patterns Middleware( CORSMiddleware, allow_origin_regex=r'https://.+\.(q-ai\.org|q-ai\.fr|sfaxware\.com)(:(443|4200))?', allow_methods=["*"], allow_headers=["*"], allow_credentials=True, ) ] ) @app.get("/", response_class=HTMLResponse) async def root(): response = f''' Some HTML in here

Welcome to SfaxWare

Current version is {API_VERSION} ''' return response # Run application using uvicorn server if started as a python executable script if __name__ == '__main__': from uvicorn import main from uvicorn.config import LOGGING_CONFIG # Log configuration LOGGING_CONFIG['formatters']['access']['fmt'] = '%(asctime)s %(levelprefix)s %(client_addr)s - "%(request_line)s" %(status_code)s' LOGGING_CONFIG['formatters']['default']['fmt'] = '%(asctime)s %(levelprefix)s %(name)s | %(message)s' if environ.get('LOG_PROFILE', 'prod') == 'prod': LOGGING_CONFIG['handlers']['default'] = { 'formatter': 'default', 'class': 'logging.handlers.TimedRotatingFileHandler', 'filename': '/tmp/fastapi-readme.log', 'when': 'midnight', 'utc': True, } LOGGING_CONFIG['loggers']['sfxpkg'] = { 'handlers': ['default'], 'level': 'INFO', 'propagate': True } pkgPath = splitext(realpath(__file__))[0] pkgName = pkgPath.replace(rootDir, 'sfxpkg').replace(sep, '.') main.params[0].default = pkgName + ':app' main()