### Install starlette-compress Source: https://github.com/zaczero/starlette-compress/blob/main/README.md Installs the starlette-compress package using pip. This is the first step to integrate the middleware into your Starlette or FastAPI application. ```Shell pip install starlette-compress ``` -------------------------------- ### Manage Custom Content-Types Source: https://github.com/zaczero/starlette-compress/blob/main/README.md Provides examples of how to add or remove content-types that the middleware should compress. By default, only certain content-types are compressed, and this allows customization. ```Python from starlette_compress import add_compress_type, remove_compress_type add_compress_type("application/my-custom-type") remove_compress_type("application/json") ``` -------------------------------- ### Basic Starlette Integration Source: https://github.com/zaczero/starlette-compress/blob/main/README.md Demonstrates how to add the CompressMiddleware to a Starlette application. This involves importing the middleware and adding it to the application's middleware list. ```Python from starlette.applications import Starlette from starlette.middleware import Middleware from starlette_compress import CompressMiddleware middleware = [ Middleware(CompressMiddleware) ] app = Starlette(routes=..., middleware=middleware) ``` -------------------------------- ### Basic FastAPI Integration Source: https://github.com/zaczero/starlette-compress/blob/main/README.md Shows how to add the CompressMiddleware to a FastAPI application using the add_middleware method. This enables response compression for FastAPI applications. ```Python from fastapi import FastAPI from starlette_compress import CompressMiddleware app = FastAPI() app.add_middleware(CompressMiddleware) ``` -------------------------------- ### Configure Minimum Response Size Source: https://github.com/zaczero/starlette-compress/blob/main/README.md Illustrates how to set a custom minimum response size for compression. Responses smaller than this size will not be compressed. This can be configured for both Starlette and FastAPI applications. ```Python # Starlette middleware = [ Middleware(CompressMiddleware, minimum_size=1000) ] # FastAPI app.add_middleware(CompressMiddleware, minimum_size=1000) ``` -------------------------------- ### Tune Compression Levels Source: https://github.com/zaczero/starlette-compress/blob/main/README.md Explains how to adjust the compression levels for ZStd, Brotli, and GZip algorithms. Higher levels result in smaller compressed sizes but increase compression time. This configuration applies to both Starlette and FastAPI. ```Python # Starlette middleware = [ Middleware(CompressMiddleware, zstd_level=6, brotli_quality=6, gzip_level=6) ] # FastAPI app.add_middleware(CompressMiddleware, zstd_level=6, brotli_quality=6, gzip_level=6) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.