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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
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 ---

@app.get("/", response_class=HTMLResponse)
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)