patx/micropie
got rid of built in serve static, use 3rd part module for static files if needed
Commit 42247a4 · patx · 2025-01-28T14:31:22-05:00
Comments
No comments yet.
Diff
diff --git a/MicroPie.py b/MicroPie.py
index e422ebc..16c5767 100644
--- a/MicroPie.py
+++ b/MicroPie.py
@@ -371,19 +371,3 @@ class Server:
raise ImportError("Jinja2 is not installed.")
return self.env.get_template(name).render(kwargs)
- def serve_static(
- self, filepath: str
- ) -> Union[Tuple[int, str], Tuple[int, bytes, List[Tuple[str, str]]]]:
- safe_root = os.path.abspath("static")
- requested_file = os.path.abspath(os.path.join("static", filepath))
- if not requested_file.startswith(safe_root):
- return 403, "403 Forbidden"
- if not os.path.isfile(requested_file):
- return 404, "404 Not Found"
- content_type, _ = mimetypes.guess_type(requested_file)
- if not content_type:
- content_type = "application/octet-stream"
- with open(requested_file, "rb") as f:
- content = f.read()
- return 200, content, [("Content-Type", content_type)]
-
diff --git a/README.md b/README.md
index d27a9fc..e5cebb4 100644
--- a/README.md
+++ b/README.md
@@ -60,7 +60,7 @@ Access your app at [http://127.0.0.1:8000](http://127.0.0.1:8000).
## **Core Features**
### **1. Flexible Routing**
-MicroPie automatically maps URLs to methods within your `Server` class. Routes can be defined as either synchronous or asynchronous functions, offering unparalleled flexibility.
+MicroPie automatically maps URLs to methods within your `Server` class. Routes can be defined as either synchronous or asynchronous functions, offering good flexibility.
#### **Basic Routing**
```python
@@ -116,14 +116,8 @@ class MyApp(Server):
```
### **5. Static File Serving**
-Serve static files such as CSS, JS, and images from a `static` directory.
+Here again, like Websockets, MiroPie does not have a built in static file protocal. While MicroPie does not natively support static files, if you need them, you can easily integrate dedicated libraries like **ServeStatic** ot **Starlette’s StaticFiles** alongside Uvicorn to handle async static file serving. Check out [examples/serve_static](https://github.com/patx/micropie/tree/main/examples/serve_static) to see this in action.
-```python
-class MyApp(Server):
- def static(self, filename):
- return self.serve_static(filename)
-```
-To serve static files, place your files in the `static` directory and access them via `/static/<filename>`. You can define any route method handler you would like to serve static files, but for security reasons the built-in `serve_static` method will only serve files from the `static` directory.
### **6. Streaming Responses**
Support for streaming responses makes it easy to send data in chunks.
@@ -162,7 +156,7 @@ Check out the [examples folder](https://github.com/patx/micropie/tree/main/examp
- Template rendering
- Custom HTTP request handling
- File uploads
-- Serving static content
+- Serving static content with ServeStatic
- Session usage
- Websockets with Socket.io
- Async Streaming
diff --git a/examples/static_content/app.py b/examples/static_content/app.py
index a83fde6..d28b8d3 100644
--- a/examples/static_content/app.py
+++ b/examples/static_content/app.py
@@ -1,9 +1,12 @@
+from servestatic import ServeStaticASGI
from MicroPie import Server
class Root(Server):
+ async def index(self):
+ return "Hello, World!"
- def static(self, filename):
- return self.serve_static(filename)
-
-
+# Create the application
app = Root()
+
+# Wrap it with ServeStaticASGI for static file serving
+app = ServeStaticASGI(application, root="static")
diff --git a/setup.py b/setup.py
index 67cd881..47dd727 100644
--- a/setup.py
+++ b/setup.py
@@ -38,11 +38,11 @@ setup(name="MicroPie",
"Intended Audience :: Developers",
"License :: OSI Approved :: BSD License",
"Operating System :: OS Independent",
- "Programming Language :: Python",
"Topic :: Internet :: WWW/HTTP :: Dynamic Content",
"Topic :: Internet :: WWW/HTTP :: WSGI",
"Topic :: Internet :: WWW/HTTP :: HTTP Servers",
"Topic :: Internet :: WWW/HTTP :: WSGI :: Application",
+ "Framework :: AsyncIO",
"Topic :: Software Development :: Libraries :: Application Frameworks",
"Typing :: Typed"],
py_modules=['MicroPie'],