patx/micropie
remove work in progress blog app example
Commit 997b8e3 · patx · 2025-08-19T01:21:23-04:00
Comments
No comments yet.
Diff
diff --git a/examples/blog/app.py b/examples/blog/app.py
deleted file mode 100644
index 50de51f..0000000
--- a/examples/blog/app.py
+++ /dev/null
@@ -1,97 +0,0 @@
-from micropie import App
-from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
-from sqlalchemy.orm import sessionmaker, DeclarativeBase
-from sqlalchemy import Column, Integer, String, Text, DateTime
-from sqlalchemy.sql import select
-from datetime import datetime
-import markdown
-from jinja2 import Environment, FileSystemLoader
-import asyncio
-import os
-
-# Initialize Jinja2 environment for templates
-env = Environment(loader=FileSystemLoader('templates'))
-
-# Convert Markdown to HTML with GFM support
-def markdown_to_html(text):
- return markdown.markdown(text, extensions=['extra', 'tables', 'fenced_code'])
-
-# SQLAlchemy setup
-class Base(DeclarativeBase):
- pass
-
-class Post(Base):
- __tablename__ = 'posts'
- id = Column(Integer, primary_key=True)
- title = Column(String(100), nullable=False)
- content = Column(Text, nullable=False)
- created_at = Column(DateTime, nullable=False, default=datetime.utcnow)
-
-# SQLite async engine and session (will be initialized in startup handler)
-engine = None
-AsyncSessionLocal = None
-
-# Lifecycle event handlers
-async def setup_db():
- global engine, AsyncSessionLocal
- print("Setting up SQLite database...")
- engine = create_async_engine('sqlite+aiosqlite:///blog.db', echo=False)
- AsyncSessionLocal = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
- async with engine.begin() as conn:
- await conn.run_sync(Base.metadata.create_all)
- print("SQLite database setup complete.")
-
-async def close_db():
- global engine
- print("Closing SQLite database...")
- if engine:
- await engine.dispose()
- print("SQLite database closed.")
-
-class BlogApp(App):
- def __init__(self):
- super().__init__()
- # Register lifecycle handlers
- self.startup_handlers.append(setup_db)
- self.shutdown_handlers.append(close_db)
-
- async def index(self):
- # Fetch all posts, sorted by creation date (descending)
- async with AsyncSessionLocal() as session:
- posts = await session.execute(
- select(Post).order_by(Post.created_at.desc())
- )
- posts = posts.scalars().all()
- for post in posts:
- post.html_content = markdown_to_html(post.content)
- return await self._render_template('index.html', posts=posts)
-
- async def new(self, title=None, content=None):
- if self.request.method == 'POST':
- # Handle form submission
- title = self.request.body_params.get('title', [None])[0]
- content = self.request.body_params.get('content', [None])[0]
- if title and content:
- # Insert new post into SQLite
- async with AsyncSessionLocal() as session:
- new_post = Post(title=title, content=content)
- session.add(new_post)
- await session.commit()
- return self._redirect('/')
- return await self._render_template('new_post.html')
-
- async def post(self, id):
- # Fetch a single post by ID
- async with AsyncSessionLocal() as session:
- post = await session.get(Post, int(id))
- if not post:
- return 404, "Post not found"
- post.html_content = markdown_to_html(post.content)
- return await self._render_template('post.html', post=post)
-
-# Create the app instance
-app = BlogApp()
-
-if __name__ == '__main__':
- import uvicorn
- uvicorn.run(app, host='127.0.0.1', port=8080)
diff --git a/examples/blog/blog.db b/examples/blog/blog.db
deleted file mode 100644
index fd53bcf..0000000
Binary files a/examples/blog/blog.db and /dev/null differ
diff --git a/examples/blog/templates/index.html b/examples/blog/templates/index.html
deleted file mode 100644
index 3be686e..0000000
--- a/examples/blog/templates/index.html
+++ /dev/null
@@ -1,35 +0,0 @@
-<!DOCTYPE html>
-<html lang="en">
-<head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>MicroPie Blog</title>
- <script src="https://cdn.tailwindcss.com"></script>
-</head>
-<body class="bg-gray-100 font-sans">
- <header class="bg-blue-600 text-white p-4">
- <div class="container mx-auto flex justify-between items-center">
- <h1 class="text-2xl font-bold">MicroPie Blog</h1>
- <nav>
- <a href="/" class="px-4 hover:underline" aria-label="Home">Home</a>
- <a href="/new" class="px-4 hover:underline" aria-label="Create New Post">New Post</a>
- </nav>
- </div>
- </header>
- <main class="container mx-auto p-6">
- <div class="grid gap-6">
- {% for post in posts %}
- <article class="bg-white p-6 rounded-lg shadow-md" role="article">
- <h2 class="text-xl font-semibold">
- <a href="/post/{{ post.id }}" class="text-blue-600 hover:underline">{{ post.title }}</a>
- </h2>
- <p class="text-gray-500 text-sm">Posted on {{ post.created_at.strftime('%Y-%m-%d %H:%M:%S') }}</p>
- <div class="prose max-w-none mt-4">{{ post.html_content | safe }}</div>
- </article>
- {% else %}
- <p class="text-gray-600">No posts yet. <a href="/new" class="text-blue-600 hover:underline">Create one!</a></p>
- {% endfor %}
- </div>
- </main>
-</body>
-</html>
diff --git a/examples/blog/templates/new_post.html b/examples/blog/templates/new_post.html
deleted file mode 100644
index fcb3f57..0000000
--- a/examples/blog/templates/new_post.html
+++ /dev/null
@@ -1,56 +0,0 @@
-<!DOCTYPE html>
-<html lang="en">
-<head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Create New Post - MicroPie Blog</title>
- <script src="https://cdn.tailwindcss.com"></script>
-</head>
-<body class="bg-gray-100 font-sans">
- <header class="bg-blue-600 text-white p-4">
- <div class="container mx-auto flex justify-between items-center">
- <h1 class="text-2xl font-bold">MicroPie Blog</h1>
- <nav>
- <a href="/" class="px-4 hover:underline" aria-label="Home">Home</a>
- <a href="/new" class="px-4 hover:underline" aria-label="Create New Post">New Post</a>
- </nav>
- </div>
- </header>
- <main class="container mx-auto p-6">
- <div class="bg-white p-6 rounded-lg shadow-md max-w-2xl mx-auto">
- <h2 class="text-2xl font-semibold mb-4">Create New Post</h2>
- <div>
- <input type="text" id="title" name="title" placeholder="Post Title" required
- class="w-full p-2 mb-4 border rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
- aria-label="Post Title">
- <textarea id="content" name="content" placeholder="Write your Markdown content here..." required
- class="w-full p-2 mb-4 border rounded-md h-64 focus:outline-none focus:ring-2 focus:ring-blue-500"
- aria-label="Post Content"></textarea>
- <button onclick="submitForm()"
- class="bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700"
- aria-label="Submit Post">Submit</button>
- </div>
- <p class="mt-4">
- <a href="/" class="text-blue-600 hover:underline" aria-label="Back to Home">Back to Home</a>
- </p>
- </div>
- <script>
- function submitForm() {
- const title = document.getElementById('title').value;
- const content = document.getElementById('content').value;
- if (!title || !content) {
- alert('Please fill in both title and content.');
- return;
- }
- const formData = new FormData();
- formData.append('title', title);
- formData.append('content', content);
- fetch('/new', {
- method: 'POST',
- body: formData
- }).then(() => window.location.href = '/');
- }
- </script>
- </main>
-</body>
-</html>
diff --git a/examples/blog/templates/post.html b/examples/blog/templates/post.html
deleted file mode 100644
index d3fb6b9..0000000
--- a/examples/blog/templates/post.html
+++ /dev/null
@@ -1,30 +0,0 @@
-<!DOCTYPE html>
-<html lang="en">
-<head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>{{ post.title }} - MicroPie Blog</title>
- <script src="https://cdn.tailwindcss.com"></script>
-</head>
-<body class="bg-gray-100 font-sans">
- <header class="bg-blue-600 text-white p-4">
- <div class="container mx-auto flex justify-between items-center">
- <h1 class="text-2xl font-bold">MicroPie Blog</h1>
- <nav>
- <a href="/" class="px-4 hover:underline" aria-label="Home">Home</a>
- <a href="/new" class="px-4 hover:underline" aria-label="Create New Post">New Post</a>
- </nav>
- </div>
- </header>
- <main class="container mx-auto p-6">
- <article class="bg-white p-6 rounded-lg shadow-md max-w-2xl mx-auto" role="article">
- <h1 class="text-2xl font-semibold mb-2">{{ post.title }}</h1>
- <p class="text-gray-500 text-sm mb-4">Posted on {{ post.created_at.strftime('%Y-%m-%d %H:%M:%S') }}</p>
- <div class="prose max-w-none">{{ post.html_content | safe }}</div>
- <p class="mt-4">
- <a href="/" class="text-blue-600 hover:underline" aria-label="Back to Home">Back to Home</a>
- </p>
- </article>
- </main>
-</body>
-</html>