#!/usr/bin/env python3
"""Serves the PWA POC on http://localhost:8080.

localhost is treated as a secure origin by Chromium, so the page is
installable without a TLS certificate.
"""
import http.server
import os
import socketserver

PORT = 8080
ROOT = os.path.dirname(os.path.abspath(__file__))


class Handler(http.server.SimpleHTTPRequestHandler):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, directory=ROOT, **kwargs)

    def end_headers(self):
        # Make sure the SW is allowed to control the whole origin.
        if self.path.endswith('sw.js'):
            self.send_header('Service-Worker-Allowed', '/')
            self.send_header('Content-Type', 'application/javascript')
        if self.path.endswith('manifest.json'):
            self.send_header('Content-Type', 'application/manifest+json')
        self.send_header('Cache-Control', 'no-store')
        super().end_headers()


if __name__ == '__main__':
    socketserver.TCPServer.allow_reuse_address = True
    with socketserver.TCPServer(('127.0.0.1', PORT), Handler) as httpd:
        print(f'Serving PWA POC on http://localhost:{PORT}/index.html')
        httpd.serve_forever()
