Skip to content

Commit

Permalink
Make server.py work except for showing content updates on collection …
Browse files Browse the repository at this point in the history
…pages.
  • Loading branch information
yaph committed Feb 4, 2020
1 parent e88f963 commit bb65aaf
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions logya/server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# TODOs
# Use watchgod to keep track of changes in `static` and `content` dirs.
# Watch for changes in `static` and `content` dirs.
# New and changed `static` files are copied to `public`.
# New files in `content` that have an allowed extension and not set to `noindex` result in a full rebuild of the index.
# In `do_GET` only update `content` and generated `index` pages.
Expand All @@ -11,7 +11,18 @@
from shutil import copyfile
from urllib.parse import unquote, urlparse

from logya.util import paths
from logya.core import Logya
from logya.content import add_collections, read, read_all, write_collection
from logya.writer import DocWriter
from logya.util import paths, config


site_index = read_all(paths, config)
add_collections(site_index, config)
L = Logya()
L.init_env()
L.build_index()
L.template.vars['debug'] = True


class HTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
Expand All @@ -21,15 +32,13 @@ def __init__(self, *args):
super(HTTPRequestHandler, self).__init__(*args, directory=paths.public.as_posix())

def do_GET(self):
"""Return refreshed resource."""

update_resource(self.path)
super(HTTPRequestHandler, self).do_GET()


def update_resource(url_path):
def update_resource(url):
# Use only the actual path and ignore possible query params issue #3.
src_url = unquote(urlparse(url_path).path)
src_url = unquote(urlparse(url).path)
src_name = src_url.lstrip('/')

# If a static file is requested update it and return.
Expand All @@ -42,8 +51,32 @@ def update_resource(url_path):
copyfile(src_static, dst_static)
return True

# Rebuild index for unknown HTML file requests.
if src_url not in site_index and url.endswith(('/', '.html', '.htm')):
site_index.update(read_all(paths, config))
add_collections(site_index, config)
if src_url not in site_index:
print(f'No content or collection at: {src_url}')
return

content = site_index[src_url]
# Update content document
if 'doc' in content:
doc = read(content['path'], paths, config)
#content['doc'].update(doc)
DocWriter(paths.public, L.template).write(doc, L.get_doc_template(doc))
print(f'Refreshed doc at URL: {url}')
return
# Update collection page
if 'docs' in content:
# FIXME does not show changes made to docs in collection
path = Path(paths.public, src_name, 'index.html')
write_collection(path, content, L.template, config)
print(f'Refreshed collection: {url}')


def serve(args):
L.template.vars['base_url'] = f'http://{args.host}:{args.port}'
with socketserver.TCPServer((args.host, args.port), HTTPRequestHandler) as httpd:
print(f'Serving on http://{args.host}:{args.port}/')
print(f'Serving on {L.template.vars["base_url"]}')
httpd.serve_forever()

0 comments on commit bb65aaf

Please sign in to comment.