def get_app() -> FastAPI: """ Creates the Fast API application instance :return: The application instance """ settings = get_settings() app = FastAPI( title="LinuxForHealth Connect", description="LinuxForHealth Connectors for Inbound Data Processing", version=__version__, ) app.add_middleware(HTTPSRedirectMiddleware) app.include_router(router) app.add_event_handler("startup", configure_logging) app.add_event_handler("startup", log_configuration) app.add_event_handler("startup", configure_internal_integrations) app.add_event_handler("shutdown", close_internal_clients) app.add_exception_handler(HTTPException, http_exception_handler) # use the slowapi rate limiter app.add_middleware(SlowAPIMiddleware) limiter = Limiter(key_func=get_remote_address, default_limits=[settings.connect_rate_limit]) app.state.limiter = limiter app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler) return app
a, b = 0, 1 for _ in range(n): yield a a, b = b, a + b fibo_row = list(fib(fibo_count)) fibo_path = '/' + '/'.join(map(str, fibo_row)) maze_link = '/maze' treasure_link = f'{fibo_path}/treasure' print(f'treasure path is: {treasure_link}') # treasure_link = f'/treasure' app = Starlette(debug=True) limiter = Limiter(key_func=get_remote_address) app.state.limiter = limiter app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler) @app.route('/maze/hint') async def hint(request): message = '\tHere is your hint:\n\n' \ '\ta+b is to a as a is to b.\n\n' \ '\tThe answer to all questions is, quote:\n' \ '\t\t\"[...] the sort of number that you could ' \ 'without any fear introduce to your parents\"' \ '\n' return PlainTextResponse(message)
import time from fastapi import FastAPI, Request from fastapi.responses import HTMLResponse from fastapi.staticfiles import StaticFiles from fastapi.templating import Jinja2Templates from slowapi import Limiter, _rate_limit_exceeded_handler from slowapi.errors import RateLimitExceeded from slowapi.util import get_remote_address from starlette.middleware.cors import CORSMiddleware from faker import DhivehiFaker as BaivaruFaker limiter = Limiter(key_func=get_remote_address) limiter.enabled = True app = FastAPI( title="Baivaru Faker", description="Generate lorem but for Dhivehi", version="0.0.1", ) app.state.limiter = limiter app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler) # Allow all origins CORS Middleware app.add_middleware( CORSMiddleware, allow_origins=["*"],
from config.utils import get_config from fastapi import Depends, Request from slowapi import Limiter from slowapi.util import get_remote_address conf = get_config() default_limit = conf.get_string("rate-limits.default") limiter = Limiter( key_func=get_remote_address, default_limits=[default_limit], headers_enabled=True)
from app.api.api_v1.routers.notes import notes_router from app.core import config from app.db.session import SessionLocal from app.core.auth import get_current_active_user from slowapi import Limiter, _rate_limit_exceeded_handler from slowapi.util import get_remote_address from slowapi.errors import RateLimitExceeded from slowapi.middleware import SlowAPIMiddleware from fastapi.middleware.cors import CORSMiddleware # Initialize app, with default rate limiting for all endpoints limiter = Limiter(key_func=get_remote_address, default_limits=["1/second"]) app = FastAPI( title=config.PROJECT_NAME, docs_url="/api/docs", openapi_url="/api" ) app.state.limiter = limiter app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler) app.add_middleware(SlowAPIMiddleware) # Enable CORS app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], )
from engines.controller import BEST, ENGINE_NAME_MAP from gql_query import GQLQuery, RateLimGQLApp from models.request import TranslationRequest from models.response import TranslationResponse from settings import DatabaseSettings, FeaturesSettings, Settings from translate import controller, do_translation _logger = logging.getLogger(__name__) features = FeaturesSettings() with open("VERSION") as f: version = f.read() limiter = Limiter( key_func=get_ipaddr, storage_uri=str(features.redis_dsn) if features.redis_dsn else None, ) app = FastAPI( title="multi-translate", description= "Multi-Translate is a unified interface on top of various translate APIs providing optimal " "translations, persistence, fallback.", version=version, ) origins = [ "http://localhost.tiangolo.com", "https://localhost.tiangolo.com", "http://localhost", "http://localhost:8080",
from random import choice from typing import Optional from fastapi import FastAPI from fastapi.exceptions import HTTPException from json import load from starlette.requests import Request from slowapi import Limiter, _rate_limit_exceeded_handler from slowapi.util import get_remote_address from slowapi.errors import RateLimitExceeded from slowapi.middleware import SlowAPIMiddleware with open('./jokes.json') as f: all_jokes = load(f) limiter = Limiter(key_func=get_remote_address, default_limits=["5/minute"]) app = FastAPI() app.state.limiter = limiter app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler) app.add_middleware(SlowAPIMiddleware) @app.get("/") async def home(): return {"joke": "Yo momma"} @app.get("/jokes") async def send_joke(count: Optional[int] = 1): if count > 1 and count < len(all_jokes): return [{"joke": choice(all_jokes)} for i in range(count)]
from slowapi import Limiter from slowapi.util import get_remote_address limiter = Limiter( key_func=get_remote_address, default_limits=["30 per minute"], headers_enabled=True)