Skip to content

Commit 932ed13

Browse files
committed
adding a fastapi server, Dockerfile
1 parent 63bc391 commit 932ed13

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM python:3.12
2+
3+
COPY . .
4+
5+
RUN pip3 install --upgrade pip
6+
RUN pip3 install fastapi[standard] uvicorn[standard]
7+
8+
RUN export PYTHONPATH="."
9+
10+
RUN python3 parser.py test_files_decoder/x1.txt
11+
RUN python3 encoder.py test_files_encoder/x1_python.txt
12+
13+
EXPOSE 8080
14+
CMD ["python3", "main.py"]

__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__package__ = "torx_parser"

encoder.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,20 @@ async def tests():
560560
# print("output:", encoded_grim)
561561

562562

563+
class BencoderAPI:
564+
"""
565+
API for encoding data into bencoding format.
566+
"""
567+
@staticmethod
568+
async def encode(data: str) -> str:
569+
"""
570+
Create a BEncoderOperation object and encode the data.
571+
"""
572+
encoder = BEncoderOperation()
573+
encoded = await encoder.encode(data)
574+
return str(encoded)
575+
576+
563577
async def task_run(filename):
564578
"""
565579
Task to read data from a file.

main.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import uvicorn
2+
3+
from encoder import BencoderAPI
4+
from parser import BdecoderAPI
5+
6+
from fastapi import FastAPI, HTTPException
7+
8+
app = FastAPI(title="Bencoder and Bdecoder API")
9+
10+
@app.get("/")
11+
async def root():
12+
return {"message": "Hello World"}
13+
14+
@app.get("/encode_file/")
15+
async def encode():
16+
encoded_content = "none"
17+
file_path = "./test_files_encoder/x1_python.txt"
18+
try:
19+
with open(file_path, "r") as buffer:
20+
content = buffer.read()
21+
encoder = BencoderAPI()
22+
encoded_content = await encoder.encode(content)
23+
except Exception as e:
24+
print(str(e))
25+
raise HTTPException(status_code=500, detail=str(e))
26+
27+
return {"encoded_content": content}
28+
29+
@app.get("/decode_file/")
30+
async def decode():
31+
decoded_content = "none"
32+
file_path = "./test_files_decoder/x1.txt"
33+
try:
34+
with open(file_path, "r") as buffer:
35+
content = buffer.read()
36+
decoder = BdecoderAPI()
37+
decoded_content = await decoder.decode(content)
38+
except Exception as e:
39+
raise HTTPException(status_code=500, detail=str(e))
40+
41+
return {"decoded_content": decoded_content}
42+
43+
44+
if __name__ == "__main__":
45+
uvicorn.run("main:app", host="0.0.0.0", port=8080, reload=False)

parser.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,20 @@ async def parse(self, data: str) -> BDictionary:
299299
return output
300300

301301

302+
class BdecoderAPI:
303+
"""
304+
API for encoding data into bencoding format.
305+
"""
306+
@staticmethod
307+
async def decode(data: str) -> str:
308+
"""
309+
Create a BEncoderOperation object and encode the data.
310+
"""
311+
parser = BDecoderOperation()
312+
parsed = await parser.parse(data)
313+
return str(parsed)
314+
315+
302316
async def task_run(filename):
303317
"""
304318
Task to read data from a file.

0 commit comments

Comments
 (0)