-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontroller.py
More file actions
74 lines (63 loc) · 2.13 KB
/
controller.py
File metadata and controls
74 lines (63 loc) · 2.13 KB
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
"""
This module contains the FastAPI application and the endpoints for the floor generation.
"""
import secrets
from fastapi import FastAPI
from starlette.responses import FileResponse
from fastapi.middleware.cors import CORSMiddleware
from utils import globals as my_globals
from generators.generator import Generator
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
_FILE_NAME = my_globals.DEFAULT_FLOOR_NAME + my_globals.JSON_SUFFIX
_ROOT = "/"
_GENERATE = "/gen"
@app.get(_ROOT)
async def root():
"""This endpoint returns a test message."""
return {"message": "Test"}
@app.get(_GENERATE + "/{floor_id}")
async def generate_with_id(floor_id: int) -> FileResponse:
"""
This endpoint generates a floor with the given id
and returns it in a FileResponse as a json file.
@param floor_id id for the floor
@return: Floor in json file
"""
generator = Generator(
seed=secrets.token_hex(16), output_file_name=_FILE_NAME, stage_id=floor_id
)
generator.generate()
path = generator.save()
return FileResponse(path=path, filename=_FILE_NAME)
@app.get(_GENERATE + "/{floor_id}/{seed}")
async def generate_with_id_and_seed(floor_id: int, seed: str) -> FileResponse:
"""
This endpoint generates a floor with the given id
and seed and returns it in a FileResponse as a json file.
@param floor_id: id for the floor
@param seed: seed for the floor
@return: Floor in json file
"""
generator = Generator(seed=seed, output_file_name=_FILE_NAME, stage_id=floor_id)
generator.generate()
path = generator.save()
return FileResponse(path=path, filename=_FILE_NAME)
@app.get(_GENERATE)
async def generate() -> FileResponse:
"""
This endpoint generates a floor with the id zero and returns it as a json file.
@return: Floor as json file
"""
generator = Generator(
seed=secrets.token_hex(16), output_file_name=_FILE_NAME, stage_id=0
)
generator.generate()
path = generator.save()
return FileResponse(path=path, filename=_FILE_NAME)