Commit e9ec4ed1 authored by 张宇's avatar 张宇

rpc_view

parent 15dae3fd
# -*- coding: utf-8 -*-
from gm_rpcd.all import bind, context
ROUTER_PREFIX = 'courier/'
# ROUTER_PREFIX = 'courier/'
ROUTER_PREFIX = ''
bind_prefix = lambda endpoint, **options: bind(ROUTER_PREFIX+endpoint, **options)
......@@ -4,6 +4,7 @@
<config name="application_name" value="courier"/>
<config name="service_list">
<element value="courier"/>
<element value="message"/>
</config>
<config name="initializer_list">
<element value="courier.management_interface.initialize:initialize"/>
......
......@@ -147,8 +147,8 @@ STATIC_URL = '/static/'
#######################################################
####################### LOGGING #######################
#######################################################
# https://docs.djangoproject.com/en/2.2/topics/logging/
# django.utils.log:18, django.conf.global_settings:559
# https://docs.djangoproject.com/en/3.0/topics/logging/
# django.utils.log:18, django.conf.global_settings:561
PROJECT_LOG_PATH = '/data/log/courier/app'
if not os.path.exists(PROJECT_LOG_PATH):
try:
......@@ -274,4 +274,4 @@ COUNT_LIMIT = 100
ES_SEARCH_TIMEOUT = '10s'
DATABASE_ROUTERS = ['courier.db_routers.MessageRouter']
MESSAGE_DB_NAME = 'default'
MESSAGE_SLAVE_DB_NAME = 'slave'
\ No newline at end of file
import multiprocessing
workers = multiprocessing.cpu_count() + 1
bind = '0.0.0.0:8000'
bind = '0.0.0.0:8005'
proc_name = 'courier'
timeout = 3600
preload_app = True
......
......@@ -11,6 +11,7 @@ from adapter.old_system import bind_prefix
from adapter.rpcd.exceptions import RPCPermanentError
from api.models.message import ConversationUserStatus
from rpc import gaia_client
from rpc_framework.decorators import rpc_view
from search.utils import search_conversation_from_es
from services.unread.stat import UserUnread
......@@ -232,3 +233,8 @@ def message_conversation_list_v3(user_ids: List[int],
}
@rpc_view('message/conversation/can_send')
def check_can_send_message(context, target_uid: str) -> Dict:
# print(context.session_id)
print('+++', context, dir(context), context._context.session_id)
return {'a': 'b'}
......@@ -17,4 +17,4 @@ raven==6.10.0
elasticsearch==2.3.0
kafka-python==1.4.7
gunicorn==20.0.4
djangorestframework==3.11.0
\ No newline at end of file
#djangorestframework==3.11.0
\ No newline at end of file
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
import sys
from contextlib import contextmanager
from typing import Optional, Tuple
from gm_rpcd.internals.context import Context
from rpc_framework import exceptions
from rpc_framework.settings import api_settings
class WrappedAttributeError(Exception):
pass
@contextmanager
def wrap_attributeerrors():
"""
Used to re-raise AttributeErrors caught during authentication, preventing
these errors from otherwise being handled by the attribute access protocol.
"""
try:
yield
except AttributeError:
info = sys.exc_info()
exc = WrappedAttributeError(str(info[1]))
raise exc.with_traceback(info[2])
class RPCViewContext(object):
def __init__(self,
context: Context,
authenticators: Optional[Tuple]=None,
interceptors: Optional[Tuple]=None,
negotiator: Optional[Tuple]=None):
self._context = context
self.authenticators = authenticators or ()
self.interceptors = interceptors or ()
# self.negotiator = negotiator or self._default_negotiator()
@property
def user(self):
"""
Returns the user associated with the current request, as authenticated
by the authentication classes provided to the request.
"""
if not hasattr(self, '_user'):
with wrap_attributeerrors():
self._authenticate()
return self._user
@user.setter
def user(self, value):
"""
Sets the user on the current request. This is necessary to maintain
compatibility with django.contrib.auth where the user property is
set in the login and logout functions.
Note that we also set the user on Django's underlying `HttpRequest`
instance, ensuring that it is available to any middleware in the stack.
"""
self._user = value
self._request.user = value
def _authenticate(self):
"""
Attempt to authenticate the request using each authentication instance
in turn.
"""
for authenticator in self.authenticators:
try:
user_auth_tuple = authenticator.authenticate(self)
except exceptions.RPCViewBaseException:
self._not_authenticated()
raise
if user_auth_tuple is not None:
self._authenticator = authenticator
self.user, self.auth = user_auth_tuple
return
self._not_authenticated()
def _not_authenticated(self):
"""
Set authenticator, user & authtoken representing an unauthenticated request.
Defaults are None, AnonymousUser & None.
"""
self._authenticator = None
if api_settings.UNAUTHENTICATED_USER:
self.user = api_settings.UNAUTHENTICATED_USER()
else:
self.user = None
if api_settings.UNAUTHENTICATED_TOKEN:
self.auth = api_settings.UNAUTHENTICATED_TOKEN()
else:
self.auth = None
# -*- coding: utf-8 -*-
import types
from rpc_framework.views import RPCView
def rpc_view(endpoint):
"""
Decorator that converts a function-based view into an RPCView subclass.
"""
def decorator(func):
WrappedAPIView = type(
'WrappedAPIView',
(RPCView,),
{'__doc__': func.__doc__}
)
WrappedAPIView.endpoint = endpoint
def handler(self, context, *args, **kwargs):
return func(context, *args, **kwargs)
setattr(WrappedAPIView, 'handler', handler)
WrappedAPIView.__name__ = func.__name__
WrappedAPIView.__module__ = func.__module__
# WrappedAPIView.renderer_classes = getattr(func, 'renderer_classes',
# RPCView.renderer_classes)
WrappedAPIView.parser_classes = getattr(func, 'parser_classes',
RPCView.parser_classes)
WrappedAPIView.authentication_classes = getattr(func, 'authentication_classes',
RPCView.authentication_classes)
WrappedAPIView.throttle_classes = getattr(func, 'throttle_classes',
RPCView.throttle_classes)
WrappedAPIView.permission_classes = getattr(func, 'permission_classes',
RPCView.permission_classes)
# WrappedAPIView.schema = getattr(func, 'schema', RPCView.schema)
return WrappedAPIView.rpc_bind()
return decorator
# -*- coding: utf-8 -*-
class RPCViewBaseException(Exception):
pass
# -*- coding: utf-8 -*-
import gm_rpcd
from gm_rpcd.internals.dynamic_scopes import Keys, dynamic_scope
class RPCAbstractView(object):
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
@classmethod
def rpc_bind(cls, **initkwargs):
"""
Store the original class on the view function.
This allows us to discover information about the view when we do URL
reverse lookups. Used for breadcrumb generation.
"""
def view(*args, **kwargs):
self = cls(**initkwargs)
# context = gm_rpcd.all.context
context = dynamic_scope.get(Keys.CONTEXT)
self.setup(context, *args, **kwargs)
return self.dispatch(context, *args, **kwargs)
view.view_class = cls
view.view_initkwargs = initkwargs
# avoid exception raise at gm_rpcd/internals/object_resolution_tables.py:62
view.__name__ = cls.__name__
view.__module__ = cls.__module__
gm_rpcd.all.bind(cls.endpoint)(view)
return view
def setup(self, context, *args, **kwargs):
"""Initialize attributes shared by all view methods."""
self.context = context
self.args = args
self.kwargs = kwargs
def dispatch(self, context, *args, **kwargs):
# print(type(context)) # gm_rpcd.internals.proxy_object.ProxyObject
# assert isinstance(context, Context), 'context must be instance of gm_rpcd.internals.context.Context'
return self.handler(context, *args, **kwargs)
"""
Settings for REST framework are all namespaced in the REST_FRAMEWORK setting.
For example your project's `settings.py` file might look like this:
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': [
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.TemplateHTMLRenderer',
],
'DEFAULT_PARSER_CLASSES': [
'rest_framework.parsers.JSONParser',
'rest_framework.parsers.FormParser',
'rest_framework.parsers.MultiPartParser',
],
}
This module provides the `api_setting` object, that is used to access
REST framework settings, checking for user settings first, then falling
back to the defaults.
"""
from django.conf import settings
from django.test.signals import setting_changed
from django.utils.module_loading import import_string
# from rest_framework import ISO_8601
DEFAULTS = {
# Base API policies
# 'DEFAULT_RENDERER_CLASSES': [
# 'rest_framework.renderers.JSONRenderer',
# 'rest_framework.renderers.BrowsableAPIRenderer',
# ],
# 'DEFAULT_PARSER_CLASSES': [
# 'rest_framework.parsers.JSONParser',
# 'rest_framework.parsers.FormParser',
# 'rest_framework.parsers.MultiPartParser'
# ],
# 'DEFAULT_AUTHENTICATION_CLASSES': [
# 'rest_framework.authentication.SessionAuthentication',
# 'rest_framework.authentication.BasicAuthentication'
# ],
# 'DEFAULT_PERMISSION_CLASSES': [
# 'rest_framework.permissions.AllowAny',
# ],
# 'DEFAULT_THROTTLE_CLASSES': [],
# 'DEFAULT_CONTENT_NEGOTIATION_CLASS': 'rest_framework.negotiation.DefaultContentNegotiation',
# 'DEFAULT_METADATA_CLASS': 'rest_framework.metadata.SimpleMetadata',
# 'DEFAULT_VERSIONING_CLASS': None,
# Generic view behavior
# 'DEFAULT_PAGINATION_CLASS': None,
# 'DEFAULT_FILTER_BACKENDS': [],
# Schema
# 'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.openapi.AutoSchema',
# Throttling
# 'DEFAULT_THROTTLE_RATES': {
# 'user': None,
# 'anon': None,
# },
# 'NUM_PROXIES': None,
# Pagination
# 'PAGE_SIZE': None,
# Filtering
# 'SEARCH_PARAM': 'search',
# 'ORDERING_PARAM': 'ordering',
# Versioning
# 'DEFAULT_VERSION': None,
# 'ALLOWED_VERSIONS': None,
# 'VERSION_PARAM': 'version',
#
# Authentication
# 'UNAUTHENTICATED_USER': 'django.contrib.auth.models.AnonymousUser',
# 'UNAUTHENTICATED_TOKEN': None,
# View configuration
# 'VIEW_NAME_FUNCTION': 'rest_framework.views.get_view_name',
# 'VIEW_DESCRIPTION_FUNCTION': 'rest_framework.views.get_view_description',
# Exception handling
# 'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler',
# 'NON_FIELD_ERRORS_KEY': 'non_field_errors',
# Testing
# 'TEST_REQUEST_RENDERER_CLASSES': [
# 'rest_framework.renderers.MultiPartRenderer',
# 'rest_framework.renderers.JSONRenderer'
# ],
# 'TEST_REQUEST_DEFAULT_FORMAT': 'multipart',
# Hyperlink settings
# 'URL_FORMAT_OVERRIDE': 'format',
# 'FORMAT_SUFFIX_KWARG': 'format',
# 'URL_FIELD_NAME': 'url',
# Input and output formats
# 'DATE_FORMAT': ISO_8601,
# 'DATE_INPUT_FORMATS': [ISO_8601],
#
# 'DATETIME_FORMAT': ISO_8601,
# 'DATETIME_INPUT_FORMATS': [ISO_8601],
# 'TIME_FORMAT': ISO_8601,
# 'TIME_INPUT_FORMATS': [ISO_8601],
# Encoding
# 'UNICODE_JSON': True,
# 'COMPACT_JSON': True,
# 'STRICT_JSON': True,
# 'COERCE_DECIMAL_TO_STRING': True,
# 'UPLOADED_FILES_USE_URL': True,
#
# Browseable API
# 'HTML_SELECT_CUTOFF': 1000,
# 'HTML_SELECT_CUTOFF_TEXT': "More than {count} items...",
# Schemas
# 'SCHEMA_COERCE_PATH_PK': True,
# 'SCHEMA_COERCE_METHOD_NAMES': {
# 'retrieve': 'read',
# 'destroy': 'delete'
# },
}
# List of settings that may be in string import notation.
IMPORT_STRINGS = [
# 'DEFAULT_RENDERER_CLASSES',
# 'DEFAULT_PARSER_CLASSES',
# 'DEFAULT_AUTHENTICATION_CLASSES',
# 'DEFAULT_PERMISSION_CLASSES',
# 'DEFAULT_THROTTLE_CLASSES',
# 'DEFAULT_CONTENT_NEGOTIATION_CLASS',
# 'DEFAULT_METADATA_CLASS',
# 'DEFAULT_VERSIONING_CLASS',
# 'DEFAULT_PAGINATION_CLASS',
# 'DEFAULT_FILTER_BACKENDS',
# 'DEFAULT_SCHEMA_CLASS',
# 'EXCEPTION_HANDLER',
# 'TEST_REQUEST_RENDERER_CLASSES',
# 'UNAUTHENTICATED_USER',
# 'UNAUTHENTICATED_TOKEN',
# 'VIEW_NAME_FUNCTION',
# 'VIEW_DESCRIPTION_FUNCTION'
]
# List of settings that have been removed
REMOVED_SETTINGS = [
# 'PAGINATE_BY', 'PAGINATE_BY_PARAM', 'MAX_PAGINATE_BY',
]
def perform_import(val, setting_name):
"""
If the given setting is a string import notation,
then perform the necessary import or imports.
"""
if val is None:
return None
elif isinstance(val, str):
return import_from_string(val, setting_name)
elif isinstance(val, (list, tuple)):
return [import_from_string(item, setting_name) for item in val]
return val
def import_from_string(val, setting_name):
"""
Attempt to import a class from a string representation.
"""
try:
return import_string(val)
except ImportError as e:
msg = "Could not import '%s' for API setting '%s'. %s: %s." % (val, setting_name, e.__class__.__name__, e)
raise ImportError(msg)
class APISettings:
"""
A settings object, that allows API settings to be accessed as properties.
For example:
from rest_framework.settings import api_settings
print(api_settings.DEFAULT_RENDERER_CLASSES)
Any setting with string import paths will be automatically resolved
and return the class, rather than the string literal.
"""
def __init__(self, user_settings=None, defaults=None, import_strings=None):
if user_settings:
self._user_settings = self.__check_user_settings(user_settings)
self.defaults = defaults or DEFAULTS
self.import_strings = import_strings or IMPORT_STRINGS
self._cached_attrs = set()
@property
def user_settings(self):
if not hasattr(self, '_user_settings'):
self._user_settings = getattr(settings, 'REST_FRAMEWORK', {})
return self._user_settings
def __getattr__(self, attr):
if attr not in self.defaults:
raise AttributeError("Invalid API setting: '%s'" % attr)
try:
# Check if present in user settings
val = self.user_settings[attr]
except KeyError:
# Fall back to defaults
val = self.defaults[attr]
# Coerce import strings into classes
if attr in self.import_strings:
val = perform_import(val, attr)
# Cache the result
self._cached_attrs.add(attr)
setattr(self, attr, val)
return val
def __check_user_settings(self, user_settings):
SETTINGS_DOC = "https://www.django-rest-framework.org/api-guide/settings/"
for setting in REMOVED_SETTINGS:
if setting in user_settings:
raise RuntimeError("The '%s' setting has been removed. Please refer to '%s' for available settings." % (setting, SETTINGS_DOC))
return user_settings
def reload(self):
for attr in self._cached_attrs:
delattr(self, attr)
self._cached_attrs.clear()
if hasattr(self, '_user_settings'):
delattr(self, '_user_settings')
api_settings = APISettings(None, DEFAULTS, IMPORT_STRINGS)
def reload_api_settings(*args, **kwargs):
setting = kwargs['setting']
if setting == 'RPC_FRAMEWORK':
api_settings.reload()
setting_changed.connect(reload_api_settings)
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment