main.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. from sqlalchemy import null
  2. from starlette.types import Message
  3. from fastapi import FastAPI, Request, Depends
  4. from starlette.middleware.cors import CORSMiddleware
  5. import base64
  6. import json
  7. import dataset
  8. from datetime import datetime
  9. # from starlette.middleware.authentication import AuthenticationMiddleware
  10. from app.crud.crud_user import user
  11. from app.api.api_v1.api import api_router
  12. from app.core.config import settings
  13. from app.api import deps
  14. from sqlalchemy.orm import Session
  15. app = FastAPI(
  16. title=settings.PROJECT_NAME, openapi_url=f"{settings.API_V1_STR}/openapi.json")
  17. # Set all CORS enabled origins
  18. # allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS],
  19. if settings.BACKEND_CORS_ORIGINS:
  20. app.add_middleware(
  21. CORSMiddleware,
  22. # AuthenticationMiddleware,
  23. allow_origins=["*"],
  24. allow_credentials=True,
  25. allow_methods=["*"],
  26. allow_headers=["*"],
  27. )
  28. # async def set_body(request: Request, body: bytes):
  29. # async def receive() -> Message:
  30. # return {"type": "http.request", "body": body}
  31. # request._receive = receive
  32. # async def get_body(request: Request) -> bytes:
  33. # body = await request.body()
  34. # await set_body(request, body)
  35. # return body
  36. # @app.middleware("http")
  37. # async def app_entry(request: Request, call_next,
  38. # ):
  39. # db = dataset.connect(
  40. # 'mysql://choozmo:pAssw0rd@db.ptt.cx:3306/arkcard?charset=utf8mb4'
  41. # )
  42. # await set_body(request, await request.body())
  43. # headers = str(request.headers).replace("\"","\'")
  44. # try:
  45. # userid = int(json.loads(base64.b64decode(request.headers['authorization'][7:].split('.')[1]).decode('utf-8'))['sub'])
  46. # except:
  47. # if( "/api/v1/line/collection/" or "/api/v1/line/receive/" or "/api/v1/line/shop/" in request.url.path):
  48. # lineid = request.url.path.split("/")[-1]
  49. # sql = 'SELECT id, userid FROM users where userid =\'' + lineid + '\''
  50. # try:
  51. # userid = int(db.query(sql).next()['id'])
  52. # except:
  53. # userid = -1
  54. # db.query('Insert into log (create_time, url_path, headers, userid) VALUE (\'' + str(datetime.now()) + '\' ,\'' + request.url.path + '\' , \"' + headers + '\" , '+ str(userid) + ')')
  55. # response = await call_next(request)
  56. # return response
  57. app.include_router(api_router, prefix=settings.API_V1_STR)