feat: use pathlib for HTTP handler
This commit is contained in:
parent
8e9f1d80b4
commit
1cf39ad10b
2 changed files with 9 additions and 11 deletions
|
@ -2,6 +2,7 @@ import logging
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
from typing import Any, Optional
|
from typing import Any, Optional
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
import tomllib
|
import tomllib
|
||||||
|
|
||||||
|
@ -13,7 +14,7 @@ class Config:
|
||||||
certificates: list[str]
|
certificates: list[str]
|
||||||
domains: dict[str, str]
|
domains: dict[str, str]
|
||||||
secrets: dict[str, str]
|
secrets: dict[str, str]
|
||||||
acme_path: str
|
acme_path: Path
|
||||||
|
|
||||||
def find_zone(self, domain: str) -> str:
|
def find_zone(self, domain: str) -> str:
|
||||||
parts = domain.split('.')
|
parts = domain.split('.')
|
||||||
|
@ -74,9 +75,9 @@ def read_config(path: Optional[str]) -> Config:
|
||||||
c.post_acquire = post_acquire
|
c.post_acquire = post_acquire
|
||||||
|
|
||||||
if 'acme_path' in raw_conf:
|
if 'acme_path' in raw_conf:
|
||||||
c.acme_path = raw_conf['acme_path']
|
c.acme_path = Path(raw_conf['acme_path'])
|
||||||
else:
|
else:
|
||||||
c.acme_path = '/var/www/acme/.well-known/acme-challenge'
|
c.acme_path = Path('/var/www/acme/.well-known/acme-challenge')
|
||||||
|
|
||||||
if 'certificates' not in raw_conf:
|
if 'certificates' not in raw_conf:
|
||||||
log.error('missing "certificates"')
|
log.error('missing "certificates"')
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
import os
|
|
||||||
|
|
||||||
from ..config import Config
|
from ..config import Config
|
||||||
from .base import Handler
|
from .base import Handler
|
||||||
|
|
||||||
|
@ -7,12 +5,11 @@ from .base import Handler
|
||||||
class HTTPHandler(Handler):
|
class HTTPHandler(Handler):
|
||||||
def __init__(self, zone: str, config: Config, token: str) -> None:
|
def __init__(self, zone: str, config: Config, token: str) -> None:
|
||||||
super().__init__(zone, config, token)
|
super().__init__(zone, config, token)
|
||||||
self.filepath = os.path.join(config.acme_path, token)
|
self.filepath = config.acme_path / token
|
||||||
|
|
||||||
def create(self, record_name: str, record_value: str) -> None:
|
def create(self, _: str, record_value: str) -> None:
|
||||||
with open(self.filepath, 'w') as f:
|
with self.filepath.open('w') as f:
|
||||||
f.write(record_value)
|
f.write(record_value)
|
||||||
|
|
||||||
def remove(self, record_name: str) -> None:
|
def remove(self, _: str) -> None:
|
||||||
if os.path.isfile(self.filepath):
|
self.filepath.unlink(missing_ok=True)
|
||||||
os.unlink(self.filepath)
|
|
||||||
|
|
Loading…
Reference in a new issue