-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
52 lines (42 loc) · 1.15 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
import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import Optional, List
from base64 import b64decode
from pyrawprint import send_raw_data_to_printer
from pyrawprint import get_printers
PORT=8219
class PrintRequest(BaseModel):
data: str
printer_name: Optional[str] = None
class PrintersResponse(BaseModel):
printers: List[str] = []
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.post("/print")
def send_to_printer(print_request: PrintRequest):
decoded_data = b64decode(print_request.data)
send_raw_data_to_printer(
decoded_data, print_request.printer_name
)
@app.get(
"/printers",
response_model=PrintersResponse
)
def printers():
printers = get_printers()
printers_response = PrintersResponse()
printers_response.printers = printers
return printers_response
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8219)