add --config CLI parameter
This commit is contained in:
parent
1acb693e2b
commit
ab804354de
@ -1,5 +1,8 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import dataclasses
|
||||||
|
|
||||||
import hypercorn.config
|
import hypercorn.config
|
||||||
import hypercorn.trio.run
|
import hypercorn.trio.run
|
||||||
import hypercorn.utils
|
import hypercorn.utils
|
||||||
@ -19,8 +22,21 @@ def run(config: hypercorn.config.Config) -> None:
|
|||||||
sock.close()
|
sock.close()
|
||||||
|
|
||||||
|
|
||||||
|
@dataclasses.dataclass
|
||||||
|
class CliArguments:
|
||||||
|
config: str | None
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument('--config', '-c')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
self.config = args.config
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
_config = capport.config.Config.load_default_once()
|
args = CliArguments()
|
||||||
|
_config = capport.config.Config.load_default_once(filename=args.config)
|
||||||
|
|
||||||
hypercorn_config = hypercorn.config.Config()
|
hypercorn_config = hypercorn.config.Config()
|
||||||
hypercorn_config.application_path = 'capport.api.app'
|
hypercorn_config.application_path = 'capport.api.app'
|
||||||
|
@ -12,6 +12,7 @@ _cached_config: typing.Optional[Config] = None
|
|||||||
|
|
||||||
@dataclasses.dataclass
|
@dataclasses.dataclass
|
||||||
class Config:
|
class Config:
|
||||||
|
_source_filename: str
|
||||||
controllers: typing.List[str]
|
controllers: typing.List[str]
|
||||||
server_names: typing.List[str]
|
server_names: typing.List[str]
|
||||||
comm_secret: str
|
comm_secret: str
|
||||||
@ -21,10 +22,14 @@ class Config:
|
|||||||
debug: bool
|
debug: bool
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def load_default_once() -> Config:
|
def load_default_once(filename: typing.Optional[str] = None) -> Config:
|
||||||
global _cached_config
|
global _cached_config
|
||||||
if not _cached_config:
|
if not _cached_config:
|
||||||
_cached_config = Config.load()
|
_cached_config = Config.load(filename)
|
||||||
|
elif not filename is None and filename != _cached_config._source_filename:
|
||||||
|
raise RuntimeError(
|
||||||
|
f"Already loaded different config from {_cached_config._source_filename!r} instead of {filename!r}",
|
||||||
|
)
|
||||||
return _cached_config
|
return _cached_config
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@ -38,6 +43,7 @@ class Config:
|
|||||||
data = yaml.safe_load(f)
|
data = yaml.safe_load(f)
|
||||||
controllers = list(map(str, data['controllers']))
|
controllers = list(map(str, data['controllers']))
|
||||||
return Config(
|
return Config(
|
||||||
|
_source_filename=filename,
|
||||||
controllers=controllers,
|
controllers=controllers,
|
||||||
server_names=data.get('server-names', []),
|
server_names=data.get('server-names', []),
|
||||||
comm_secret=str(data.get('comm-secret', None) or data['secret']),
|
comm_secret=str(data.get('comm-secret', None) or data['secret']),
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import dataclasses
|
||||||
import typing
|
import typing
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
@ -61,8 +63,21 @@ async def amain(config: capport.config.Config) -> None:
|
|||||||
await sn.send('STATUS=Kernel fully synchronized')
|
await sn.send('STATUS=Kernel fully synchronized')
|
||||||
|
|
||||||
|
|
||||||
|
@dataclasses.dataclass
|
||||||
|
class CliArguments:
|
||||||
|
config: str | None
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument('--config', '-c')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
self.config = args.config
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
config = capport.config.Config.load_default_once()
|
args = CliArguments()
|
||||||
|
config = capport.config.Config.load_default_once(filename=args.config)
|
||||||
capport.utils.cli.init_logger(config)
|
capport.utils.cli.init_logger(config)
|
||||||
try:
|
try:
|
||||||
trio.run(amain, config)
|
trio.run(amain, config)
|
||||||
|
Loading…
Reference in New Issue
Block a user