소스 검색

WSGI_SERVER selectable + DebugAPI

boyska 10 년 전
부모
커밋
be56c206b4
2개의 변경된 파일52개의 추가작업 그리고 2개의 파일을 삭제
  1. 5 0
      server/default_config.py
  2. 47 2
      server/server.py

+ 5 - 0
server/default_config.py

@@ -1,5 +1,10 @@
 HOST = 'localhost'
 PORT = '8000'
+WSGI_SERVER = 'wsgiref'
+WSGI_SERVER_OPTIONS = {}
+#WSGI_SERVER = 'gunicorn'
+#WSGI_SERVER_OPTIONS = {'workers': 4, 'worker_class': 'eventlet'}
+
 DEBUG = True
 DB_URI = 'sqlite:///techrec.db'
 AUDIO_OUTPUT = 'output/'

+ 47 - 2
server/server.py

@@ -275,13 +275,58 @@ class RecServer:
                                          root='pages/'))
 
 
+class DebugAPI(Bottle):
+    '''
+    This application is useful for testing the webserver itself
+    '''
+    def __init__(self):
+        Bottle.__init__(self)
+        self.route('/sleep/:milliseconds', callback=self.sleep)
+        self.route('/cpusleep/:howmuch', callback=self.cpusleep)
+        self.route('/big/:exponent', callback=self.big)
+
+    def sleep(self, milliseconds):
+        import time
+        time.sleep(int(milliseconds)/1000.0)
+        return 'ok'
+
+    def cpusleep(self, howmuch):
+        out = ''
+        for i in xrange(int(howmuch) * (10**3)):
+            if i % 11234 == 0:
+                out += 'a'
+        return out
+
+    def big(self, exponent):
+        '''
+        returns a 2**n -1 string
+        '''
+        for i in xrange(int(exponent)):
+            yield str(i) * (2 ** i)
+
+    def help(self):
+        response.content_type = 'text/plain'
+        return '''
+    /sleep/<int:milliseconds> : sleep, than say "ok"
+    /cpusleep/<int:howmuch> : busysleep, than say "ok"
+    /big/<int:exponent> : returns a 2**n -1 byte content
+    '''
+
+
 def main_cmd(*args):
     """meant to be called from argparse"""
     c = RecServer()
     c._app.mount('/date', DateApp())
     c._app.mount('/api', RecAPI())
-    c._app.run(host=get_config()['HOST'], port=get_config()['PORT'],
-               debug=get_config()['DEBUG'])
+    if get_config()['DEBUG']:
+        c._app.mount('/debug', DebugAPI())
+
+    c._app.run(server=get_config()['WSGI_SERVER'],
+               host=get_config()['HOST'],
+               port=get_config()['PORT'],
+               debug=get_config()['DEBUG'],
+               **get_config()['WSGI_SERVER_OPTIONS']
+               )
 
 if __name__ == '__main__':
     from cli import common_pre