import os
from pyramid.config import Configurator
from wsgiref.simple_server import WSGIRequestHandler
from src import LOG_DIR
import logging


log_path = os.path.join(LOG_DIR, "web_documentation.log")

# Replace the default StreamHandler with a FileHandler
logging.basicConfig(
    level=logging.INFO,
    handlers=[logging.FileHandler(log_path, encoding="utf-8")],
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)

logger = logging.getLogger(__name__)  # inherits the same file handler

docport = 8000


class QuietHandler(WSGIRequestHandler):
    # suppress “GET /…” access log
    def log_request(self, code="-", size="-"):
        logger.info("Request: {} {}".format(self.requestline, code))
        pass

    # suppress all other messages (errors, etc.)
    def log_message(self, fmt, *args):
        logger.error("Error: {}, Args: {}".format(fmt, args))
        pass


def website():
    config = Configurator()

    # Set up static file serving from the 'site/' directory
    config.add_static_view(
        name="/", path=os.path.join(os.getcwd(), "site"), cache_max_age=3600
    )

    app = config.make_wsgi_app()
    return app
