-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
46 lines (30 loc) · 1.04 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
import os
# import netifaces
import uvicorn
from fastapi import FastAPI
from fastapi.exceptions import HTTPException
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from service import ArrowControlAdapter, get_ip
app = FastAPI()
@app.get("/")
def index():
return FileResponse("templates/control_page.html", media_type="text/html")
@app.get("/favicon.ico")
def favicon():
return FileResponse("templates/favicon.ico")
ACTIONS_ENABLED = ["up", "down", "left", "right"]
@app.get("/{cmd}")
def get_cmd(cmd):
if cmd not in ACTIONS_ENABLED:
raise HTTPException(status_code=400, detail="No such command available")
ArrowControlAdapter.click_keyb(cmd)
if __name__ == "__main__":
cwd = os.path.dirname(os.path.abspath(__file__))
app.mount("/templates", StaticFiles(directory=os.path.join(cwd, "templates")))
host = "0.0.0.0"
port = 8000
print("Try to connect to this ip addr in your phone:")
ip = f"{get_ip()}:{port}"
print(ip)
uvicorn.run(app, host=host, port=port)