# -*- coding: utf-8 -*-
"""酱腌菜电商看板 HTTP 服务（no-cache 版）
替代 python -m http.server，所有响应附带 Cache-Control: no-store，
确保浏览器每次刷新都拿到最新 dashboard.html 与 data/*.json。
用法: python serve_board.py [port]
"""
import http.server
import socketserver
import sys
import os

DIR = r"D:\doubao\tmp\酱腌菜电商看板"
PORT = 8000
if len(sys.argv) > 1:
    PORT = int(sys.argv[1])


class NoCacheHandler(http.server.SimpleHTTPRequestHandler):
    def __init__(self, *a, **kw):
        super().__init__(*a, directory=DIR, **kw)

    def end_headers(self):
        self.send_header('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')
        self.send_header('Pragma', 'no-cache')
        self.send_header('Expires', '0')
        super().end_headers()

    def log_message(self, fmt, *args):
        sys.stdout.write("[%s] %s\n" % (self.log_date_time_string(), fmt % args))
        sys.stdout.flush()


class BoardServer(socketserver.ThreadingTCPServer):
    allow_reuse_address = True
    daemon_threads = True


def main():
    print("board server on 0.0.0.0:%d (no-cache) dir=%s" % (PORT, DIR), flush=True)
    with BoardServer(("0.0.0.0", PORT), NoCacheHandler) as httpd:
        httpd.serve_forever()


if __name__ == "__main__":
    main()
