Exemple #1
0
try:
    import hashlib

    sha1 = hashlib.sha1
except ImportError:
    import sha

    sha1 = sha.new

from torngas.storage import storage
from torngas.utils import safestr
from torngas.utils import lazyimport
from torngas.settings_manager import settings
from torngas.cache import caches

cache_module = lazyimport("torngas.cache")
rx = re.compile("^[0-9a-fA-F]+$")


class SessionMiddleware(object):
    _cachestore = None
    session = None

    def process_init(self, application):
        self._cachestore = caches[settings.SESSION.session_cache_alias]

    def process_request(self, handler, clear):
        session = SessionManager(handler, self._cachestore, settings.SESSION)
        session.load_session()
        handler.session = session
Exemple #2
0
    sha1 = hashlib.sha1
except ImportError:
    import sha

    sha1 = sha.new

from torngas.storage import storage
from torngas.utils import safestr
from torngas.utils import lazyimport
from torngas.middleware import BaseMiddleware
from torngas.logger.client import SysLogger
from torngas.settings_manager import settings
from torngas.cache import get_cache

cache_module = lazyimport('torngas.cache')
rx = re.compile('^[0-9a-fA-F]+$')


class SessionMiddleware(BaseMiddleware):
    _cachestore = None
    session = None

    def process_init(self, application):
        self._cachestore = get_cache(settings.SESSION.session_cache_alias)

    def process_request(self, handler):
        session = SessionManager(handler, self._cachestore, settings.SESSION)
        session.load_session()
        handler.session = session
try:
    import hashlib

    sha1 = hashlib.sha1
except ImportError:
    import sha

    sha1 = sha.new
import hmac, re
from torngas.utils.storage import storage
from torngas.utils.strtools import safestr
from torngas.utils import lazyimport
from middleware_manager import BaseMiddleware
from torngas.utils import Null

logger_module = lazyimport('torngas.helpers.logger_helper')

settings_module = lazyimport('torngas.helpers.settings_helper')
cache_module = lazyimport('torngas.cache')
rx = re.compile('^[0-9a-fA-F]+$')



class SessionMiddleware(BaseMiddleware):
    def process_init(self, application):
        self._cachestore = cache_module.get_cache(settings_module.settings.SESSION.session_cache_alias)


    def process_request(self, handler):
        session = SessionManager(handler, self._cachestore, settings_module.settings.SESSION)
        session.load_session()
Exemple #4
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tornado.web as web
from tornado.log import app_log
from torngas.utils import lazyimport

middleware_module = lazyimport('torngas.middleware')


class Application(web.Application):
    def __init__(self, handlers=None, default_host="", transforms=None,
                 wsgi=False, **settings):

        super(Application, self).__init__(
            handlers=handlers,
            default_host=default_host,
            transforms=transforms,
            wsgi=wsgi, **settings)

        self.middleware_manager = middleware_module.MiddlewareManager()
        self.middleware_manager.run_init_hooks(self)

    def __call__(self, request):
        try:
            self.middleware_manager.run_call_hooks(request)
            handler = web.Application.__call__(self, request)
            self.middleware_manager.run_endcall_hooks(handler)

        except Exception, e:
            app_log.error(e)
Exemple #5
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import warnings
import sys
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
from tornado.util import import_object
from torngas.utils import lazyimport
from torngas.exception import ConfigError, BaseError

settings = lazyimport("torngas.settings_manager.settings")

reload(sys)
sys.setdefaultencoding('utf-8')
define("port", default=8000, help="run server on it", type=int)
define("settings", help="setting module name", type=str)
define("address", default='127.0.0.1', help='listen host,default:127.0.0.1', type=str)
define("servermode", default='httpserver', help="run server mode", type=str, metavar='httpserver|logserver')


class Server(object):
    settings = settings
    urls = []
    application = None

    def load_application(self, application=None):

        if self.settings.TRANSLATIONS:
