Skip to main content

Fast light weight web server

Project description

dustapi - A Simple Python Web Framework

dustapi is a lightweight web framework written in Python, designed to be simple and easy to use. It provides basic routing and response handling, making it a great starting point for learning about web frameworks or building small web applications.

Features

  • Simple and intuitive routing
  • Support for all HTTP methods (GET, POST, PUT, DELETE, etc.)
  • WebSocket support
  • Custom response types (JSON, HTML, etc.)
  • Middleware support (planned)
  • Extensible and lightweight
  • Auto generate docs with swagger
  • Support for jwt and session manager

Installation

To install dustapi, you can simply use pip:

pip install dustapi

Usage

Here is a simple example of how to create a web application using dustapi:

Create a dustapi project

dustapi createproject

Run dustapi server

dustapi runserver --host 0.0.0.0 --port 8000 --template-folder mytemplates --static-folder mystatic --log-file myapp.log

Example

from dustapi.application import Application, get_request
from dustapi.responses import JsonResponse, HtmlResponse, Response
import os

app = Application()

UPLOAD_FOLDER = 'uploads'
if not os.path.exists(UPLOAD_FOLDER):
    os.makedirs(UPLOAD_FOLDER)

def save_uploaded_file(file_data, upload_folder):
    filename = file_data['filename']
    filepath = os.path.join(upload_folder, filename)
    with open(filepath, 'wb') as f:
        f.write(file_data['content'])
    return filename

@app.route('/', methods=['GET'])
async def home():
    return app.render_template('index.html', title="Home", heading="Welcome to dustapi Framework", content="This is the home page.")

@app.route('/hello', methods=['GET'])
async def hello():
    return "Hello, World!"

@app.route('/json', methods=['GET'])
async def json_example():
    data = {"message": "This is a JSON response"}
    return JsonResponse(data)

@app.route('/data', methods=['POST'])
async def post_data():
    return "Data received via POST"

@app.route('/update', methods=['PUT'])
async def update_data():
    return "Data received via PUT"

@app.route('/delete', methods=['DELETE'])
async def delete_data():
    return "Data received via DELETE"

@app.route('/upload', methods=['POST'])
async def upload_file():
    request = get_request()
    if 'file' not in request.form:
        raise ValueError("No file part in the request")
    
    file_data = request.form['file']
    filename = file_data['filename']
    filepath = save_uploaded_file(file_data, UPLOAD_FOLDER)

    return f"File {filename} uploaded successfully"

@app.websocket('/ws')
async def echo(websocket, path):
    async for message in websocket:
        await websocket.send(f"Echo: {message}")

# Custom error handler for ValueError
@app.errorhandler(ValueError)
def handle_value_error(exc):
    return Response(str(exc), status=400)

# Custom error handler for generic exceptions
@app.errorhandler(Exception)
def handle_generic_exception(exc):
    return Response("An unexpected error occurred.", status=500)

if __name__ == '__main__':
    app.run(host='localhost', port=5000)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dustapi-0.0.5.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dustapi-0.0.5-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file dustapi-0.0.5.tar.gz.

File metadata

  • Download URL: dustapi-0.0.5.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for dustapi-0.0.5.tar.gz
Algorithm Hash digest
SHA256 b832736412c590db8bf395e6ebb52b081d3a641b595447b6092160d6d5dc048a
MD5 401196494fa64d96f0e13e1e4f6070a7
BLAKE2b-256 9a2bea42443ee337570a3bdc5f2eff758581c52a4074441c9e95d23e9466df01

See more details on using hashes here.

File details

Details for the file dustapi-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: dustapi-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 13.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for dustapi-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 12ac999dda1050058f37858315620c5b726ef04b443c35d9a734ab0aaf0cd081
MD5 f084929926be508eaa76dd2b3ad94705
BLAKE2b-256 0367ff9b97bd9cfc81fa058149fd6621880feca05ff304d8369e5c572c6bcebf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page