import os
import redis
from http.server import BaseHTTPRequestHandler, HTTPServer
# Fetch Redis URI from environment variables
REDIS_URI = os.getenv("REDIS_URI")
# Initialize Redis client
def connect_redis():
try:
# Connect to Redis server using URI
redis_client = redis.from_url(REDIS_URI)
# Check connection by pinging Redis
redis_client.ping()
return True
except redis.ConnectionError:
return False
# HTTP request handler class
class RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
# Check if Redis is reachable
if connect_redis():
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write(b"Redis connection successful!")
else:
self.send_response(500)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write(b"Failed to connect to Redis")
# Run the server
def run(server_class=HTTPServer, handler_class=RequestHandler, port=8080):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print(f'Starting server on port {port}...')
httpd.serve_forever()
if __name__ == '__main__':
run()