import http.server
import socketserver
import os

class CORSHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
    def end_headers(self):
        self.send_header('Access-Control-Allow-Origin', '*')
        self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
        self.send_header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type')
        http.server.SimpleHTTPRequestHandler.end_headers(self)

def run_server(port, directory):
    """
    Run a simple HTTP server serving files from the specified directory with CORS enabled.
    """
    # Change the working directory to the specified directory
    os.makedirs(directory, exist_ok=True)
    os.chdir(directory)

    # Create the HTTP server using the CORS-enabled handler
    with socketserver.TCPServer(("", port), CORSHTTPRequestHandler) as httpd:
        print(f"Serving files at port {port} with CORS enabled")
        httpd.serve_forever()
