-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
58 lines (41 loc) · 1.5 KB
/
main.py
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
import os
from fastapi import FastAPI, Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer
from fastapi.responses import PlainTextResponse
import uvicorn
from mal import refresh_token
import json
from dotenv import load_dotenv
import logging
import platform
load_dotenv()
app = FastAPI()
@app.get("/")
async def root():
return {"message": "MAL Token Manager"}
@app.get("/mal/redirect", response_class=PlainTextResponse)
async def read_item(code):
return code
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
def get_current_user(token: str = Depends(oauth2_scheme)):
if token != os.environ['APP_TOKEN']:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid authentication credentials",
headers={"WWW-Authenticate": "Bearer"},
)
return token
@app.post("/mal/token/refresh")
def refresh_mal_token(token: str = Depends(get_current_user)):
token = refresh_token()
return {'access_token': token['access_token'], 'expires_at': token['expires_at'], 'expires_at_h': token['expires_at_h']}
SERVER_PORT = os.getenv('SERVER_PORT_GUEST', 7777)
@app.on_event("startup")
async def startup_event():
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("uvicorn")
logger.info(f'Running on: {platform.system()}')
logger.info(
f"http://localhost:{SERVER_PORT} (Windows)")
if __name__ == '__main__':
uvicorn.run("main:app", host="0.0.0.0", port=int(SERVER_PORT))