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