This commit is contained in:
@@ -8,34 +8,34 @@ from services.core import get_author_by_user
|
||||
from settings import AUTH_URL
|
||||
|
||||
|
||||
logger = logging.getLogger('[services.auth] ')
|
||||
logger = logging.getLogger("[services.auth] ")
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
|
||||
async def check_auth(req) -> str | None:
|
||||
logger.debug('checking auth...')
|
||||
user_id = ''
|
||||
async def check_auth(req):
|
||||
logger.debug("checking auth...")
|
||||
user_id = ""
|
||||
try:
|
||||
token = req.headers.get('Authorization')
|
||||
token = req.headers.get("Authorization")
|
||||
if token:
|
||||
# Logging the authentication token
|
||||
query_name = 'validate_jwt_token'
|
||||
operation = 'ValidateToken'
|
||||
query_name = "validate_jwt_token"
|
||||
operation = "ValidateToken"
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
variables = {
|
||||
'params': {
|
||||
'token_type': 'access_token',
|
||||
'token': token,
|
||||
"params": {
|
||||
"token_type": "access_token",
|
||||
"token": token,
|
||||
}
|
||||
}
|
||||
|
||||
gql = {
|
||||
'query': f'query {operation}($params: ValidateJWTTokenInput!) {{ {query_name}(params: $params) {{ is_valid claims }} }}',
|
||||
'variables': variables,
|
||||
'operationName': operation,
|
||||
"query": f"query {operation}($params: ValidateJWTTokenInput!) {{ {query_name}(params: $params) {{ is_valid claims }} }}",
|
||||
"variables": variables,
|
||||
"operationName": operation,
|
||||
}
|
||||
# Asynchronous HTTP request to the authentication server
|
||||
async with ClientSession() as session:
|
||||
@@ -44,24 +44,24 @@ async def check_auth(req) -> str | None:
|
||||
) as response:
|
||||
if response.status == 200:
|
||||
data = await response.json()
|
||||
errors = data.get('errors')
|
||||
errors = data.get("errors")
|
||||
if errors:
|
||||
logger.error(f'{errors}')
|
||||
logger.error(f"{errors}")
|
||||
else:
|
||||
user_id = (
|
||||
data.get('data', {})
|
||||
data.get("data", {})
|
||||
.get(query_name, {})
|
||||
.get('claims', {})
|
||||
.get('sub')
|
||||
.get("claims", {})
|
||||
.get("sub")
|
||||
)
|
||||
logger.info(f'got user_id: {user_id}')
|
||||
logger.info(f"got user_id: {user_id}")
|
||||
return user_id
|
||||
except Exception as e:
|
||||
# Handling and logging exceptions during authentication check
|
||||
logger.error(e)
|
||||
|
||||
if not user_id:
|
||||
raise HTTPException(status_code=401, detail='Unauthorized')
|
||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||
|
||||
|
||||
def login_required(f):
|
||||
@@ -69,16 +69,16 @@ def login_required(f):
|
||||
async def decorated_function(*args, **kwargs):
|
||||
info = args[1]
|
||||
context = info.context
|
||||
req = context.get('request')
|
||||
req = context.get("request")
|
||||
user_id = await check_auth(req)
|
||||
if user_id:
|
||||
context['user_id'] = user_id.strip()
|
||||
context["user_id"] = user_id.strip()
|
||||
author = get_author_by_user(user_id)
|
||||
if author and 'id' in author:
|
||||
context['author_id'] = author['id']
|
||||
if author and "id" in author:
|
||||
context["author_id"] = author["id"]
|
||||
else:
|
||||
logger.debug(author)
|
||||
HTTPException(status_code=401, detail='Unauthorized')
|
||||
HTTPException(status_code=401, detail="Unauthorized")
|
||||
return await f(*args, **kwargs)
|
||||
|
||||
return decorated_function
|
||||
|
@@ -9,59 +9,61 @@ from models.member import ChatMember
|
||||
from settings import API_BASE
|
||||
|
||||
|
||||
logger = logging.getLogger('[services.core] ')
|
||||
logger = logging.getLogger("[services.core] ")
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
|
||||
def _request_endpoint(query_name, body) -> dict:
|
||||
logger.debug(f'requesting {query_name}...')
|
||||
response = requests.post(API_BASE, headers={'Content-Type': 'application/json'}, json=body, timeout=30.0)
|
||||
logger.debug(f"requesting {query_name}...")
|
||||
response = requests.post(
|
||||
API_BASE, headers={"Content-Type": "application/json"}, json=body, timeout=30.0
|
||||
)
|
||||
if response.status_code == 200:
|
||||
try:
|
||||
r = response.json()
|
||||
result = r.get('data', {}).get(query_name, {})
|
||||
result = r.get("data", {}).get(query_name, {})
|
||||
if result:
|
||||
logger.info(f'entries amount in result: {len(result)} ')
|
||||
logger.info(f"entries amount in result: {len(result)} ")
|
||||
return result
|
||||
except ValueError as e:
|
||||
logger.error(f'Error decoding JSON response: {e}')
|
||||
logger.error(f"Error decoding JSON response: {e}")
|
||||
|
||||
return {}
|
||||
|
||||
|
||||
def get_all_authors():
|
||||
query_name = 'get_authors_all'
|
||||
query_name = "get_authors_all"
|
||||
|
||||
gql = {
|
||||
'query': 'query { ' + query_name + '{ id slug pic name user } }',
|
||||
'variables': None,
|
||||
"query": "query { " + query_name + "{ id slug pic name user } }",
|
||||
"variables": None,
|
||||
}
|
||||
|
||||
return _request_endpoint(query_name, gql)
|
||||
|
||||
|
||||
def get_author_by_user(user: str):
|
||||
operation = 'GetAuthorId'
|
||||
query_name = 'get_author_id'
|
||||
operation = "GetAuthorId"
|
||||
query_name = "get_author_id"
|
||||
gql = {
|
||||
'query': f'query {operation}($user: String!) {{ {query_name}(user: $user){{ id }} }}', # noqa E201, E202
|
||||
'operationName': operation,
|
||||
'variables': {'user': user.strip()},
|
||||
"query": f"query {operation}($user: String!) {{ {query_name}(user: $user){{ id }} }}", # noqa E201, E202
|
||||
"operationName": operation,
|
||||
"variables": {"user": user.strip()},
|
||||
}
|
||||
|
||||
return _request_endpoint(query_name, gql)
|
||||
|
||||
|
||||
def get_my_followed() -> List[ChatMember]:
|
||||
query_name = 'get_my_followed'
|
||||
query_name = "get_my_followed"
|
||||
|
||||
gql = {
|
||||
'query': 'query { ' + query_name + ' { authors { id slug pic name } } }',
|
||||
'variables': None,
|
||||
"query": "query { " + query_name + " { authors { id slug pic name } } }",
|
||||
"variables": None,
|
||||
}
|
||||
|
||||
result = _request_endpoint(query_name, gql)
|
||||
return result.get('authors', [])
|
||||
return result.get("authors", [])
|
||||
|
||||
|
||||
class CacheStorage:
|
||||
@@ -85,12 +87,12 @@ class CacheStorage:
|
||||
self = CacheStorage
|
||||
async with self.lock:
|
||||
result = get_all_authors()
|
||||
logger.info(f'cache loaded {len(result)}')
|
||||
logger.info(f"cache loaded {len(result)}")
|
||||
if result:
|
||||
CacheStorage.authors = result
|
||||
for a in result:
|
||||
user_id = a.get('user')
|
||||
author_id = str(a.get('id'))
|
||||
user_id = a.get("user")
|
||||
author_id = str(a.get("id"))
|
||||
self.authors_by_user[user_id] = a
|
||||
self.authors_by_id[author_id] = a
|
||||
|
||||
@@ -101,14 +103,14 @@ class CacheStorage:
|
||||
self = CacheStorage
|
||||
while True:
|
||||
try:
|
||||
logger.info(' - updating profiles data...')
|
||||
logger.info(" - updating profiles data...")
|
||||
await self.update_authors()
|
||||
failed = 0
|
||||
except Exception as er:
|
||||
failed += 1
|
||||
logger.error(f'{er} - update failed #{failed}, wait 10 seconds')
|
||||
logger.error(f"{er} - update failed #{failed}, wait 10 seconds")
|
||||
if failed > 3:
|
||||
logger.error(' - not trying to update anymore')
|
||||
logger.error(" - not trying to update anymore")
|
||||
import traceback
|
||||
|
||||
traceback.print_exc()
|
||||
@@ -116,8 +118,11 @@ class CacheStorage:
|
||||
if failed == 0:
|
||||
when = datetime.now(timezone.utc) + timedelta(seconds=self.period)
|
||||
t = format(when.astimezone().isoformat())
|
||||
logger.info(' ⎩ next update: %s' % (t.split('T')[0] + ' ' + t.split('T')[1].split('.')[0]))
|
||||
logger.info(
|
||||
" ⎩ next update: %s"
|
||||
% (t.split("T")[0] + " " + t.split("T")[1].split(".")[0])
|
||||
)
|
||||
await asyncio.sleep(self.period)
|
||||
else:
|
||||
await asyncio.sleep(10)
|
||||
logger.info(' - trying to update data again')
|
||||
logger.info(" - trying to update data again")
|
||||
|
@@ -4,21 +4,21 @@ from models.chat import ChatUpdate, Message
|
||||
from services.rediscache import redis
|
||||
|
||||
|
||||
async def notify_message(message: Message, action='create'):
|
||||
async def notify_message(message: Message, action="create"):
|
||||
channel_name = f"message:{message['chat_id']}"
|
||||
data = {'payload': message, 'action': action}
|
||||
data = {"payload": message, "action": action}
|
||||
try:
|
||||
await redis.publish(channel_name, json.dumps(data))
|
||||
print(f'[services.presence] ok {data}')
|
||||
print(f"[services.presence] ok {data}")
|
||||
except Exception as e:
|
||||
print(f'Failed to publish to channel {channel_name}: {e}')
|
||||
print(f"Failed to publish to channel {channel_name}: {e}")
|
||||
|
||||
|
||||
async def notify_chat(chat: ChatUpdate, member_id: int, action='create'):
|
||||
channel_name = f'chat:{member_id}'
|
||||
data = {'payload': chat, 'action': action}
|
||||
async def notify_chat(chat: ChatUpdate, member_id: int, action="create"):
|
||||
channel_name = f"chat:{member_id}"
|
||||
data = {"payload": chat, "action": action}
|
||||
try:
|
||||
await redis.publish(channel_name, json.dumps(data))
|
||||
print(f'[services.presence] ok {data}')
|
||||
print(f"[services.presence] ok {data}")
|
||||
except Exception as e:
|
||||
print(f'Failed to publish to channel {channel_name}: {e}')
|
||||
print(f"Failed to publish to channel {channel_name}: {e}")
|
||||
|
@@ -5,7 +5,7 @@ import redis.asyncio as aredis
|
||||
from settings import REDIS_URL
|
||||
|
||||
|
||||
logger = logging.getLogger('[services.redis] ')
|
||||
logger = logging.getLogger("[services.redis] ")
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ class RedisCache:
|
||||
async def execute(self, command, *args, **kwargs):
|
||||
if self._client:
|
||||
try:
|
||||
logger.debug(f'{command} {args} {kwargs}')
|
||||
logger.debug(f"{command} {args} {kwargs}")
|
||||
r = await self._client.execute_command(command, *args, **kwargs)
|
||||
logger.debug(type(r))
|
||||
logger.debug(r)
|
||||
@@ -56,4 +56,4 @@ class RedisCache:
|
||||
|
||||
redis = RedisCache()
|
||||
|
||||
__all__ = ['redis']
|
||||
__all__ = ["redis"]
|
||||
|
30
services/sentry.py
Normal file
30
services/sentry.py
Normal file
@@ -0,0 +1,30 @@
|
||||
import sentry_sdk
|
||||
from sentry_sdk.integrations.ariadne import AriadneIntegration
|
||||
from sentry_sdk.integrations.redis import RedisIntegration
|
||||
from sentry_sdk.integrations.starlette import StarletteIntegration
|
||||
|
||||
from settings import SENTRY_DSN
|
||||
|
||||
|
||||
def start_sentry():
|
||||
# sentry monitoring
|
||||
try:
|
||||
sentry_sdk.init(
|
||||
SENTRY_DSN,
|
||||
# Set traces_sample_rate to 1.0 to capture 100%
|
||||
# of transactions for performance monitoring.
|
||||
traces_sample_rate=1.0,
|
||||
# Set profiles_sample_rate to 1.0 to profile 100%
|
||||
# of sampled transactions.
|
||||
# We recommend adjusting this value in production.
|
||||
profiles_sample_rate=1.0,
|
||||
enable_tracing=True,
|
||||
integrations=[
|
||||
StarletteIntegration(),
|
||||
AriadneIntegration(),
|
||||
RedisIntegration(),
|
||||
],
|
||||
)
|
||||
except Exception as e:
|
||||
print("[services.sentry] init error")
|
||||
print(e)
|
Reference in New Issue
Block a user