Repository navigation
Expand file tree
/
Copy pathconfig.py
More file actions
132 lines (113 loc) · 3.7 KB
/
Copy pathconfig.py
File metadata and controls
132 lines (113 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project :WebBookList
@IDE :PyCharm
@Author :TyrantBlue
@Date :2025/7/9 16:55
"""
import os.path
from typing import List
from pydantic_settings import BaseSettings, SettingsConfigDict
from typing import Dict, Any
from pydantic import SecretStr, BaseModel
# BASE_DIR = Path(__file__).parent
# # 定义.env文件的绝对路径
# ENV_FILE_PATH = BASE_DIR / ".env"
class APIConfig(BaseSettings):
# 调试模式
APP_DEBUG: bool = True
# 项目信息
VERSION: str = "0.0.1"
PROJECT_NAME: str = "WebBookList"
PROJECT_DESCRIPTION: str = "WebBookList"
# 静态资源目录
STATIC_DIR: str = os.path.join(os.getcwd(), "static")
# 视图目录
# TEMPLATES_DIR: str = os.path.join(os.getcwd(), "templates")
# 跨域请求
CORS_ORIGINS: List[str] = ["*"]
CORS_ALLOW_CREDENTIALS: bool = True
CORS_ALLOW_METHODS: List[str] = ["*"]
CORS_ALLOW_HEADERS: List[str] = ["*"]
# session
SESSION_SECRET_KEY: str = "YOUR SESSION KEY."
SESSION_SESSION_COOKIE: str = "session"
SESSION_MAX_AGE: int = 10
# token
TOKEN_SECRET_KEY: str = "test_secret_key"
TOKEN_ALGORITHM: str = "HS256"
TOKEN_EXP: int = 14 * 24 * 60 * 60
class RedisSettings(BaseSettings):
"""
redis设置类
"""
model_config = SettingsConfigDict(env_prefix='REDIS_', extra="ignore")
host: str
port: int
encoding: str = 'utf-8'
username: str = ''
password: SecretStr = SecretStr('')
db: int = 0
decode_responses: bool = True
def redis_config(self, db: int | None = None) -> Dict[str, Any]:
"""
获取redis的连接设置
:db: 数据库名
:return:
"""
if db is None:
db = self.db
return {
"url": f"redis://{self.username}:{self.password.get_secret_value()}"
f"@{self.host}:{self.port}"
f"/{db}",
"decode_responses": self.decode_responses
}
class DatabaseSettings(BaseModel):
"""
database 设置类
"""
host: str
port: int
username: str
password: SecretStr
database: str
class TortoiseSettings(BaseSettings):
# 指定环境文件的来源
model_config = SettingsConfigDict(extra="ignore", env_nested_delimiter='_')
db_postgres: DatabaseSettings
# 多库配置
# db_mysql: DatabaseSettings
tortoise_apps: Dict[str, Any] = {
"main_model": {
"models": ["models"],
"default_connection": "db_postgres"
},
# "sec_model": {
# "models": ["models"],
# "default_connection": "db_mysql"
# },
}
use_timezone: bool = False
timezone: str = "Asia/Shanghai"
@property
def tortoise_orm_config(self) -> Dict[str, Any]:
return {
"connections": {
"db_postgres": f"postgres://{self.db_postgres.username}:{self.db_postgres.password.get_secret_value()}"
f"@{self.db_postgres.host}:{self.db_postgres.port}"
f"/{self.db_postgres.database}",
# "db_mysql": f"mysql://{self.db_mysql.username}:{self.db_mysql.password.get_secret_value()}"
# f"@{self.db_mysql.host}:{self.db_mysql.port}"
# f"/{self.db_mysql.database_name}",
},
"apps": self.tortoise_apps,
"use_timezone": self.use_timezone,
"timezone": self.timezone
}
settings = APIConfig() # 框架基础设置
redis_settings = RedisSettings() # redis设置
tortoise_settings = TortoiseSettings() # tortoise数据库设置
if __name__ == '__main__':
print(redis_settings.redis_config)