File size: 2,135 Bytes
d8b82c9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | #!/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'''
<html>
<head>
<title>Some HTML in here</title>
</head>
<body>
<h1>Welcome to SfaxWare</h1>
Current version is {API_VERSION}
</body>
</html>
'''
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()
|