Exemple #6
0
#!/usr/bin/env python
# -*- coding: utf-8  -*-
from torngas.utils import lazyimport
from torngas.middleware import BaseMiddleware

__author__ = 'qingyun.meng'
"""
Description: signal from django
SignalMiddleware提供在程序运行至中间件call,request,response,endcall四个阶段时触发信号的能力

"""
signals_module = lazyimport('torngas.dispatch')


class SignalMiddleware(BaseMiddleware):
    def process_call(self, request):
        signals_module.signals.call_started.send(sender=request.__class__)

    def process_request(self, handler):
        signals_module.signals.handler_started.send(sender=handler.__class__)

    def process_response(self, handler, chunk=None):
        signals_module.signals.handler_finished.send(sender=handler.__class__)

    def process_endcall(self, handler):
        signals_module.signals.call_finished.send(sender=handler.__class__)


if __name__ == '__main__':
    pass
Exemple #7
0
#!/usr/bin/env python
#coding=utf-8

import tornado.web as web
from torngas.utils import lazyimport
from torngas.helpers.logger_helper import logger

signals_module = lazyimport('torngas.dispatch')
middleware_module = lazyimport('torngas.middleware')
logger_module = lazyimport('torngas.helpers.logger_helper')


class AppApplication(web.Application):
    def __init__(self,
                 handlers=None,
                 default_host="",
                 transforms=None,
                 wsgi=False,
                 **settings):

        web.Application.__init__(self,
                                 handlers=handlers,
                                 default_host=default_host,
                                 transforms=transforms,
                                 wsgi=wsgi,
                                 **settings)

        self.middleware_manager = middleware_module.MiddlewareManager()
        self.middleware_manager.run_init_hooks(self)

    def __call__(self, request):
Exemple #8
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import warnings
import logging, os, sys
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
from tornado.util import import_object
from torngas.utils import lazyimport
from torngas.exception import ConfigError, TorngasError

reload(sys)
sys.setdefaultencoding('utf-8')
application_module = lazyimport('torngas.application')
settings_module = lazyimport('torngas.helpers.settings_helper')


class Server(object):
    settings = None
    urls = []
    application = None
    proj_path = None

    def init(self, project_path=None):
        self.load_define()
        tornado.options.parse_command_line()
        self.settings = settings_module.settings
        self.proj_path = project_path
        return self
Exemple #9
0
    sha1 = hashlib.sha1
except ImportError:
    import sha

    sha1 = sha.new

from torngas.utils.storage import storage
from torngas.utils.strtools import safestr
from torngas.utils import lazyimport
from torngas.middleware import BaseMiddleware
from tornado.log import app_log
from torngas.settings_manager import settings
from torngas.cache import get_cache

cache_module = lazyimport('torngas.cache')
rx = re.compile('^[0-9a-fA-F]+$')


class SessionMiddleware(BaseMiddleware):
    _cachestore = None
    session = None

    def process_init(self, application):
        self._cachestore = get_cache(settings.SESSION.session_cache_alias)

    def process_request(self, handler):
        session = SessionManager(handler, self._cachestore, settings.SESSION)
        session.load_session()
        handler.session = session
Exemple #10
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import warnings
import sys
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
from tornado.util import import_object
from torngas.utils import lazyimport
from torngas.exception import ConfigError, BaseError

settings = lazyimport("torngas.settings_manager.settings")

reload(sys)
sys.setdefaultencoding('utf-8')
define("port", default=8000, help="run server on it", type=int)
define("settings", help="setting module name", type=str)
define("address",
       default='127.0.0.1',
       help='listen host,default:127.0.0.1',
       type=str)
define("servermode",
       default='httpserver',
       help="run server mode",
       type=str,
       metavar='httpserver|logserver')


class Server(object):
Exemple #11
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tornado.web as web
from tornado.log import app_log,access_log
from torngas.utils import lazyimport


