Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small python snippet to allow ppl serving archived myst htmls #7

Open
agahkarakuzu opened this issue Dec 9, 2024 · 0 comments
Open
Labels
documentation Improvements or additions to documentation

Comments

@agahkarakuzu
Copy link
Member

import http.server
import socketserver
import os

DIRECTORY= "/Users/agah/Downloads/LivingPreprint_10.55458_NeuroLibre_00031_33723c"
BASE_URL = "/10.55458/neurolibre.00031"

class CustomHandler(http.server.SimpleHTTPRequestHandler):
    def translate_path(self, path):
        # Remove the base URL prefix from the path
        if path.startswith(BASE_URL):
            path = path[len(BASE_URL):]
        # Serve files from the specified directory
        path = os.path.join(DIRECTORY, path.lstrip("/"))
        return path

    def do_GET(self):
        # Check if the requested file exists
        file_path = self.translate_path(self.path)
        if not os.path.exists(file_path):
            # If file doesn't exist, try appending `.html`
            file_path += ".html"
            if os.path.exists(file_path):
                # Update the path to point to the .html file
                self.path += ".html"
        # Call the parent class's GET handler
        super().do_GET()

# Set the port for the server
PORT = 8000

with socketserver.TCPServer(("", PORT), CustomHandler) as httpd:
    print(f"Serving at http://localhost:{PORT}{BASE_URL}")
    httpd.serve_forever()
@agahkarakuzu agahkarakuzu added the documentation Improvements or additions to documentation label Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant