Automatic request handler route registration
This commit is contained in:
parent
f58adf2833
commit
3bda68b466
|
@ -37,11 +37,16 @@ class OctetStreamHandler(object):
|
|||
|
||||
|
||||
class ChunkTemplateHandler(BaseTextHandler):
|
||||
pass
|
||||
"""TODO
|
||||
"""
|
||||
URI = ('/', '/index.htm', 'index')
|
||||
|
||||
|
||||
class ThumbHandler(object):
|
||||
"""TODO
|
||||
"""
|
||||
mimetype = 'image/png'
|
||||
URI = ('/getthumb',)
|
||||
|
||||
@staticmethod
|
||||
def handle(httpd):
|
||||
|
@ -54,7 +59,6 @@ class LegacyListHandler(BaseTextHandler):
|
|||
|
||||
URI: ``/superlist``
|
||||
"""
|
||||
|
||||
URI = ('/superlist',)
|
||||
|
||||
@staticmethod
|
||||
|
@ -110,11 +114,16 @@ class JsonListHandler(object):
|
|||
|
||||
|
||||
class SingleFileHandler(OctetStreamHandler):
|
||||
pass
|
||||
"""TODO
|
||||
"""
|
||||
URI = ('/get/',)
|
||||
|
||||
|
||||
class ZipFileHandler(object):
|
||||
"""TODO
|
||||
"""
|
||||
mimetype = 'application/zip'
|
||||
URI = ('/getzip',)
|
||||
|
||||
@staticmethod
|
||||
def handle(httpd):
|
||||
|
@ -130,6 +139,7 @@ class SuperStreamHandler(OctetStreamHandler):
|
|||
|
||||
URI: ``/getstream``
|
||||
"""
|
||||
URI = ('/getstream',)
|
||||
|
||||
@staticmethod
|
||||
def _get_streamsize(files):
|
||||
|
@ -160,11 +170,16 @@ class SuperStreamHandler(OctetStreamHandler):
|
|||
|
||||
|
||||
class AssetHandler(OctetStreamHandler):
|
||||
pass
|
||||
"""TODO
|
||||
"""
|
||||
URI = ('*',)
|
||||
|
||||
|
||||
class ApkRequestHandler(object):
|
||||
"""TODO
|
||||
"""
|
||||
mimetype = 'application/vnd.android.package-archive'
|
||||
URI = ('/getapk',)
|
||||
|
||||
@staticmethod
|
||||
def handle(httpd):
|
||||
|
|
|
@ -8,12 +8,8 @@ import socket
|
|||
import struct
|
||||
import base64
|
||||
|
||||
|
||||
from SuperBeam.RequestHandlers import (
|
||||
ChunkTemplateHandler, ThumbHandler, ApkRequestHandler, LegacyListHandler,
|
||||
SingleFileHandler, ZipFileHandler, SuperStreamHandler, AssetHandler,
|
||||
JsonListHandler
|
||||
)
|
||||
import inspect
|
||||
from SuperBeam import RequestHandlers
|
||||
|
||||
|
||||
class SuperBeamServer(BaseHTTPRequestHandler):
|
||||
|
@ -37,9 +33,14 @@ class SuperBeamServer(BaseHTTPRequestHandler):
|
|||
files (list): list of files served by the `SuperBeamServer`.
|
||||
"""
|
||||
|
||||
def __init__(self, debug=False, *args, **kwargs):
|
||||
def __init__(self, request, client_address, server, debug=False):
|
||||
self.debug = debug
|
||||
super(SuperBeamServer, self).__init__()
|
||||
self.routes = {}
|
||||
for _, cls in inspect.getmembers(RequestHandlers, inspect.isclass):
|
||||
if hasattr(cls, 'URI'):
|
||||
for route in cls.URI:
|
||||
self.routes[route] = cls
|
||||
super(SuperBeamServer, self).__init__(request, client_address, server)
|
||||
|
||||
def do_GET(self):
|
||||
"""GET request handler
|
||||
|
@ -53,26 +54,12 @@ class SuperBeamServer(BaseHTTPRequestHandler):
|
|||
exists - forwarded to :func:`respond()`, where the request gets
|
||||
answered.
|
||||
"""
|
||||
# TODO Add globbing
|
||||
if self.debug:
|
||||
print(threading.current_thread())
|
||||
paths = {
|
||||
'index': ChunkTemplateHandler,
|
||||
'/': ChunkTemplateHandler,
|
||||
'/index.htm': ChunkTemplateHandler, # TODO handler2
|
||||
'/getthumb': ThumbHandler,
|
||||
'/light': ChunkTemplateHandler, # TODO handler2
|
||||
'/superlist': LegacyListHandler,
|
||||
'/jsonlist': JsonListHandler,
|
||||
'/get/': SingleFileHandler,
|
||||
'/getapk': ApkRequestHandler,
|
||||
'/getzip': ZipFileHandler,
|
||||
'/getstream': SuperStreamHandler,
|
||||
'*': AssetHandler,
|
||||
}
|
||||
|
||||
if self.path in paths: # TODO startswith key check
|
||||
self.respond(paths[self.path])
|
||||
# TODO Add globbing
|
||||
if self.path in self.routes: # TODO startswith key check
|
||||
self.respond(self.routes[self.path])
|
||||
|
||||
def respond(self, handler):
|
||||
"""Respond client with request with registered handler.
|
||||
|
|
Loading…
Reference in New Issue