From 802fad1325647a1e842dcfbdcaff3a391ac3b04c Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Sat, 16 Mar 2024 22:44:07 +0100
Subject: [PATCH] Add Jinja2 templating, and requirements.txt infrastructure.

---
 requirements.txt                 | 1 +
 scripts/setup_dev_environment.sh | 6 +++++-
 task.py                          | 5 ++++-
 templates/base.html              | 8 ++++++++
 templates/index.html             | 5 +++++
 5 files changed, 23 insertions(+), 2 deletions(-)
 create mode 100644 requirements.txt
 create mode 100644 templates/base.html
 create mode 100644 templates/index.html

diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..cd1737d
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1 @@
+Jinja2==3.1.3
diff --git a/scripts/setup_dev_environment.sh b/scripts/setup_dev_environment.sh
index 8ea3a44..32848d9 100755
--- a/scripts/setup_dev_environment.sh
+++ b/scripts/setup_dev_environment.sh
@@ -3,7 +3,11 @@ set -e
 
 if [ ! -d '.git' ]; then
   echo "Cannot find .git/, please run me from your cloned repo's top directory!" 
-elif [ ! -f '.git/hooks/pre-commit' ]; then
+fi
+
+pip install -r requirements.txt
+
+if [ ! -f '.git/hooks/pre-commit' ]; then
   echo 'Linking pre-commit hook.' 
   cd .git/hooks/
   ln -s ../../scripts/pre-commit
diff --git a/task.py b/task.py
index a1510b0..2a6c699 100755
--- a/task.py
+++ b/task.py
@@ -4,8 +4,10 @@ plom's task manager
 """
 from http.server import BaseHTTPRequestHandler
 from http.server import HTTPServer
+from jinja2 import Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader
 
 HTTP_PORT = 8082
+TEMPLATES_DIR = 'templates'
 
 
 class HandledException(Exception):
@@ -19,13 +21,14 @@ class TaskHandler(BaseHTTPRequestHandler):
         """Handle any GET request."""
         self.send_response(200)
         self.end_headers()
-        html = '<html><body><p>hi there!</p></body></html>'
+        html = self.server.jinja.get_template('index.html').render()
         self.wfile.write(bytes(html, 'utf-8'))
 
 
 def main():
     """Main loop."""
     server = HTTPServer(('localhost', HTTP_PORT), TaskHandler)
+    server.jinja = JinjaEnv(loader=JinjaFSLoader(TEMPLATES_DIR))
     print(f'running at port {HTTP_PORT}')
     try:
         server.serve_forever()
diff --git a/templates/base.html b/templates/base.html
new file mode 100644
index 0000000..508281e
--- /dev/null
+++ b/templates/base.html
@@ -0,0 +1,8 @@
+<!DOCTYPE html>
+<html>
+<meta charset="UTF-8">
+<body>
+{% block content %}
+{% endblock %}
+</body>
+</html>
diff --git a/templates/index.html b/templates/index.html
new file mode 100644
index 0000000..72d81d4
--- /dev/null
+++ b/templates/index.html
@@ -0,0 +1,5 @@
+{% extends 'base.html' %}
+
+{% block content %}
+Hi there!
+{% endblock %}
-- 
2.30.2