Skip to content

Commit 66be1d7

Browse files
committed
Support index pages in static folder
1 parent f473c69 commit 66be1d7

File tree

5 files changed

+21
-4
lines changed

5 files changed

+21
-4
lines changed

examples/simple_extensions.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ def get(self):
168168
# Create LabThings Flask app
169169
app, labthing = create_app(
170170
__name__,
171-
prefix="/api",
172171
title="My Lab Device API",
173172
description="Test LabThing-based API",
174173
version="0.1.0",

examples/static/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>Hello world</h1>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>Hello subfolder</h1>

src/labthings/server/extensions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ def __init__(
5454
self.add_view(
5555
self.static_view_class, f"{static_url_path}/<path:path>", endpoint="static"
5656
)
57+
self.add_view(
58+
self.static_view_class, f"{static_url_path}", endpoint="static_root"
59+
)
5760

5861
@property
5962
def views(self):

src/labthings/server/view/builder.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
from . import View, ActionView, PropertyView
88
from ..spec.utilities import compile_view_spec
99

10-
from flask import send_from_directory
10+
import os
11+
import glob
12+
from flask import send_file, abort
1113
import uuid
1214

1315

@@ -124,8 +126,19 @@ def static_from(static_folder: str, name=None):
124126
name = f"static-{uid}"
125127

126128
# Create inner functions
127-
def _get(self, path):
128-
return send_from_directory(static_folder, path)
129+
def _get(self, path=""):
130+
full_path = os.path.join(static_folder, path)
131+
if not os.path.exists(full_path):
132+
return abort(404)
133+
134+
if os.path.isfile(full_path):
135+
return send_file(full_path)
136+
137+
if os.path.isdir(full_path):
138+
indexes = glob.glob(os.path.join(full_path, "index.*"))
139+
if not indexes:
140+
return abort(404)
141+
return send_file(indexes[0])
129142

130143
# Generate a basic property class
131144
generated_class = type(name, (View, object), {"get": _get})

0 commit comments

Comments
 (0)