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

Setup some kind of static website #35

Open
Kawacrepe opened this issue Aug 17, 2020 · 2 comments
Open

Setup some kind of static website #35

Kawacrepe opened this issue Aug 17, 2020 · 2 comments
Labels
enhancement New feature or request

Comments

@Kawacrepe
Copy link

Il serait intéressant de setup un site statique permettant d'afficher les .md d'une manière un peu plus sexy ;D
Ou même de le réaliser à la main pour les plus téméraires ?

@Julien00859
Copy link
Member

Pour ce que ça vaut, voila ce que j'ai sur mon propre site pour faire le rendu des fichiers markdown, exemple: https://docs.drlazor.be/python_ide.md

/etc/nginx/sites-available/docs

upstream markdown_renderer {
        server unix:/var/run/markdown_renderer.sock;
}
server {
        listen 0.0.0.0:80;
        listen [::]:80;
        server_name docs.drlazor.be;
        root /home/julien/Public;

        location / {
                autoindex on;
        }

        location ~ \.md$ {
                proxy_pass http://markdown_renderer;
        }
}

/etc/systemd/system/markdown_renderer.service

[Unit]
Description=HTTPD over UDS that renders markdown to HTML
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/markdown_renderer.py

[Install]
WantedBy=multi-user.target

/usr/bin/markdown_renderer.py

#!/usr/bin/env python3

"""HTTPD over UDS that render markdown files to HTML"""

from os import unlink, chmod
from http.server import SimpleHTTPRequestHandler
from signal import signal, SIGTERM, SIGINT
from socketserver import UnixStreamServer
from markdown import markdownFromFile
from threading import Thread


server_addr = "/var/run/markdown_renderer.sock"
base_dir = "/home/julien/Public"

html_pre = b"""<!DOCTYPE html5>
<head>
    <meta charset=utf-8>
    <style>
        body { font-family: sans-serif; }
        pre { background-color: #f5f5f5; }
    </style>
</head>
<body>
<header>
    <p>[<a href="/">index</a>]</p>
</header>
"""

html_post = b"""
</body>
</html>"""

class UnixStreamServer(UnixStreamServer):
    def __enter__(self, *args):
        return self

    def __exit__(self, *args):
        self.server_close()


class Handler(SimpleHTTPRequestHandler):
    def address_string(self):
        # UDS don't have addresses
        return ""

    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-Type", "text/html; charset=UTF-8")
        self.end_headers()
        self.wfile.write(html_pre)
        markdownFromFile(input="{}{}".format(base_dir, self.path),
                         output=self.wfile,
                         output_format="html5",
                         encoding="utf-8")
        self.wfile.write(html_post)

if __name__ == "__main__":
    with UnixStreamServer(server_addr, Handler) as httpd:
        chmod(server_addr, 0o733)
        signal(SIGTERM, lambda *_: Thread(target=httpd.shutdown).start())

        print("Listening for connections on", server_addr)
        try:
            httpd.serve_forever()
        except KeyboardInterrupt:
            pass

    print("Exiting...")
    unlink(server_addr)

@AntoineJT AntoineJT added the enhancement New feature or request label Aug 17, 2020
@AntoineJT
Copy link
Member

Merci pour l'issue, dès la création du dépôt notions, on y avait pensé.
Elle confirme que c'est une bonne idée. Je met ça en enhancement et je vais l'épingler, mais ce n'est pas prévu pour tout de suite.
Merci Lazor également pour le snippet.

@AntoineJT AntoineJT pinned this issue Aug 17, 2020
@Kawacrepe Kawacrepe changed the title Setup some kinds of static website Setup some kind of static website Aug 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants