Coverage for zombie_nomnom_api/app.py: 100%

16 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-07 04:25 +0000

1""" 

2Module that contains the click entrypoint for our cli interface. 

3 

4We only handle a handful of configurations for out webserver. 

5""" 

6 

7from typing import Any 

8import re 

9import click 

10import uvicorn 

11 

12from .server import fastapi_app 

13 

14 

15class HostnameParameter(click.ParamType): 

16 def __init__(self) -> None: 

17 super().__init__() 

18 self.name = "hostname" 

19 

20 hostname_format = re.compile( 

21 r"^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$" 

22 ) 

23 

24 def convert( 

25 self, 

26 value: Any, 

27 param: click.Parameter | None, 

28 ctx: click.Context | None, 

29 ) -> Any: 

30 if not isinstance(value, str): 

31 value = str(value) 

32 if not self.hostname_format.match(value): 

33 self.fail("String value is not a valid hostname.") 

34 return value 

35 

36 

37@click.command() 

38@click.option("--port", "-p", type=int, default=5000) 

39@click.option("--host", "-h", type=HostnameParameter(), default="localhost") 

40@click.option("--worker-count", "-w", type=int, default=1) 

41def main(port: int, host: str, worker_count: int): # pragma: no cover 

42 uvicorn.run(fastapi_app, port=port, host=host, workers=worker_count) 

43 

44 

45if __name__ == "__main__": # pragma: no cover 

46 main()