Add example projects
Some checks failed
CI / test (push) Has been cancelled
CI / build (push) Has been cancelled

This commit is contained in:
2026-02-01 23:47:35 +00:00
parent 187bd7d0e7
commit dfabe37769

108
examples/simple_project.py Normal file
View File

@@ -0,0 +1,108 @@
"""
A simple example project demonstrating CodeChunk CLI usage.
This is a simple web server module with basic routing and request handling.
"""
import json
from typing import Dict, List, Optional
from dataclasses import dataclass
@dataclass
class Request:
"""HTTP request representation."""
method: str
path: str
headers: Dict[str, str]
body: Optional[str] = None
@dataclass
class Response:
"""HTTP response representation."""
status_code: int
body: str
content_type: str = "text/plain"
class Router:
"""Simple HTTP router for handling routes."""
def __init__(self):
self.routes: Dict[str, callable] = {}
def add_route(self, path: str, handler: callable) -> None:
"""Add a new route to the router."""
self.routes[path] = handler
def get_handler(self, path: str) -> Optional[callable]:
"""Get handler for a given path."""
return self.routes.get(path)
class WebServer:
"""Simple web server implementation."""
def __init__(self, host: str = "localhost", port: int = 8080):
self.host = host
self.port = port
self.router = Router()
self.running = False
def start(self) -> None:
"""Start the web server."""
self.running = True
self._log(f"Server started on {self.host}:{self.port}")
def stop(self) -> None:
"""Stop the web server."""
self.running = False
self._log("Server stopped")
def handle_request(self, request: Request) -> Response:
"""Handle an incoming HTTP request."""
handler = self.router.get_handler(request.path)
if handler is None:
return Response(404, "Not Found")
try:
result = handler(request)
return Response(200, json.dumps(result))
except Exception as e:
return Response(500, str(e))
def _log(self, message: str) -> None:
"""Log a message."""
print(f"[Server] {message}")
def create_default_server() -> WebServer:
"""Create a web server with default configuration."""
server = WebServer(host="0.0.0.0", port=8080)
@server.router.add_route
def home(request: Request) -> Dict:
return {"status": "ok", "message": "Welcome!"}
@server.router.add_route
def health(request: Request) -> Dict:
return {"status": "healthy"}
return server
def main() -> None:
"""Main entry point for the web server."""
server = create_default_server()
print("Starting web server...")
server.start()
print("Server is running. Press Ctrl+C to stop.")
if __name__ == "__main__":
main()