feat: add prefix for env variables

This commit is contained in:
acgnhik 2023-03-19 15:23:39 +08:00
parent f8797de8e7
commit dba4c50437
10 changed files with 44 additions and 38 deletions

View File

@ -15,9 +15,9 @@ RUN apt-get update && \
apt-get purge -y --auto-remove build-essential python3-dev apt-get purge -y --auto-remove build-essential python3-dev
# ref: https://github.com/docker-library/python/issues/60#issuecomment-134322383 # ref: https://github.com/docker-library/python/issues/60#issuecomment-134322383
ENV DEFAULT_SETTINGS_FILE=/cfg/settings.toml ENV BLREC_DEFAULT_SETTINGS_FILE=/cfg/settings.toml
ENV DEFAULT_LOG_DIR=/log ENV BLREC_DEFAULT_LOG_DIR=/log
ENV DEFAULT_OUT_DIR=/rec ENV BLREC_DEFAULT_OUT_DIR=/rec
ENV TZ="Asia/Shanghai" ENV TZ="Asia/Shanghai"
EXPOSE 2233 EXPOSE 2233

View File

@ -17,9 +17,9 @@ RUN sed -i "s/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g" /etc/apt/sources.li
apt-get purge -y --auto-remove build-essential python3-dev apt-get purge -y --auto-remove build-essential python3-dev
# ref: https://github.com/docker-library/python/issues/60#issuecomment-134322383 # ref: https://github.com/docker-library/python/issues/60#issuecomment-134322383
ENV DEFAULT_SETTINGS_FILE=/cfg/settings.toml ENV BLREC_DEFAULT_SETTINGS_FILE=/cfg/settings.toml
ENV DEFAULT_LOG_DIR=/log ENV BLREC_DEFAULT_LOG_DIR=/log
ENV DEFAULT_OUT_DIR=/rec ENV BLREC_DEFAULT_OUT_DIR=/rec
ENV TZ="Asia/Shanghai" ENV TZ="Asia/Shanghai"
EXPOSE 2233 EXPOSE 2233

View File

@ -79,9 +79,9 @@
### 环境变量 ### 环境变量
- 默认设置文件位置: `ENV DEFAULT_SETTINGS_FILE=/cfg/settings.toml` - 默认设置文件位置: `ENV BLREC_DEFAULT_SETTINGS_FILE=/cfg/settings.toml`
- 默认日志存放目录: `ENV DEFAULT_LOG_DIR=/log` - 默认日志存放目录: `ENV BLREC_DEFAULT_LOG_DIR=/log`
- 默认录播存放目录: `ENV DEFAULT_OUT_DIR=/rec` - 默认录播存放目录: `ENV BLREC_DEFAULT_OUT_DIR=/rec`
- 默认时区: `ENV TZ="Asia/Shanghai"` - 默认时区: `ENV TZ="Asia/Shanghai"`
### 默认参数运行 ### 默认参数运行
@ -164,18 +164,18 @@ api key 可以使用数字和字母,长度限制为最短 8 最长 80。
作为 ASGI 应用运行,参数通过环境变量指定。 作为 ASGI 应用运行,参数通过环境变量指定。
- `config` 指定设置文件 - `BLREC_CONFIG` 指定设置文件
- `out_dir` 指定录播存放位置 - `BLREC_OUT_DIR` 指定录播存放位置
- `log_dir` 指定日志存放位置 - `BLREC_LOG_DIR` 指定日志存放位置
- `api_key` 指定 `api key` - `BLREC_API_KEY` 指定 `api key`
### bash ### bash
config=path/to/settings.toml out_dir=path/to/dir api_key=******** uvicorn blrec.web:app --host 0.0.0.0 --port 8000 BLREC_CONFIG=path/to/settings.toml BLREC_OUT_DIR=path/to/dir BLREC_API_KEY=******** uvicorn blrec.web:app --host 0.0.0.0 --port 8000
### cmd ### cmd
set config=D:\\path\\to\\config.toml & set out_dir=D:\\path\\to\\dir & set api_key=******** uvicorn blrec.web:app --host 0.0.0.0 --port 8000 set BLREC_CONFIG=D:\\path\\to\\config.toml & set BLREC_OUT_DIR=D:\\path\\to\\dir & set BLREC_API_KEY=******** uvicorn blrec.web:app --host 0.0.0.0 --port 8000
## Webhook ## Webhook

View File

@ -18,7 +18,7 @@ __all__ = 'AppApi', 'WebApi'
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
TRACE_API_REQ = bool(os.environ.get('TRACE_API_REQ')) TRACE_API_REQ = bool(os.environ.get('BLREC_TRACE_API_REQ'))
class BaseApi(ABC): class BaseApi(ABC):

View File

@ -54,13 +54,13 @@ def cli_main(
) -> None: ) -> None:
"""Bilibili live streaming recorder""" """Bilibili live streaming recorder"""
if config is not None: if config is not None:
os.environ['config'] = config os.environ['BLREC_CONFIG'] = config
if api_key is not None: if api_key is not None:
os.environ['api_key'] = api_key os.environ['BLREC_API_KEY'] = api_key
if out_dir is not None: if out_dir is not None:
os.environ['out_dir'] = out_dir os.environ['BLREC_OUT_DIR'] = out_dir
if log_dir is not None: if log_dir is not None:
os.environ['log_dir'] = log_dir os.environ['BLREC_LOG_DIR'] = log_dir
if root_path: if root_path:
if not root_path.startswith('/'): if not root_path.startswith('/'):

