From dfabe37769329f8d13af37bf3cc43f3c1e6a6fda Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Sun, 1 Feb 2026 23:47:35 +0000 Subject: [PATCH] Add example projects --- examples/simple_project.py | 108 +++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 examples/simple_project.py diff --git a/examples/simple_project.py b/examples/simple_project.py new file mode 100644 index 0000000..1cf033a --- /dev/null +++ b/examples/simple_project.py @@ -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()