<s> import logging <EOL> import logging . config <EOL> from settings import Common <EOL> from service . api import RedisOperationFactory <EOL> from twisted . internet import reactor <EOL> from twisted . internet . endpoints import TCP4ClientEndpoint <EOL> from twisted . web import proxy <EOL> from twisted . web import server <EOL> from twisted . python import log <EOL> from module import Registry <EOL> from protocols . http import HBReverseProxyRequest <EOL> from protocols . http import HBReverseProxyResource <EOL> from protocols . http import HBProxyMgmtRedisSubscriberFactory <EOL> logging . config . dictConfig ( Common . LOGGING ) <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> class HBProxyServer ( object ) : <EOL> def __init__ ( self , configs ) : <EOL> self . proxy_address = configs [ '<STR_LIT>' ] <EOL> self . proxy_port = configs [ '<STR_LIT>' ] <EOL> self . upstream_host = configs [ '<STR_LIT>' ] <EOL> self . upstream_port = configs [ '<STR_LIT>' ] <EOL> self . redis_host = configs [ '<STR_LIT>' ] <EOL> self . redis_port = configs [ '<STR_LIT>' ] <EOL> self . request_channel = configs [ '<STR_LIT>' ] <EOL> self . response_channel = configs [ '<STR_LIT>' ] <EOL> self . _start_logging ( ) <EOL> self . module_registry = Registry ( configs ) <EOL> self . site = server . Site ( proxy . ReverseProxyResource ( self . upstream_host , <EOL> self . upstream_port , '<STR_LIT>' ) ) <EOL> redis_endpoint = TCP4ClientEndpoint ( reactor , self . redis_host , <EOL> self . redis_port ) <EOL> op_factory = RedisOperationFactory ( self , redis_endpoint , <EOL> self . response_channel ) <EOL> self . redis_conn = redis_endpoint . connect ( <EOL> HBProxyMgmtRedisSubscriberFactory ( self . request_channel , <EOL> op_factory ) ) <EOL> self . redis_conn . addCallback ( self . subscribe ) . addCallback ( <EOL> self . start_proxy ) <EOL> def _start_logging ( self ) : <EOL> self . observer = log . PythonLoggingObserver ( loggerName = "<STR_LIT>" ) <EOL> self . observer . start ( ) <EOL> logger . info ( "<STR_LIT>" ) <EOL> def start_proxy ( self , result ) : <EOL> resource = HBReverseProxyResource ( self . upstream_host , <EOL> self . upstream_port , '<STR_LIT>' , <EOL> self . module_registry ) <EOL> f = server . Site ( resource ) <EOL> f . requestFactory = HBReverseProxyRequest <EOL> self . protocol = self . proxy_port <EOL> self . proxy = reactor . listenTCP ( self . protocol , f , <EOL> interface = self . proxy_address ) <EOL> log . msg ( "<STR_LIT>" ) <EOL> return self . proxy <EOL> def subscribe ( self , redis ) : <EOL> return redis . subscribe ( ) <EOL> def run ( self ) : <EOL> reactor . run ( ) <EOL> def _stop_test ( self , result ) : <EOL> reactor . stop ( ) <EOL> def test ( self ) : <EOL> self . tests = self . module_registry . test ( ) <EOL> self . tests . addCallback ( self . _stop_test ) <EOL> self . tests . callback ( "<STR_LIT>" ) <EOL> reactor . run ( ) </s>
