{"id":23525712,"url":"https://github.com/napjon/flext","last_synced_at":"2025-05-14T08:12:44.679Z","repository":{"id":268682329,"uuid":"905146481","full_name":"napjon/flext","owner":"napjon","description":"A lightweight Flask extension that brings Next.js-style API routing","archived":false,"fork":false,"pushed_at":"2024-12-18T09:12:40.000Z","size":7,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-17T01:45:35.189Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/napjon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-18T08:55:32.000Z","updated_at":"2024-12-18T09:12:44.000Z","dependencies_parsed_at":"2024-12-18T09:40:14.627Z","dependency_job_id":"c9bd89a1-72d9-414a-aea2-76978a21b77f","html_url":"https://github.com/napjon/flext","commit_stats":null,"previous_names":["napjon/flext"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/napjon%2Fflext","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/napjon%2Fflext/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/napjon%2Fflext/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/napjon%2Fflext/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/napjon","download_url":"https://codeload.github.com/napjon/flext/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254101554,"owners_count":22014908,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-25T19:11:56.705Z","updated_at":"2025-05-14T08:12:39.671Z","avatar_url":"https://github.com/napjon.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Flext 🚀\n\nA lightweight Flask extension that brings Next.js-style API routing to Flask applications. Organize your Flask APIs using file-based routing, just like Next.js!\n\n## Features ✨\n\n- 📁 File-based routing similar to Next.js\n- 🔄 Automatic HTTP method detection from filenames\n- 🔌 Simple integration with existing Flask apps\n- 0️⃣ Zero additional dependencies beyond Flask\n- 🎯 Convention over configuration\n- 🔧 Easy to extend and customize\n\n## Installation 📦\n\n```bash\n# Currently local installation\ngit clone https://github.com/napjon/flext\ncd flext\npip install -e .\n```\n\n## Quick Start 🚀\n\n1. Create your API structure:\n\n```\nyour_app/\n├── app.py\n├── api/\n│   ├── hello/\n│   │   ├── get.py\n│   │   └── post.py\n```\n\n2. Write your endpoint in `api/hello/get.py`:\n\n```python\ndef main(request):\n    return {\"message\": \"Hello World!\"}\n```\n\n3. Setup your Flask app (`app.py`):\n\n```python\nfrom flask import Flask\nfrom flext import Flext\n\napp = Flask(__name__)\napp = Flext(app)\n\nif __name__ == '__main__':\n    app.run(debug=True)\n```\n\nThat's it! Your API endpoints are now available at:\n- `GET /api/hello`\n- `POST /api/hello`\n\n## API Endpoint Structure 📝\n\nEach endpoint should have a `main` function that accepts a Flask request object:\n\n```python\ndef main(request):\n    \"\"\"\n    Args:\n        request (flask.Request): HTTP request object.\n    Returns:\n        Response: Any Flask-compatible response\n    \"\"\"\n    return {\"status\": \"success\"}\n```\n\n## Supported HTTP Methods 🔄\n\nFiles are automatically mapped to HTTP methods:\n- `get.py` → GET\n- `post.py` → POST\n- `put.py` → PUT\n- `delete.py` → DELETE\n- `patch.py` → PATCH\n\n## Configuration ⚙️\n\nCustomize Flext behavior during initialization:\n\n```python\napp = Flext(\n    app,\n    api_dir='custom_api_dir',  # Default: 'api'\n    prefix='/api/v1',          # Default: '/api'\n    debug=True                 # Default: False\n)\n```\n\n## Deployment 🌐\n\n### Traditional Server\n\n```bash\npip install gunicorn\ngunicorn app:app\n```\n\n### Docker\n\n```dockerfile\nFROM python:3.9-slim\n\nWORKDIR /app\nCOPY . .\nRUN pip install -r requirements.txt\n\nCMD [\"gunicorn\", \"app:app\"]\n```\n\n### Cloud Functions (GCP)\n\nModify the entry point for Google Cloud Functions:\n\n```python\n# main.py\nfrom flask import Flask\nfrom flext import Flext\n\napp = Flask(__name__)\napp = Flext(app)\n\n# For Cloud Functions\ndef entry_point(request):\n    return app(request)\n```\n\n## Common Patterns 📋\n\n### Error Handling\n\nCreate a global error handler:\n\n```python\n# api/_middleware.py\ndef handle_errors(app):\n    @app.errorhandler(404)\n    def not_found(e):\n        return {\"error\": \"Not found\"}, 404\n\n    @app.errorhandler(500)\n    def server_error(e):\n        return {\"error\": \"Internal server error\"}, 500\n```\n\n### Request Validation\n\nValidate incoming requests:\n\n```python\n# api/users/post.py\nfrom flask import jsonify\n\ndef main(request):\n    if not request.is_json:\n        return jsonify({\"error\": \"Content-Type must be application/json\"}), 400\n        \n    data = request.get_json()\n    required = ['napjon', 'email']\n    \n    if not all(k in data for k in required):\n        return jsonify({\"error\": \"Missing required fields\"}), 400\n        \n    return jsonify({\"status\": \"user created\"})\n```\n\n### Database Integration\n\nUse Flask's application context:\n\n```python\n# api/users/get.py\nfrom flask import current_app\n\ndef main(request):\n    db = current_app.config['DB']\n    users = db.query(...)\n    return jsonify(users)\n```\n\n## Development Guide 🛠️\n\n### Project Structure\n\n```\nflext/\n├── flext.py           # Main library code\n├── setup.py          # Package setup\n├── requirements.txt  # Dependencies\n└── tests/           # Test cases\n```\n\n### Running Tests\n\n```bash\npip install pytest\npytest tests/\n```\n\n### Future Development Ideas 💡\n\n1. **Middleware Support**\n   - Add support for middleware files (e.g., `_middleware.py`)\n   - Enable global and route-specific middleware\n\n2. **API Documentation**\n   - Automatic OpenAPI/Swagger documentation generation\n   - Integration with Flask-RESTX\n\n3. **Hot Reloading**\n   - Implement hot reloading for API routes\n   - Development server with auto-reload\n\n4. **Type Hints**\n   - Add comprehensive type hints\n   - Runtime type checking\n\n5. **Route Validation**\n   - Schema validation support\n   - Pydantic integration\n\n6. **CLI Tool**\n   - Scaffold new API endpoints\n   - Generate boilerplate code\n\n## Contributing 🤝\n\n1. Fork the repository\n2. Create a feature branch (`git checkout -b feature/amazing-feature`)\n3. Commit your changes (`git commit -m 'Add amazing feature'`)\n4. Push to the branch (`git push origin feature/amazing-feature`)\n5. Open a Pull Request\n\n### Development Setup\n\n```bash\n# Clone repository\ngit clone https://github.com/napjon/flext\ncd flext\n\n# Create virtual environment\npython -m venv venv\nsource venv/bin/activate  # or `venv\\Scripts\\activate` on Windows\n\n# Install dependencies\npip install -r requirements.txt\npip install -r requirements-dev.txt\n\n# Run tests\npytest\n```\n\n## Support 💬\n\n- 📝 [Documentation](https://github.com/napjon/flext/wiki)\n- 🐛 [Issue Tracker](https://github.com/napjon/flext/issues)\n- 💻 [Examples](https://github.com/napjon/flext/tree/main/examples)\n\n## License 📄\n\nMIT License - see [LICENSE](LICENSE) for details\n\n## Acknowledgments 🙏\n\n- Inspired by Next.js API routes\n- Built on top of Flask\n- Made with ❤️ by the community","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnapjon%2Fflext","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnapjon%2Fflext","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnapjon%2Fflext/lists"}