signals_module = lazyimport('torngas.dispatch')
middleware_module = lazyimport('torngas.middleware')


class AppApplication(web.Application):
    def __init__(self, handlers=None, default_host="", transforms=None,
                 wsgi=False, **settings):

        web.Application.__init__(self,
                                 handlers=handlers,
                                 default_host=default_host,
                                 transforms=transforms,
                                 wsgi=wsgi, **settings)

        self.middleware_manager = middleware_module.MiddlewareManager()
        self.middleware_manager.run_init_hooks(self)

    def __call__(self, request):
        try:
            signals_module.signals.call_started.send(sender=self.__class__)
            self.middleware_manager.run_call_hooks(request)
            handler = web.Application.__call__(self, request)
            self.middleware_manager.run_endcall_hooks(handler)
Exemple #12
0
#!/usr/bin/env python
# -*- coding: utf-8  -*-
from torngas.utils import lazyimport
from torngas.middleware import BaseMiddleware

__author__ = 'qingyun.meng'

"""
Description: signal from django
SignalMiddleware提供在程序运行至中间件call,request,response,endcall四个阶段时触发信号的能力

"""
signals_module = lazyimport('torngas.dispatch')


class SignalMiddleware(BaseMiddleware):
    def process_call(self, request):
        signals_module.signals.call_started.send(sender=request.__class__)

    def process_request(self, handler):
        signals_module.signals.handler_started.send(sender=handler.__class__)

    def process_response(self, handler):
        signals_module.signals.handler_finished.send(sender=handler.__class__)

    def process_endcall(self, handler):
        signals_module.signals.call_finished.send(sender=handler.__class__)


if __name__ == '__main__':
    pass
Exemple #13
0
#!/usr/bin/env python
# -*- coding: utf-8  -*-
from torngas.utils import lazyimport
from torngas.middleware import BaseMiddleware

__author__ = "qingyun.meng"

"""
Description: signal from django
"""
signals_module = lazyimport("torngas.dispatch")


class SignalMiddleware(BaseMiddleware):
    def process_call(self, request):
        signals_module.signals.call_started.send(sender=request.__class__)

    def process_request(self, handler):
        signals_module.signals.handler_started.send(sender=handler.__class__)

    def process_response(self, handler):
        signals_module.signals.handler_finished.send(sender=handler.__class__)

    def process_endcall(self, handler):
        signals_module.signals.call_finished.send(sender=handler.__class__)


if __name__ == "__main__":
    pass
Exemple #14
0
# -*- coding: utf-8 -*-
import warnings
import logging, os, sys
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
from tornado.log import enable_pretty_logging
from tornado.util import import_object
from torngas.utils import lazyimport
from torngas.exception import ConfigError

reload(sys)
sys.setdefaultencoding('utf-8')
application_module = lazyimport('torngas.application')
settings_module = lazyimport('torngas.helpers.settings_helper')
logger_module = lazyimport('torngas.helpers.logger_helper')

define("port", default=8000, help="run server on it", type=int)
define("setting", default='settings_devel', help="""config used to set the configuration file type,\n
settings_devel was default,you can set settings_functest or settings_production (it's your config file name)""",
       type=str)
define("address", default='localhost', help='listen host,default:localhost', type=str)
define("log_frefix", default='torngas', help='log file dirname', type=str)


class Server(object):
    def __init__(self, project_path=None, application=None):
        tornado.options.parse_command_line()
        self.application = application
Exemple #15
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tornado.web as web
from torngas.utils import lazyimport
from torngas.helpers.logger_helper import logger

signals_module = lazyimport('torngas.dispatch')
middleware_module = lazyimport('torngas.middleware')
logger_module = lazyimport('torngas.helpers.logger_helper')


