Spaces:
Sleeping
Sleeping
| import os | |
| import requests | |
| import logging | |
| from fastapi import FastAPI, Request, HTTPException | |
| from fastapi.responses import HTMLResponse | |
| # Configure logging | |
| logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') | |
| logger = logging.getLogger(__name__) | |
| # Initialize FastAPI app | |
| app = FastAPI() | |
| # --- IP Geolocation Logic --- | |
| def get_client_ip(request: Request) -> str: | |
| """ | |
| Extracts the client's IP address from the request. | |
| Handles common proxy headers (like X-Forwarded-For) if present. | |
| """ | |
| # Check for common proxy headers first | |
| x_forwarded_for = request.headers.get("x-forwarded-for") | |
| if x_forwarded_for: | |
| # The first IP is the client's IP | |
| return x_forwarded_for.split(',')[0].strip() | |
| # Fallback to the direct client host | |
| return request.client.host | |
| def get_ip_geolocation(ip_address: str): | |
| """Fetches geolocation data for a given IP address using ipinfo.io.""" | |
| # Using a free, public IP geolocation service. | |
| # Note: For production, consider rate limits and API keys. | |
| if ip_address in ["127.0.0.1", "::1"]: | |
| logger.warning(f"Skipping IP geolocation for local address: {ip_address}") | |
| return {"ip": ip_address, "error": "Local IP address, skipping external lookup."} | |
| try: | |
| # Use a timeout to prevent hanging | |
| response = requests.get(f"https://ipinfo.io/{ip_address}/json", timeout=5) | |
| response.raise_for_status() # Raise an exception for bad status codes | |
| data = response.json() | |
| return { | |
| "ip": data.get("ip"), | |
| "city": data.get("city"), | |
| "region": data.get("region"), | |
| "country": data.get("country"), | |
| "loc": data.get("loc"), # latitude,longitude | |
| "org": data.get("org"), | |
| } | |
| except requests.exceptions.RequestException as e: | |
| logger.error(f"Error fetching IP geolocation for {ip_address}: {e}") | |
| return {"ip": ip_address, "error": "Could not fetch IP geolocation data"} | |
| # --- Frontend HTML Content (to be served at /) --- | |
| # This is a simple, non-location-related UI as requested. | |
| html_content = """ | |
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>Seasons Greeting</title> | |
| <style> | |
| * { | |
| margin: 0; | |
| padding: 0; | |
| box-sizing: border-box; | |
| } | |
| body { | |
| font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; | |
| overflow-x: hidden; | |
| background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); | |
| min-height: 100vh; | |
| position: relative; | |
| } | |
| .snowflake { | |
| position: fixed; | |
| top: -10px; | |
| color: white; | |
| font-size: 1em; | |
| opacity: 0.8; | |
| animation: fall linear infinite; | |
| z-index: 1; | |
| } | |
| @keyframes fall { | |
| to { | |
| transform: translateY(100vh) rotate(360deg); | |
| } | |
| } | |
| .container { | |
| max-width: 1200px; | |
| margin: 0 auto; | |
| padding: 2rem; | |
| position: relative; | |
| z-index: 2; | |
| } | |
| .greeting-card { | |
| background: rgba(255, 255, 255, 0.95); | |
| border-radius: 30px; | |
| padding: 3rem; | |
| box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3); | |
| backdrop-filter: blur(10px); | |
| position: relative; | |
| overflow: hidden; | |
| animation: fadeInUp 1s ease; | |
| } | |
| @keyframes fadeInUp { | |
| from { | |
| opacity: 0; | |
| transform: translateY(30px); | |
| } | |
| to { | |
| opacity: 1; | |
| transform: translateY(0); | |
| } | |
| } | |
| .greeting-card::before { | |
| content: ''; | |
| position: absolute; | |
| top: -50%; | |
| right: -50%; | |
| width: 200%; | |
| height: 200%; | |
| background: linear-gradient(45deg, transparent, rgba(255, 255, 255, 0.1), transparent); | |
| transform: rotate(45deg); | |
| animation: shine 3s infinite; | |
| } | |
| @keyframes shine { | |
| 0%, 100% { transform: translateX(-100%) rotate(45deg); } | |
| 50% { transform: translateX(100%) rotate(45deg); } | |
| } | |
| .header { | |
| text-align: center; | |
| margin-bottom: 3rem; | |
| } | |
| .season-badge { | |
| display: inline-block; | |
| background: linear-gradient(135deg, #667eea, #764ba2); | |
| color: white; | |
| padding: 0.5rem 1.5rem; | |
| border-radius: 50px; | |
| font-size: 0.9rem; | |
| font-weight: 600; | |
| margin-bottom: 1rem; | |
| animation: pulse 2s ease-in-out infinite; | |
| } | |
| @keyframes pulse { | |
| 0%, 100% { transform: scale(1); } | |
| 50% { transform: scale(1.05); } | |
| } | |
| h1 { | |
| font-size: 3.5rem; | |
| background: linear-gradient(135deg, #667eea, #764ba2, #f093fb); | |
| -webkit-background-clip: text; | |
| -webkit-text-fill-color: transparent; | |
| background-clip: text; | |
| margin-bottom: 0.5rem; | |
| font-weight: 800; | |
| line-height: 1.2; | |
| } | |
| .sunday-text { | |
| font-size: 1.8rem; | |
| color: #555; | |
| font-weight: 300; | |
| margin-bottom: 1rem; | |
| } | |
| .date { | |
| color: #888; | |
| font-size: 1rem; | |
| font-weight: 500; | |
| } | |
| .content-grid { | |
| display: grid; | |
| grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); | |
| gap: 2rem; | |
| margin-top: 2rem; | |
| } | |
| .feature-card { | |
| background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%); | |
| padding: 2rem; | |
| border-radius: 20px; | |
| text-align: center; | |
| transition: transform 0.3s ease, box-shadow 0.3s ease; | |
| cursor: pointer; | |
| } | |
| .feature-card:hover { | |
| transform: translateY(-10px); | |
| box-shadow: 0 15px 40px rgba(0, 0, 0, 0.2); | |
| } | |
| .feature-card:nth-child(1) { | |
| background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%); | |
| } | |
| .feature-card:nth-child(2) { | |
| background: linear-gradient(135deg, #a1c4fd 0%, #c2e9fb 100%); | |
| } | |
| .feature-card:nth-child(3) { | |
| background: linear-gradient(135deg, #fbc2eb 0%, #a6c1ee 100%); | |
| } | |
| .icon { | |
| font-size: 3rem; | |
| margin-bottom: 1rem; | |
| } | |
| .feature-card h3 { | |
| color: #333; | |
| font-size: 1.4rem; | |
| margin-bottom: 0.5rem; | |
| font-weight: 700; | |
| } | |
| .feature-card p { | |
| color: #555; | |
| line-height: 1.6; | |
| font-size: 0.95rem; | |
| } | |
| .message-section { | |
| margin-top: 3rem; | |
| padding: 2.5rem; | |
| background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); | |
| border-radius: 20px; | |
| color: white; | |
| text-align: center; | |
| } | |
| .message-section h2 { | |
| font-size: 2rem; | |
| margin-bottom: 1rem; | |
| font-weight: 700; | |
| } | |
| .message-section p { | |
| font-size: 1.1rem; | |
| line-height: 1.8; | |
| opacity: 0.95; | |
| } | |
| .cta-button { | |
| display: inline-block; | |
| margin-top: 2rem; | |
| padding: 1rem 2.5rem; | |
| background: white; | |
| color: #667eea; | |
| text-decoration: none; | |
| border-radius: 50px; | |
| font-weight: 700; | |
| font-size: 1.1rem; | |
| transition: all 0.3s ease; | |
| box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2); | |
| } | |
| .cta-button:hover { | |
| transform: translateY(-3px); | |
| box-shadow: 0 15px 40px rgba(0, 0, 0, 0.3); | |
| } | |
| .footer { | |
| text-align: center; | |
| margin-top: 3rem; | |
| color: white; | |
| font-size: 0.9rem; | |
| opacity: 0.8; | |
| } | |
| @media (max-width: 768px) { | |
| .container { | |
| padding: 1rem; | |
| } | |
| .greeting-card { | |
| padding: 2rem 1.5rem; | |
| border-radius: 20px; | |
| } | |
| h1 { | |
| font-size: 2.5rem; | |
| } | |
| .sunday-text { | |
| font-size: 1.4rem; | |
| } | |
| .content-grid { | |
| grid-template-columns: 1fr; | |
| gap: 1.5rem; | |
| } | |
| .feature-card { | |
| padding: 1.5rem; | |
| } | |
| .message-section { | |
| padding: 2rem 1.5rem; | |
| } | |
| .message-section h2 { | |
| font-size: 1.5rem; | |
| } | |
| .message-section p { | |
| font-size: 1rem; | |
| } | |
| } | |
| @media (max-width: 480px) { | |
| h1 { | |
| font-size: 2rem; | |
| } | |
| .sunday-text { | |
| font-size: 1.2rem; | |
| } | |
| .icon { | |
| font-size: 2.5rem; | |
| } | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="container"> | |
| <div class="greeting-card"> | |
| <div class="header"> | |
| <div class="season-badge">β¨ Season's Greetings β¨</div> | |
| <h1>Happy Beautiful Sunday!</h1> | |
| <p class="sunday-text">Wishing you joy & peace</p> | |
| <p class="date">December 7, 2025</p> | |
| </div> | |
| <div class="content-grid"> | |
| <div class="feature-card"> | |
| <div class="icon">βοΈ</div> | |
| <h3>Bright Beginnings</h3> | |
| <p>Start your Sunday with positive energy and embrace the beauty of this wonderful day.</p> | |
| </div> | |
| <div class="feature-card"> | |
| <div class="icon">π</div> | |
| <h3>Peaceful Moments</h3> | |
| <p>Take time to relax, reflect, and enjoy the tranquility that Sundays bring.</p> | |
| </div> | |
| <div class="feature-card"> | |
| <div class="icon">π</div> | |
| <h3>Joyful Celebrations</h3> | |
| <p>Create memorable moments with loved ones and celebrate the gift of togetherness.</p> | |
| </div> | |
| </div> | |
| <div class="message-section"> | |
| <h2>May Your Day Be Filled With Wonder</h2> | |
| <p>On this beautiful Sunday, may you find peace in every moment, joy in every smile, and love in every heartbeat. Let the warmth of this season remind you of all the blessings that surround you.</p> | |
| <a href="#" class="cta-button">Share The Joy</a> | |
| </div> | |
| </div> | |
| <div class="footer"> | |
| <p>Made with β€οΈ | Have a Blessed Sunday</p> | |
| </div> | |
| </div> | |
| <script> | |
| // Create animated snowflakes | |
| function createSnowflake() { | |
| const snowflake = document.createElement('div'); | |
| snowflake.classList.add('snowflake'); | |
| snowflake.innerHTML = 'β'; | |
| snowflake.style.left = Math.random() * 100 + 'vw'; | |
| snowflake.style.animationDuration = Math.random() * 3 + 5 + 's'; | |
| snowflake.style.opacity = Math.random() * 0.6 + 0.3; | |
| snowflake.style.fontSize = Math.random() * 10 + 10 + 'px'; | |
| document.body.appendChild(snowflake); | |
| setTimeout(() => { | |
| snowflake.remove(); | |
| }, 8000); | |
| } | |
| setInterval(createSnowflake, 300); | |
| </script> | |
| </body> | |
| </html> | |
| """ | |
| # --- FastAPI Endpoints --- | |
| async def read_root(request: Request): | |
| """ | |
| Serves the HTML frontend and performs IP geolocation, logging the result. | |
| """ | |
| client_ip = get_client_ip(request) | |
| # 1. Perform IP Geolocation | |
| ip_geo_data = get_ip_geolocation(client_ip) | |
| # 2. Log IP Geolocation data to the console | |
| logger.info("--- IP Geolocation Data (Backend Only) ---") | |
| logger.info(f"Client IP: {client_ip}") | |
| for key, value in ip_geo_data.items(): | |
| logger.info(f"{key.capitalize()}: {value}") | |
| logger.info("-----------------------------------------") | |
| # Return the HTML content to the client | |
| return HTMLResponse(content=html_content, status_code=200) | |