Skip to content
napjonPublic

About

A lightweight Flask extension that brings Next.js-style API routing

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

Β 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Flext πŸš€

A 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!

Features ✨

  • πŸ“ File-based routing similar to Next.js
  • πŸ”„ Automatic HTTP method detection from filenames
  • πŸ”Œ Simple integration with existing Flask apps
  • 0️⃣ Zero additional dependencies beyond Flask
  • 🎯 Convention over configuration
  • πŸ”§ Easy to extend and customize

Installation πŸ“¦

# Currently local installation
git clone https://github.com/napjon/flext
cd flext
pip install -e .

Quick Start πŸš€

  1. Create your API structure:
your_app/
β”œβ”€β”€ app.py
β”œβ”€β”€ api/
β”‚   β”œβ”€β”€ hello/
β”‚   β”‚   β”œβ”€β”€ get.py
β”‚   β”‚   └── post.py
  1. Write your endpoint in api/hello/get.py:
def main(request):
    return {"message": "Hello World!"}
  1. Setup your Flask app (app.py):
from flask import Flask
from flext import Flext

app = Flask(__name__)
app = Flext(app)

if __name__ == '__main__':
    app.run(debug=True)

That's it! Your API endpoints are now available at:

  • GET /api/hello
  • POST /api/hello

API Endpoint Structure πŸ“

Each endpoint should have a main function that accepts a Flask request object:

def main(request):
    """
    Args:
        request (flask.Request): HTTP request object.
    Returns:
        Response: Any Flask-compatible response
    """
    return {"status": "success"}

Supported HTTP Methods πŸ”„

Files are automatically mapped to HTTP methods:

  • get.py β†’ GET
  • post.py β†’ POST
  • put.py β†’ PUT
  • delete.py β†’ DELETE
  • patch.py β†’ PATCH

Configuration βš™οΈ

Customize Flext behavior during initialization:

app = Flext(
    app,
    api_dir='custom_api_dir',  # Default: 'api'
    prefix='/api/v1',          # Default: '/api'
    debug=True                 # Default: False
)

Deployment 🌐

Traditional Server

pip install gunicorn
gunicorn app:app

Docker

FROM python:3.9-slim

WORKDIR /app
COPY . .
RUN pip install -r requirements.txt

CMD ["gunicorn", "app:app"]

Cloud Functions (GCP)

Modify the entry point for Google Cloud Functions:

# main.py
from flask import Flask
from flext import Flext

app = Flask(__name__)
app = Flext(app)

# For Cloud Functions
def entry_point(request):
    return app(request)

Common Patterns πŸ“‹

Error Handling

Create a global error handler:

# api/_middleware.py
def handle_errors(app):
    @app.errorhandler(404)
    def not_found(e):
        return {"error": "Not found"}, 404

    @app.errorhandler(500)
    def server_error(e):
        return {"error": "Internal server error"}, 500

Request Validation

Validate incoming requests:

# api/users/post.py
from flask import jsonify

def main(request):
    if not request.is_json:
        return jsonify({"error": "Content-Type must be application/json"}), 400
        
    data = request.get_json()
    required = ['napjon', 'email']
    
    if not all(k in data for k in required):
        return jsonify({"error": "Missing required fields"}), 400
        
    return jsonify({"status": "user created"})

Database Integration

Use Flask's application context:

# api/users/get.py
from flask import current_app

def main(request):
    db = current_app.config['DB']
    users = db.query(...)
    return jsonify(users)

Development Guide πŸ› οΈ

Project Structure

flext/
β”œβ”€β”€ flext.py           # Main library code
β”œβ”€β”€ setup.py          # Package setup
β”œβ”€β”€ requirements.txt  # Dependencies
└── tests/           # Test cases

Running Tests

pip install pytest
pytest tests/

Future Development Ideas πŸ’‘

  1. Middleware Support

    • Add support for middleware files (e.g., _middleware.py)
    • Enable global and route-specific middleware
  2. API Documentation

    • Automatic OpenAPI/Swagger documentation generation
    • Integration with Flask-RESTX
  3. Hot Reloading

    • Implement hot reloading for API routes
    • Development server with auto-reload
  4. Type Hints

    • Add comprehensive type hints
    • Runtime type checking
  5. Route Validation

    • Schema validation support
    • Pydantic integration
  6. CLI Tool

    • Scaffold new API endpoints
    • Generate boilerplate code

Contributing 🀝

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Development Setup

# Clone repository
git clone https://github.com/napjon/flext
cd flext

# Create virtual environment
python -m venv venv
source venv/bin/activate  # or `venv\Scripts\activate` on Windows

# Install dependencies
pip install -r requirements.txt
pip install -r requirements-dev.txt

# Run tests
pytest

Support πŸ’¬

License πŸ“„

MIT License - see LICENSE for details

Acknowledgments πŸ™

  • Inspired by Next.js API routes
  • Built on top of Flask
  • Made with ❀️ by the community

About

A lightweight Flask extension that brings Next.js-style API routing

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages