Skip to content

Commit

Permalink
feat: support parse config file with Variable Expansion for some secr…
Browse files Browse the repository at this point in the history
…et key
  • Loading branch information
zthxxx committed Jun 8, 2023
1 parent b54e836 commit 70716a1
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 11 deletions.
14 changes: 14 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Don't allow people to merge changes to these generated files, because the result
# may be invalid. You need to run "rush update" again.
pnpm-lock.yaml merge=binary
shrinkwrap.yaml merge=binary
npm-shrinkwrap.json merge=binary
yarn.lock merge=binary

# Rush's JSON config files use JavaScript-style code comments. The rule below prevents pedantic
# syntax highlighters such as GitHub's from highlighting these comments as errors. Your text editor
# may also require a special configuration to allow comments in JSON.
#
# For more information, see this issue: https://github.com/microsoft/rushstack/issues/1088
#
*.json linguist-language=JSON-with-Comments
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,11 @@ dist-ssr

# Editor directories and files
.vscode/*
!.vscode/extensions.json
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

.idea
.DS_Store
*.suo
Expand Down
4 changes: 2 additions & 2 deletions backend/dev.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/bin/bash
#!/usr/bin/env bash

# This script is used to run the development environment.

python3 -m venv venv

./venv/bin/python3 -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple install -r requirements-dev.txt
./venv/bin/python3 -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements-dev.txt

# install git-hooks for pre-commit before committing.
./venv/bin/pre-commit install
Expand Down
2 changes: 1 addition & 1 deletion backend/src/module/api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async def get_config(current_user=Depends(get_current_user)):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, detail="invalid token"
)
return settings
return settings.dict()


@router.post("/updateConfig")
Expand Down
5 changes: 5 additions & 0 deletions backend/src/module/database/orm/connector.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import sqlite3

from .delete import Delete
Expand All @@ -10,6 +11,10 @@

class Connector:
def __init__(self, table_name: str, data: dict, database: str = DATA_PATH):
# Create folder if not exists
if not os.path.exists(os.path.dirname(DATA_PATH)):
os.makedirs(os.path.dirname(DATA_PATH))

self._conn = sqlite3.connect(database)
self._cursor = self._conn.cursor()
self.update = Update(self, table_name, data)
Expand Down
43 changes: 36 additions & 7 deletions backend/src/module/models/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from os.path import expandvars
from pydantic import BaseModel, Field

# Sub config
Expand All @@ -12,21 +13,31 @@ class Program(BaseModel):
class Downloader(BaseModel):
type: str = Field("qbittorrent", description="Downloader type")
host: str = Field("172.17.0.1:8080", description="Downloader host")
username: str = Field("admin", description="Downloader username")
password: str = Field("adminadmin", description="Downloader password")
username_: str = Field("admin", alias="username", description="Downloader username")
password_: str = Field("adminadmin", alias="password", description="Downloader password")
path: str = Field("/downloads/Bangumi", description="Downloader path")
ssl: bool = Field(False, description="Downloader ssl")

@property
def username(self):
return expandvars(self.username_)

@property
def password(self):
return expandvars(self.password_)

class RSSParser(BaseModel):
enable: bool = Field(True, description="Enable RSS parser")
type: str = Field("mikan", description="RSS parser type")
token: str = Field("token", description="RSS parser token")
token_: str = Field("token", alias="token", description="RSS parser token")
custom_url: str = Field("mikanani.me", description="Custom RSS host url")
parser_type: str = Field("parser", description="Parser type")
filter: list[str] = Field(["720", r"\d+-\d"], description="Filter")
language: str = "zh"

@property
def token(self):
return expandvars(self.token_)

class BangumiManage(BaseModel):
enable: bool = Field(True, description="Enable bangumi manage")
Expand All @@ -45,16 +56,31 @@ class Proxy(BaseModel):
type: str = Field("http", description="Proxy type")
host: str = Field("", description="Proxy host")
port: int = Field(0, description="Proxy port")
username: str = Field("", description="Proxy username")
password: str = Field("", description="Proxy password")
username_: str = Field("", alias="username", description="Proxy username")
password_: str = Field("", alias="password", description="Proxy password")

@property
def username(self):
return expandvars(self.username_)

@property
def password(self):
return expandvars(self.password_)


class Notification(BaseModel):
enable: bool = Field(False, description="Enable notification")
type: str = Field("telegram", description="Notification type")
token: str = Field("", description="Notification token")
chat_id: str = Field("", description="Notification chat id")
token_: str = Field("", alias="token", description="Notification token")
chat_id_: str = Field("", alias="chat_id", description="Notification chat id")

@property
def token(self):
return expandvars(self.token_)

@property
def chat_id(self):
return expandvars(self.chat_id_)

class Config(BaseModel):
program: Program = Program()
Expand All @@ -64,3 +90,6 @@ class Config(BaseModel):
log: Log = Log()
proxy: Proxy = Proxy()
notification: Notification = Notification()

def dict(self, *args, by_alias=True, **kwargs):
return super().dict(*args, by_alias=by_alias, **kwargs)

0 comments on commit 70716a1

Please sign in to comment.