Module enrgdaq.daq.jobs.serve_http

Classes

class DAQJobServeHTTP (config: Any,
supervisor_config: SupervisorConfig | None = None)
Expand source code
class DAQJobServeHTTP(DAQJob):
    """
    DAQ job to serve HTTP requests.

    Can be used to serve files from a specified path, primarily for CSV files.

    Handles placeholders in the path, such as "{TODAY}" and "{YESTERDAY}".

    Attributes:
        config_type (type): The configuration class type.
        config (DAQJobServeHTTPConfig): The configuration instance.
    """

    config_type = DAQJobServeHTTPConfig
    config: DAQJobServeHTTPConfig

    def start(self):
        # Start a BasicHTTPServer in this thread
        serve_path = self.config.serve_path

        class ThreadingHTTPServer(ForkingMixIn, http.server.HTTPServer):  # type: ignore
            pass

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

            def do_GET(self) -> None:
                """
                Handle GET requests and replace placeholders in the path.
                """
                REPLACE_DICT = {
                    "TODAY": datetime.now().strftime("%Y-%m-%d"),
                    "YESTERDAY": (datetime.now() - timedelta(days=1)).strftime(
                        "%Y-%m-%d"
                    ),
                }
                for key, value in REPLACE_DICT.items():
                    self.path = self.path.replace(key, value)

                return super().do_GET()

            def log_message(self, format: str, *args) -> None:
                """
                Override to suppress logging.
                """
                pass

        def start_server():
            """
            Start the HTTP server and serve requests indefinitely.
            """
            with ThreadingHTTPServer(
                (self.config.host, self.config.port), Handler
            ) as httpd:
                self._logger.info(
                    f"Serving at port {self.config.host}:{self.config.port}"
                )
                httpd.serve_forever()

        thread = threading.Thread(target=start_server, daemon=True)
        thread.start()
        thread.join()

DAQ job to serve HTTP requests.

Can be used to serve files from a specified path, primarily for CSV files.

Handles placeholders in the path, such as "{TODAY}" and "{YESTERDAY}".

Attributes

config_type : type
The configuration class type.
config : DAQJobServeHTTPConfig
The configuration instance.

Ancestors

Class variables

var configDAQJobServeHTTPConfig
var config_type : Any

Configuration class for DAQJobServeHTTP.

Attributes

serve_path : str
The path to serve files from.
host : str
The host address to bind the server to.
port : int
The port number to bind the server to.

Methods

def start(self)
Expand source code
def start(self):
    # Start a BasicHTTPServer in this thread
    serve_path = self.config.serve_path

    class ThreadingHTTPServer(ForkingMixIn, http.server.HTTPServer):  # type: ignore
        pass

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

        def do_GET(self) -> None:
            """
            Handle GET requests and replace placeholders in the path.
            """
            REPLACE_DICT = {
                "TODAY": datetime.now().strftime("%Y-%m-%d"),
                "YESTERDAY": (datetime.now() - timedelta(days=1)).strftime(
                    "%Y-%m-%d"
                ),
            }
            for key, value in REPLACE_DICT.items():
                self.path = self.path.replace(key, value)

            return super().do_GET()

        def log_message(self, format: str, *args) -> None:
            """
            Override to suppress logging.
            """
            pass

    def start_server():
        """
        Start the HTTP server and serve requests indefinitely.
        """
        with ThreadingHTTPServer(
            (self.config.host, self.config.port), Handler
        ) as httpd:
            self._logger.info(
                f"Serving at port {self.config.host}:{self.config.port}"
            )
            httpd.serve_forever()

    thread = threading.Thread(target=start_server, daemon=True)
    thread.start()
    thread.join()

Inherited members

class DAQJobServeHTTPConfig (serve_path: str,
host: str,
port: int,
*,
verbosity: LogVerbosity = LogVerbosity.INFO,
remote_config: DAQRemoteConfig | None = <factory>,
daq_job_type: str)
Expand source code
class DAQJobServeHTTPConfig(DAQJobConfig):
    """
    Configuration class for DAQJobServeHTTP.

    Attributes:
        serve_path (str): The path to serve files from.
        host (str): The host address to bind the server to.
        port (int): The port number to bind the server to.
    """

    serve_path: str
    host: str
    port: int

Configuration class for DAQJobServeHTTP.

Attributes

serve_path : str
The path to serve files from.
host : str
The host address to bind the server to.
port : int
The port number to bind the server to.

Ancestors

Instance variables

var host : str
Expand source code
class DAQJobServeHTTPConfig(DAQJobConfig):
    """
    Configuration class for DAQJobServeHTTP.

    Attributes:
        serve_path (str): The path to serve files from.
        host (str): The host address to bind the server to.
        port (int): The port number to bind the server to.
    """

    serve_path: str
    host: str
    port: int
var port : int
Expand source code
class DAQJobServeHTTPConfig(DAQJobConfig):
    """
    Configuration class for DAQJobServeHTTP.

    Attributes:
        serve_path (str): The path to serve files from.
        host (str): The host address to bind the server to.
        port (int): The port number to bind the server to.
    """

    serve_path: str
    host: str
    port: int
var serve_path : str
Expand source code
class DAQJobServeHTTPConfig(DAQJobConfig):
    """
    Configuration class for DAQJobServeHTTP.

    Attributes:
        serve_path (str): The path to serve files from.
        host (str): The host address to bind the server to.
        port (int): The port number to bind the server to.
    """

    serve_path: str
    host: str
    port: int