handle_cors #2
29
app/main.py
29
app/main.py
@ -1,19 +1,40 @@
|
|||||||
'''
|
'''
|
||||||
Simple Geolocation with FastAPI
|
Simple Geolocation with FastAPI
|
||||||
'''
|
'''
|
||||||
|
import os
|
||||||
from ipaddress import IPv4Address, IPv4Network, IPv6Address, IPv6Network
|
from ipaddress import IPv4Address, IPv4Network, IPv6Address, IPv6Network
|
||||||
from typing import Annotated, Optional, Union
|
from typing import Annotated, Optional, Union
|
||||||
|
|
||||||
import geoip2.database
|
import geoip2.database
|
||||||
from geoip2.errors import AddressNotFoundError
|
from dotenv import load_dotenv
|
||||||
from fastapi import FastAPI, Path, Body, Request, Response, status
|
from fastapi import Body, FastAPI, Path, Request, Response, status
|
||||||
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
from fastapi.responses import RedirectResponse
|
from fastapi.responses import RedirectResponse
|
||||||
|
from geoip2.errors import AddressNotFoundError
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
# Load environment variables
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
GEOLITE2_ASN_DB = '/usr/local/share/GeoIP/GeoLite2-ASN.mmdb'
|
# Configure CORS from environment variables
|
||||||
GEOLITE2_CITY_DB = '/usr/local/share/GeoIP/GeoLite2-City.mmdb'
|
cors_origins = os.getenv('CORS_ALLOW_ORIGINS', 'http://localhost')
|
||||||
|
allow_origins = [origin.strip() for origin in cors_origins.split(',')
|
||||||
|
if origin.strip()]
|
||||||
|
|
||||||
|
app.add_middleware(
|
||||||
|
CORSMiddleware,
|
||||||
|
allow_origins=allow_origins,
|
||||||
|
allow_credentials=True,
|
||||||
|
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
||||||
|
allow_headers=["*"],
|
||||||
|
)
|
||||||
|
|
||||||
|
GEOLITE2_ASN_DB = os.getenv('GEOLITE2_ASN_DB',
|
||||||
|
'/usr/local/share/GeoIP/GeoLite2-ASN.mmdb')
|
||||||
|
GEOLITE2_CITY_DB = os.getenv('GEOLITE2_CITY_DB',
|
||||||
|
'/usr/local/share/GeoIP/GeoLite2-City.mmdb')
|
||||||
|
|
||||||
|
|
||||||
class IPAddressParam(BaseModel):
|
class IPAddressParam(BaseModel):
|
||||||
|
|||||||
Reference in New Issue
Block a user