Add pattern definitions endpoint
- Add /definitions endpoint to pattern controller - Load pattern.json with fallback paths for local dev and MicroPython
This commit is contained in:
@@ -1,10 +1,32 @@
|
|||||||
from microdot import Microdot
|
from microdot import Microdot
|
||||||
from models.pattern import Pattern
|
from models.pattern import Pattern
|
||||||
import json
|
import json
|
||||||
|
import sys
|
||||||
|
|
||||||
controller = Microdot()
|
controller = Microdot()
|
||||||
patterns = Pattern()
|
patterns = Pattern()
|
||||||
|
|
||||||
|
def load_pattern_definitions():
|
||||||
|
"""Load pattern definitions from pattern.json file."""
|
||||||
|
try:
|
||||||
|
# Try different paths for local development vs MicroPython
|
||||||
|
paths = ['db/pattern.json', 'pattern.json', '/db/pattern.json']
|
||||||
|
for path in paths:
|
||||||
|
try:
|
||||||
|
with open(path, 'r') as f:
|
||||||
|
return json.load(f)
|
||||||
|
except OSError:
|
||||||
|
continue
|
||||||
|
return {}
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error loading pattern.json: {e}")
|
||||||
|
return {}
|
||||||
|
|
||||||
|
@controller.get('/definitions')
|
||||||
|
async def get_pattern_definitions(request):
|
||||||
|
"""Get pattern definitions from pattern.json."""
|
||||||
|
definitions = load_pattern_definitions()
|
||||||
|
return json.dumps(definitions), 200, {'Content-Type': 'application/json'}
|
||||||
|
|
||||||
@controller.get('')
|
@controller.get('')
|
||||||
async def list_patterns(request):
|
async def list_patterns(request):
|
||||||
|
|||||||
Reference in New Issue
Block a user