Skip to content

Commit

Permalink
add pages serverconfig implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
OriHoch committed Sep 15, 2022
1 parent 41e2f92 commit 59f2e7d
Show file tree
Hide file tree
Showing 10 changed files with 615 additions and 11 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ jobs:
uses: actions/checkout@v3
- name: Setup Pages
uses: actions/configure-pages@v2
- run: |
pip install -r requirements.txt &&\
python3 -m pages_src.generate
- name: Upload artifact
uses: actions/upload-pages-artifact@v1
with:
# Upload entire repository
path: 'pages'
path: '.data/pages'
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v1
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ __pycache__
.pytest_cache
.cloudcli
.env
.data
12 changes: 12 additions & 0 deletions PAGES.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Github Pages

The content is generated using Python with Jinja2 templates in `pages-source` directory
and served via GitHub pages from `pages/` directory.

To generate the content:

* Install requirements: `pip install -r requirements.txt`
* Generate html for local development and watch for changes `python -m pages_src.generate --dev`

Start a local web server to test by running `python -m http.server -d .data/pages`
and then visiting `http://localhost:8000`.
9 changes: 0 additions & 9 deletions pages/index.html

This file was deleted.

Empty file added pages_src/__init__.py
Empty file.
74 changes: 74 additions & 0 deletions pages_src/generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import os
import sys
import time

import jinja2
import requests
import watchdog.events
import watchdog.observers


CALCULATOR_JS_PHP_URL = "https://console.kamatera.com/info/calculator.js.php"


def generate(calculator_js_php_url=CALCULATOR_JS_PHP_URL):
env = jinja2.Environment(
loader=jinja2.PackageLoader("pages_src", "templates"),
autoescape=jinja2.select_autoescape()
)
with open(".data/pages/index.html", "w") as f:
f.write(env.get_template("index.html").render())
with open(".data/pages/serverconfiggen.html", "w") as f:
f.write(env.get_template("serverconfiggen.html").render(
calculator_js_php_url=calculator_js_php_url
))
print('OK')


class EventHandler(watchdog.events.FileSystemEventHandler):

def __init__(self):
super().__init__()
self.modified_src_paths = set()


def on_any_event(self, event):
if not event.is_directory and not event.src_path.endswith('~'):
if event.event_type != watchdog.events.EVENT_TYPE_CLOSED:
self.modified_src_paths.add(event.src_path)


def dev():
if not os.path.exists(".data/pages/calculator.js.php"):
res = requests.get(CALCULATOR_JS_PHP_URL)
res.raise_for_status()
with open(".data/pages/calculator.js.php", "wb") as f:
f.write(res.content)
generate("calculator.js.php")
event_handler = EventHandler()
observer = watchdog.observers.Observer()
observer.schedule(event_handler, path='pages_src', recursive=True)
observer.start()
print("Watching pages_src for changes...")
try:
while True:
time.sleep(0.1)
if len(event_handler.modified_src_paths) > 0:
print(f'Regenerating, changes detected in {event_handler.modified_src_paths}')
event_handler.modified_src_paths = set()
generate("calculator.js.php")
except KeyboardInterrupt:
observer.stop()
observer.join()


def main(*args):
os.makedirs(".data/pages", exist_ok=True)
if '--dev' in args:
dev()
else:
generate()


if __name__ == "__main__":
main(*sys.argv[1:])
10 changes: 10 additions & 0 deletions pages_src/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>{% block head %}
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-iYQeCzEYFbKjA/T2uDLTpkwGzCiq6soy8tYaI1GyVh/UjpbCx/TYkiZhlZB6+fzT" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-u1OknCvxWvY5kfmNBILK2hRnQC3Pr17a+RTT6rIHI7NnikvbZlHgTPOOmMi466C8" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<title>Kamatera Toolbox</title>
{% endblock %}</head>
<body>{% block body %}{% endblock %}</body>
</html>
6 changes: 6 additions & 0 deletions pages_src/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends "base.html" %}
{% block body %}
<ul class="list-group">
<li class="list-group-item"><a href="serverconfiggen.html">Configuration Generator</a></li>
</ul>
{% endblock %}
Loading

0 comments on commit 59f2e7d

Please sign in to comment.