Initial commit with the basics
This commit is contained in:
93
app/main.py
Normal file
93
app/main.py
Normal file
@@ -0,0 +1,93 @@
|
||||
import logging
|
||||
|
||||
from datetime import datetime, time, timedelta
|
||||
from enum import Enum
|
||||
from fastapi import Body, Cookie, Depends, FastAPI, File, Form, Header, HTTPException, Path, Query, Request, status, UploadFile
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import HTMLResponse, JSONResponse, PlainTextResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from functools import lru_cache
|
||||
from pydantic import BaseModel, EmailStr, Field
|
||||
from typing import Dict, List, Optional, Set, Union
|
||||
|
||||
from sqlalchemy import create_engine, text
|
||||
from sqlalchemy.exc import IntegrityError, OperationalError
|
||||
|
||||
from . import config
|
||||
from .lib_general import *
|
||||
from .routers import items, users, websockets
|
||||
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@lru_cache()
|
||||
def get_settings():
|
||||
return config.Settings()
|
||||
|
||||
|
||||
app.mount('/static', StaticFiles(directory='static'), name='static')
|
||||
|
||||
|
||||
app.include_router(
|
||||
users.router,
|
||||
prefix='/user',
|
||||
tags=['Users'],
|
||||
#dependencies=[Depends(get_token_header)],
|
||||
#dependencies=[Depends(get_account_header)],
|
||||
#responses={404: {'description': 'Not found'}},
|
||||
)
|
||||
app.include_router(
|
||||
items.router,
|
||||
prefix='/item',
|
||||
tags=['Items'],
|
||||
#dependencies=[Depends(get_token_header)],
|
||||
#responses={404: {'description': 'Not found'}},
|
||||
)
|
||||
app.include_router(
|
||||
websockets.router,
|
||||
#prefix='/item',
|
||||
tags=['Websockets'],
|
||||
#dependencies=[Depends(get_token_header)],
|
||||
#responses={404: {'description': 'Not found'}},
|
||||
)
|
||||
|
||||
|
||||
# BEGIN: CORS
|
||||
origins = [
|
||||
'http://fastapi.localhost',
|
||||
'http://localhost',
|
||||
'http://localhost:5000',
|
||||
'http://fastapi.localhost:5000',
|
||||
'https://example.org',
|
||||
]
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=origins,
|
||||
allow_origin_regex='https://.*\.oneskyit\.com',
|
||||
allow_credentials=True,
|
||||
allow_methods=['*'],
|
||||
allow_headers=['*'],
|
||||
#expose_headers=[],
|
||||
#max_age=600,
|
||||
)
|
||||
# END: CORS
|
||||
|
||||
|
||||
#Add the processing time to the response header.
|
||||
@app.middleware('http')
|
||||
async def add_process_time_header(request: Request, call_next):
|
||||
import time
|
||||
start_time = time.time()
|
||||
response = await call_next(request)
|
||||
process_time = time.time() - start_time
|
||||
response.headers['X-Process-Time'] = str(process_time)
|
||||
return response
|
||||
|
||||
|
||||
@app.get('/', tags=['Default'])
|
||||
async def get_root():
|
||||
print(config.settings.APP_NAME)
|
||||
|
||||
return {'hello': 'This is the Aether API using FastAPI.'}
|
||||
Reference in New Issue
Block a user