Skip to content

Commit

Permalink
**BREAKING CHANGE** renamed Server class to App
Browse files Browse the repository at this point in the history
  • Loading branch information
patx committed Feb 3, 2025
1 parent 2531e7a commit a406c2c
Show file tree
Hide file tree
Showing 16 changed files with 41 additions and 41 deletions.
6 changes: 3 additions & 3 deletions MicroPie.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ def __init__(self, scope: Dict[str, Any]) -> None:
self.files: Dict[str, Any] = {}


class Server:
"""ASGI server for handling HTTP requests and WebSocket connections in MicroPie."""
class App:
"""ASGI application for handling HTTP requests and WebSocket connections in MicroPie."""
SESSION_TIMEOUT: int = 8 * 3600

def __init__(self) -> None:
"""
Initialize a new Server instance.
Initialize a new App instance.
If Jinja2 is installed, set up the template environment.
"""
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ pip install uvicorn

Save the following as `app.py`:
```python
from MicroPie import Server
from MicroPie import App

class MyApp(Server):
class MyApp(App):
async def index(self):
return "Welcome to MicroPie ASGI."

Expand All @@ -60,11 +60,11 @@ Access your app at [http://127.0.0.1:8000](http://127.0.0.1:8000).
## **Core Features**

### **1. Flexible HTTP Routing for GET Requests**
MicroPie automatically maps URLs to methods within your `Server` class. Routes can be defined as either synchronous or asynchronous functions, offering good flexibility.
MicroPie automatically maps URLs to methods within your `App` class. Routes can be defined as either synchronous or asynchronous functions, offering good flexibility.

For GET requests, pass data through query strings or URL path segments, automatically mapped to method arguments.
```python
class MyApp(Server):
class MyApp(App):
async def greet(self, name="Guest"):
return f"Hello, {name}!"

Expand All @@ -79,7 +79,7 @@ class MyApp(Server):
### **2. Flexible HTTP POST Request Handling**
MicroPie also supports handling form data submitted via HTTP POST requests. Form data is automatically mapped to method arguments. It is able to handle default values and raw POST data:
```python
class MyApp(Server):
class MyApp(App):
async def submit_default_values(self, username="Anonymous"):
return f"Form submitted by: {username}"

Expand All @@ -98,7 +98,7 @@ Dynamic HTML generation is supported via Jinja2. This happens asynchronously usi

#### **`app.py`**
```python
class MyApp(Server):
class MyApp(App):
async def index(self):
return await self._render_template("index.html", title="Welcome", message="Hello from MicroPie!")
```
Expand All @@ -125,7 +125,7 @@ Here again, like Websockets, MiroPie does not have a built in static file method
Support for streaming responses makes it easy to send data in chunks.

```python
class MyApp(Server):
class MyApp(App):
async def stream(self):
async def generator():
for i in range(1, 6):
Expand All @@ -137,7 +137,7 @@ class MyApp(Server):
Built-in session handling simplifies state management:

```python
class MyApp(Server):
class MyApp(App):
async def index(self):
if "visits" not in self.session:
self.request.session["visits"] = 1
Expand All @@ -147,7 +147,7 @@ class MyApp(Server):
```

### **8. Deployment**
MicroPie apps can be deployed using any ASGI server. For example, using Uvicorn if our application is saved as `app.py` and our `Server` subclass is assigned to the `app` variable we can run it with:
MicroPie apps can be deployed using any ASGI server. For example, using Uvicorn if our application is saved as `app.py` and our `App` subclass is assigned to the `app` variable we can run it with:
```bash
uvicorn app:app --workers 4 --port 8000
```
Expand Down
6 changes: 3 additions & 3 deletions docs/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ <h2>Request Class</h2>
<li><strong>files</strong>: Uploaded files in the request.</li>
</ul>

<h2>Server Class</h2>
<p>ASGI server for handling HTTP requests and WebSocket connections.</p>
<pre><code>class Server()</code></pre>
<h2>App Class</h2>
<p>ASGI application framework for handling HTTP requests.</p>
<pre><code>class App()</code></pre>

<h3>Properties</h3>
<ul>
Expand Down
4 changes: 2 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@

<h2>MicroPie is Fun</h2>
<pre><code>
<span class="c2">from</span> MicroPie <span class="c2">import</span> Server
<span class="c2">from</span> MicroPie <span class="c2">import</span> App

<span class="c2">class</span> MyApp(<span class="c9">Server</span>):
<span class="c2">class</span> MyApp(<span class="c9">App</span>):

