Add site map

This commit is contained in:
2025-06-15 15:41:17 +01:00
parent 3b96c59bcf
commit 4e860d14e6
9 changed files with 83 additions and 1 deletions

View File

@@ -5,6 +5,15 @@ set -x -o pipefail
tag=$1
build=$2
cat <<EOF >src/.buildinfo.json
{
"tag": "${tag}:${build}",
"date": "$(date -I)",
"host": "$(hostname -f)",
"user": "${USER}"
}
EOF
docker build -t ${tag}:latest .
if [[ $build != "" ]]; then
docker build -t ${tag}:${build} .

View File

@@ -432,6 +432,14 @@ WSGIScriptAlias / /var/www/jc/projects.wsgi
Require all granted
</Directory>
Alias "/static" "/var/www/jc/static/"
<Directory /var/www/jc/static>
Order allow,Deny
Allow from all
</Directory>
Alias "/robots.txt" "/var/www/jc/static/robots.txt"
<Files jc.wsgi>
Require all granted
</Files>

9
debug_local.sh Executable file
View File

@@ -0,0 +1,9 @@
#!/bin/bash --noprofile
run() {
python3 src/projects.wsgi || run
}
export DISCORD_ERR_HOOK='dummy'
run
unset DISCORD_ERR_HOOK

6
src/.buildinfo.json Executable file
View File

@@ -0,0 +1,6 @@
{
"tag": "jc-ng-localtest:",
"date": "2025-06-15",
"host": "jake-e580",
"user": "jake"
}

View File

@@ -12,6 +12,7 @@ app = Flask(__name__)
# These imports need to come after our app is defined as they add routes to it.
import projects # pylint: disable=wrong-import-position,unused-import
import contact # pylint: disable=wrong-import-position,unused-import
import sitemap # pylint: disable=wrong-import-position,unused-import
class DiscordLogger(logging.Handler):
''' Simple logging handler to send a message to Discord '''

View File

@@ -45,7 +45,7 @@ def to_html(content: str) -> str:
''' Jninja filter to wrap markdown '''
return markdown(content)
def get_all_posts(directory: str) -> list:
def get_all_posts(directory: str = md_directory) -> list:
''' Get all posts in the posts directory '''
abs_paths = [path.join(directory, x) for x in glob(f'{directory}/*.md')]
return [frontmatter.load(x) for x in abs_paths]

View File

@@ -4,3 +4,6 @@ import sys
sys.path.append('/var/www/jc')
from index import app as application
if __name__ == '__main__':
application.run(debug=True)

42
src/sitemap.py Executable file
View File

@@ -0,0 +1,42 @@
#!/usr/bin/python3
import xml.etree.ElementTree as ET
import json
from flask import url_for, request, Response
from re import match
from index import app
from projects import get_all_posts
def get_routes() -> list:
routes = []
for rule in app.url_map.iter_rules():
if 0 >= len(rule.arguments):
url = url_for(rule.endpoint, **(rule.defaults or {}))
routes.append(url)
return routes
def get_build_date():
try:
with open('/var/www/jc/.buildinfo.json', encoding='utf8') as build:
build_json = json.load(build)
return build_json['date']
except:
return '1970-01-01'
@app.route('/sitemap.xml')
def sitemap():
date = get_build_date()
root = ET.Element('urlset', xmlns='http://www.sitemaps.org/schemas/sitemap/0.9')
base_url = match(r'^https?:\/\/.+:?\d*(?=\/)', request.base_url).group()
for route in get_routes():
url = ET.SubElement(root, 'url')
ET.SubElement(url, 'loc').text = base_url + route
ET.SubElement(url, 'lastmod').text = date
for article in get_all_posts():
if 'link' in article.metadata:
continue
url = ET.SubElement(root, 'url')
ET.SubElement(url, 'loc').text = f'{base_url}/projects/{article.metadata['id']}'
ET.SubElement(url, 'lastmod').text = article.metadata['date'].strftime('%Y-%m-%d')
return Response(ET.tostring(root, encoding='utf-8'), 200, {'content-type': 'application/xml'})

4
src/static/robots.txt Executable file
View File

@@ -0,0 +1,4 @@
User-agent: *
Allow: /
Sitemap: https://jakecharman.co.uk/sitemap.xml