class AppApplication(web.Application):
    def __init__(self, handlers=None, default_host="", transforms=None,
                 wsgi=False, **settings):

        web.Application.__init__(self,
                                 handlers=handlers,
                                 default_host=default_host,
                                 transforms=transforms,
                                 wsgi=wsgi, **settings)

        self.middleware_manager = middleware_module.MiddlewareManager()
        self.middleware_manager.run_init_hooks(self)

    def __call__(self, request):
        try:
            signals_module.signals.call_started.send(sender=self.__class__)
            self.middleware_manager.run_call_hooks(request)
            handler = web.Application.__call__(self, request)
            self.middleware_manager.run_endcall_hooks(handler)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import base64
import hmac
import hashlib
import time
import threading
import re
import tornado
from urllib import unquote
from tornado.escape import utf8
from torngas import exception
from torngas.utils import lazyimport
from torngas.mixin.handler_mixin import UncaughtExceptionMixin, FlashMessageMixIn
from torngas.dispatch import signals
from tornado.ioloop import IOLoop
signals_module = lazyimport('torngas.dispatch')
settings_module = lazyimport('torngas.helpers.settings_helper')

class CommonHandler(tornado.web.RequestHandler):
    def __init__(self, application, request, **kwargs):
        super(CommonHandler, self).__init__(application, request, **kwargs)
        self._is_threaded = False
        self._is_torngas_finished = False
    def initialize(self, **kwargs):
        self.current_appname = kwargs.get('subapp_name', None)
    def prepare(self):
        signals_module.signals.handler_started.send(sender=self.__class__)
        self.application.middleware_manager.run_request_hooks(self)
    def on_prepare(self):
Exemple #17
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Torngas Whitelist Module

"""
from tornado import web
import types
from torngas.utils import lazyimport

settings_module = lazyimport('torngas.helpers.settings_helper')


def whitelisted(argument=None):
    """
    白名单,如果在参数中列出可访问的ip或在配置文件中列出,则被标记的请求方法仅可允许白名单ip访问
    :param argument: whitelist ip list
    :return:bool
    """

    def is_whitelisted(remote_ip, whitelist):
        if remote_ip in whitelist:
            return True
        else:
            return False

    if type(argument) is types.FunctionType:

        def wrapper(self, *args, **kwargs):
            white_setting = settings_module.settings.WHITELIST
            if white_setting:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from tornado.util import import_object
from torngas.utils import lazyimport

settings_module = lazyimport('torngas.helpers.settings_helper')

class MiddlewareManager():
    def __init__(self):
        self.request_middleware = []
        self.response_middleware = []
        self.exception_middleware = []
        self.call_middleware = []
        self.endcall_middleware = []
        self.init_middleware = []
        self.load()

    def run_init_hooks(self, app):
        self.__run_hooks('init', self.init_middleware, app)

    def run_call_hooks(self, request):
        self.__run_hooks('call', self.call_middleware, request)

    def run_endcall_hooks(self, request):
        self.__run_hooks('endcall', self.endcall_middleware, request)

    def run_request_hooks(self, req_handler):
        self.__run_hooks('request', self.request_middleware, req_handler)

    def run_response_hooks(self, req_handler):
Exemple #19
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import base64
import hmac
import hashlib
import time
import threading
import re
import tornado
from tornado.web import RequestHandler, HTTPError
from urllib import unquote
from tornado.escape import utf8
from torngas.utils import lazyimport
from torngas.mixin.handler_mixin import UncaughtExceptionMixin
from tornado.ioloop import IOLoop
signals_module = lazyimport('torngas.dispatch')
settings_module = lazyimport('torngas.helpers.settings_helper')

class CommonHandler(RequestHandler):
    def __init__(self, application, request, **kwargs):
        self._is_threaded = False
        self._is_torngas_finished = False
        self.current_appname = kwargs.pop('current_appname', None)
        super(CommonHandler, self).__init__(application, request, **kwargs)
    def initialize(self, *args, **kwargs):
        pass
    def prepare(self):
        if not self.application.middleware_manager.run_request_hooks(self):
            self.on_prepare()
    def on_prepare(self):