View File

@ -36,7 +36,7 @@ class SpaceReclaimer(SpaceEventListener, SwitchableMixin):
super().__init__() super().__init__()
self._space_monitor = space_monitor self._space_monitor = space_monitor
self.path = path self.path = path
if value := os.environ.get('REC_TTL'): if value := os.environ.get('BLREC_REC_TTL'):
try: try:
rec_ttl = int(value) rec_ttl = int(value)
except Exception as exc: except Exception as exc:

View File

@ -22,7 +22,7 @@ __all__ = ('sort',)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
TRACE_OP_SORT = bool(os.environ.get('TRACE_OP_SORT')) TRACE_OP_SORT = bool(os.environ.get('BLREC_TRACE_OP_SORT'))
def sort() -> Callable[[FLVStream], FLVStream]: def sort() -> Callable[[FLVStream], FLVStream]:

View File

@ -18,8 +18,8 @@ __all__ = ('SegmentRemuxer',)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
TRACE_REMUX_SEGMENT = bool(os.environ.get('TRACE_REMUX_SEGMENT')) TRACE_REMUX_SEGMENT = bool(os.environ.get('BLREC_TRACE_REMUX_SEGMENT'))
TRACE_LIBAV = bool(os.environ.get('TRACE_LIBAV')) TRACE_LIBAV = bool(os.environ.get('BLREC_TRACE_LIBAV'))
if TRACE_LIBAV: if TRACE_LIBAV:
logging.getLogger('libav').setLevel(5) logging.getLogger('libav').setLevel(5)
else: else:

View File

@ -55,7 +55,7 @@ __all__ = (
'PushdeerSettings', 'PushdeerSettings',
'PushplusSettings', 'PushplusSettings',
'TelegramSettings', 'TelegramSettings',
'BarkSettings' 'BarkSettings',
'NotifierSettings', 'NotifierSettings',
'NotificationSettings', 'NotificationSettings',
'EmailMessageTemplateSettings', 'EmailMessageTemplateSettings',
@ -74,19 +74,25 @@ __all__ = (
) )
DEFAULT_OUT_DIR: Final[str] = os.environ.get('DEFAULT_OUT_DIR', '.') DEFAULT_OUT_DIR: Final[str] = os.environ.get('BLREC_DEFAULT_OUT_DIR', '.')
DEFAULT_LOG_DIR: Final[str] = os.environ.get('DEFAULT_LOG_DIR', '~/.blrec/logs/') DEFAULT_LOG_DIR: Final[str] = os.environ.get('BLREC_DEFAULT_LOG_DIR', '~/.blrec/logs/')
DEFAULT_SETTINGS_FILE: Final[str] = os.environ.get( DEFAULT_SETTINGS_FILE: Final[str] = os.environ.get(
'DEFAULT_SETTINGS_FILE', '~/.blrec/settings.toml' 'BLREC_DEFAULT_SETTINGS_FILE', '~/.blrec/settings.toml'
) )
class EnvSettings(BaseSettings): class EnvSettings(BaseSettings):
settings_file: Annotated[str, Field(env='config')] = DEFAULT_SETTINGS_FILE settings_file: Annotated[str, Field(env='BLREC_CONFIG')] = DEFAULT_SETTINGS_FILE
out_dir: Optional[str] = None out_dir: Annotated[Optional[str], Field(env='BLREC_OUT_DIR')] = None
log_dir: Optional[str] = None log_dir: Annotated[Optional[str], Field(env='BLREC_LOG_DIR')] = None
api_key: Annotated[ api_key: Annotated[
Optional[str], Field(min_length=8, max_length=80, regex=r'[a-zA-Z\d\-]{8,80}'), Optional[str],
Field(
env='BLREC_API_KEY',
min_length=8,
max_length=80,
regex=r'[a-zA-Z\d\-]{8,80}',
),
] = None ] = None
class Config: class Config:
@ -420,10 +426,13 @@ class TelegramSettings(BaseModel):
@validator('server') @validator('server')
def _validate_server(cls, value: str) -> str: def _validate_server(cls, value: str) -> str:
if value != '' and not re.fullmatch(r'^https?:\/\/[a-zA-Z0-9-_.]+(:[0-9]+)?', value): if value != '' and not re.fullmatch(
r'^https?:\/\/[a-zA-Z0-9-_.]+(:[0-9]+)?', value
):
raise ValueError('server is invalid') raise ValueError('server is invalid')
return value return value
class BarkSettings(BaseModel): class BarkSettings(BaseModel):
server: str = '' server: str = ''
pushkey: str = '' pushkey: str = ''
@ -600,10 +609,7 @@ class TelegramNotificationSettings(
class BarkNotificationSettings( class BarkNotificationSettings(
BarkSettings, BarkSettings, NotifierSettings, NotificationSettings, BarkMessageTemplateSettings
NotifierSettings,
NotificationSettings,
BarkMessageTemplateSettings,
): ):
pass pass

View File

@ -146,7 +146,7 @@ class SupportDebugMixin(ABC):
super().__init__() super().__init__()
def _init_for_debug(self, room_id: int) -> None: def _init_for_debug(self, room_id: int) -> None:
if (value := os.environ.get('DEBUG')) and ( if (value := os.environ.get('BLREC_DEBUG')) and (
value == '*' or room_id in value.split(',') value == '*' or room_id in value.split(',')
): ):
self._debug = True self._debug = True