-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
79 lines (56 loc) · 2.33 KB
/
app.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from fastapi import FastAPI, BackgroundTasks, Depends, status
from fastapi.exceptions import HTTPException
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from fastapi.responses import JSONResponse
import threading
from pydantic import BaseModel
from job_bot import JobBot as Bot
import bot_config as config
from job_data import load_job_count
app = FastAPI()
bot = None
bot_thread = None
oauth2_scheme = OAuth2PasswordBearer(tokenUrl='token')
class Token(BaseModel):
access_token: str
token_type: str
def authenticate_user(username: str, password: str):
if username == config.USERNAME and password == config.PASSWORD:
return True
return False
@app.post('/token', response_model=Token)
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
if authenticate_user(form_data.username, form_data.password):
return {'access_token': form_data.username, 'token_type': 'bearer'}
else:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, detail='Invalid credentials')
@app.post('/start_bot/')
def start_bot(background_tasks: BackgroundTasks, token: str = Depends(oauth2_scheme)):
global bot, bot_thread
if bot_thread and bot_thread.is_alive():
raise HTTPException(status_code=400, detail='Bot already running')
bot = Bot(config.__dict__)
bot_thread = threading.Thread(target=bot.run)
bot_thread.start()
background_tasks.add_task(bot_thread.join)
return JSONResponse(status_code=200, content={'message': 'Bot started'})
@app.post('/stop_bot/')
def stop_bot(token: str = Depends(oauth2_scheme)):
global bot_thread
if not bot_thread or not bot_thread.is_alive():
return JSONResponse(status_code=200, content={'message': 'Bot not running, nothing to stop'})
bot.stop()
bot_thread.join()
return JSONResponse(status_code=200, content={'message': 'Bot stopped'})
@app.get('/status/')
def get_status(token: str = Depends(oauth2_scheme)):
job_count = load_job_count(config.JOB_COUNT_FILE)
if bot_thread and bot_thread.is_alive():
status = 'Running'
else:
status = 'Stopped'
return JSONResponse(status_code=200, content={'status': status, 'job_count': job_count})
if __name__ == '__main__':
import uvicorn
uvicorn.run("app:app", host='0.0.0.0', port=8001, reload=True)