Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
添加APISetting类,统一管理项目配置参数
  • Loading branch information
4linuxfun committed Oct 26, 2022
commit fc79c5dd13f4d2fb70aa6e72ab7c7d8822df52dd
12 changes: 5 additions & 7 deletions server/common/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
from fastapi.security import OAuth2PasswordBearer
from jose import JWTError, jwt
from passlib.context import CryptContext
from typing import Optional
from ..settings import settings

# to get a string like this run:
# openssl rand -hex 32
SECRET_KEY = "09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7"
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 30


pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")

Expand All @@ -25,14 +23,14 @@ def create_access_token(data):
:param data:
:return:
"""
expires_delta = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
expires_delta = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
to_encode = data.copy()
if expires_delta:
expire = datetime.utcnow() + expires_delta
else:
expire = datetime.utcnow() + timedelta(minutes=15)
to_encode.update({"exp": expire})
encoded_jwt = token_encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
encoded_jwt = token_encode(to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM)
return encoded_jwt


Expand All @@ -47,7 +45,7 @@ def token_encode(to_encode, secret_key, algorithm):
return jwt.encode(to_encode, secret_key, algorithm)


def token_decode(token, secret_key=SECRET_KEY, algorithm=ALGORITHM):
def token_decode(token, secret_key=settings.SECRET_KEY, algorithm=settings.ALGORITHM):
"""
token解密
:param token:
Expand Down
7 changes: 3 additions & 4 deletions server/db.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from sqlmodel import create_engine, SQLModel, Session, select
from .settings import settings

SQLALCHEMY_DATABASE_URL = "mysql+pymysql://root:[email protected]/simple_sam"

engine = create_engine(SQLALCHEMY_DATABASE_URL, future=False)
engine = create_engine(settings.DATABASE_URI, future=False)


def init_db():
Expand Down Expand Up @@ -34,4 +33,4 @@ def get_or_create(session: Session, model, **kwargs):
instance = model(**kwargs)
session.add(instance)
session.commit()
return instance
return instance
13 changes: 13 additions & 0 deletions server/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from pydantic import BaseSettings


class APISettings(BaseSettings):
# token加密相关参数
SECRET_KEY: str = "09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7"
ALGORITHM: str = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
# sql数据库信息
DATABASE_URI = "mysql+pymysql://root:[email protected]/devops"


settings = APISettings()