<span class="c2">async def</span> index(<span class="c9">self</span>):
return <span class="c9">'Hello World!'</span>
Expand Down
4 changes: 2 additions & 2 deletions examples/file_uploads/app.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from MicroPie import Server
from MicroPie import App

import os
from typing import Any

UPLOAD_DIR = "uploads"
os.makedirs(UPLOAD_DIR, exist_ok=True) # Ensure upload directory exists

class FileUploadApp(Server):
class FileUploadApp(App):

async def index(self):
"""Serves an HTML form for file uploads."""
Expand Down
4 changes: 2 additions & 2 deletions examples/headers/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from MicroPie import Server
from MicroPie import App


class Root(Server):
class Root(App):
def index(self):
headers = [
("Content-Type", "text/html"),
Expand Down
4 changes: 2 additions & 2 deletions examples/hello_world/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from MicroPie import Server
from MicroPie import App


class Root(Server):
class Root(App):

async def index(self):
return 'Hello ASGI World!'
Expand Down
4 changes: 2 additions & 2 deletions examples/pastebin/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

from uuid import uuid4

from MicroPie import Server
from MicroPie import App
from pickledb import PickleDB
from markupsafe import escape

db = PickleDB('pastes.db')

class Root(Server):
class Root(App):

async def index(self):
if self.request.method == "POST":
Expand Down
4 changes: 2 additions & 2 deletions examples/requests/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from MicroPie import Server
from MicroPie import App

# Request handlers defined outside the class

Expand Down Expand Up @@ -28,7 +28,7 @@ def options_handler():
]

# Define the custom server application
class Root(Server):
class Root(App):

# Handle root URL requests and delegate based on HTTP method
def index(self):
Expand Down
4 changes: 2 additions & 2 deletions examples/socketio/chatroom.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import socketio
from MicroPie import Server
from MicroPie import App

# Create a Socket.IO server with CORS support
sio = socketio.AsyncServer(async_mode="asgi", cors_allowed_origins="*") # Allow all origins

# Create the MicroPie server
class MyApp(Server):
class MyApp(App):
async def index(self):
return await self._render_template("chat.html")

Expand Down
4 changes: 2 additions & 2 deletions examples/socketio/webcam.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import socketio
from MicroPie import Server
from MicroPie import App

# Create the Socket.IO server
sio = socketio.AsyncServer(async_mode="asgi")
Expand All @@ -8,7 +8,7 @@
active_users = set()

# MicroPie Server with integrated Socket.IO
class MyApp(Server):
class MyApp(App):
async def index(self):
return await self.render_template("index_stream.html")

Expand Down
4 changes: 2 additions & 2 deletions examples/socketio/webtrc/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import socketio
from MicroPie import Server
from MicroPie import App

# 1) Create the Async Socket.IO server and wrap with an ASGI app.
sio = socketio.AsyncServer(async_mode="asgi")
Expand All @@ -8,7 +8,7 @@
active_users = set()

# 2) Create a MicroPie server class with routes
class MyApp(Server):
class MyApp(App):
async def index(self):
# A simple response for the root path
return 'Use /stream/*username* or /watch/*username*'
Expand Down
4 changes: 2 additions & 2 deletions examples/static_content/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from servestatic import ServeStaticASGI
from MicroPie import Server
from MicroPie import App

class Root(Server):
class Root(App):
async def index(self):
return "Hello, World!"

Expand Down
4 changes: 2 additions & 2 deletions examples/streaming/text.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import time
import asyncio
from MicroPie import Server
from MicroPie import App

class Root(Server):
class Root(App):

def index(self):
# Normal, immediate response (non-streaming)
Expand Down
4 changes: 2 additions & 2 deletions examples/streaming/video.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os
from MicroPie import Server
from MicroPie import App

VIDEO_PATH = "video.mp4"

class Root(Server):
class Root(App):
def index(self):
return '''
<html>
Expand Down
4 changes: 2 additions & 2 deletions examples/twutr/twutr.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from datetime import datetime
import re

from MicroPie import Server
from MicroPie import App
from pickledb import PickleDB
from markupsafe import escape

Expand Down Expand Up @@ -141,7 +141,7 @@ def replace_internal(match):
# ----------------


class Twutr(Server):
class Twutr(App):
"""
Main application class for handling routes.
The helper functions above are outside the class,
Expand Down

0 comments on commit a406c2c

Please sign in to comment.