class Server(object): def __init__(self, name, config): self.name = name context = MicroContext( config.http_max_content_length if hasattr(config, "http_max_content_length") else None, config.http_max_line_length if hasattr(config, "http_max_line_length") else 10000, config.http_max_header_count if hasattr(config, "http_max_header_count") else 100, config.hide_stack_trace if hasattr(config, "hide_stack_trace") else True, ) self.mapper = RESTMapper(context) self.is_active = config.is_active if hasattr(config, "is_active") else True self.port = int(config.port) self.handler = _import(config.handler, is_module=True) if hasattr(config, "handler") else MicroRESTHandler self.ssl = None if hasattr(config, "ssl") and config.ssl.is_active: self.ssl = SSLParam(server_side=True, keyfile=config.ssl.keyfile, certfile=config.ssl.certfile) def add_route(self, pattern): if hasattr(self, "route"): self.mapper.add(self.route.pattern, **self.route.method) self.route = Route(pattern) def add_method(self, method, path): self.route.add(method, path) def done(self): if self.is_active: if hasattr(self, "route"): self.mapper.add(self.route.pattern, **self.route.method) try: SERVER.add_server(self.port, self.handler, self.mapper, ssl=self.ssl) except Exception: log.error("unable to add %s server on port %d", self.name, self.port) raise log.info("listening on %s port %d", self.name, self.port)
def setup_servers(config, servers, is_new): for server in servers.values(): if is_new: conf = config._get('server.%s' % server.name) else: conf = config._get('%s' % server.name) if conf.is_active is False: continue context = MicroContext( conf.http_max_content_length if hasattr(conf, 'http_max_content_length') else None, conf.http_max_line_length if hasattr(conf, 'http_max_line_length') else 10000, conf.http_max_header_count if hasattr(conf, 'http_max_header_count') else 100, ) mapper = RESTMapper(context) for route in server.routes: methods = {} for method, path in route.methods.items(): methods[method] = _import(path) mapper.add(route.pattern, silent=route.silent, **methods) handler = _import(server.handler) if server.handler else MicroRESTHandler SERVER.add_server( conf.port, handler, mapper, conf.ssl.is_active, conf.ssl.certfile, conf.ssl.keyfile, ) log.info('listening on %s port %d', server.name, conf.port)
class RESTHandlerTest(unittest.TestCase): def setUp(self): self.c = RESTMapper() self.c.add('/test$', 1, 2, 3, 4) self.c.add('/foo$', 1, 2) self.c.add('/foo$', put=5) def test_basic(self): h, g = self.c._match('/test', 'GET') self.assertEqual(h, 1) self.assertEqual(g, ()) def test_nomatch(self): h, g = self.c._match('/testt', 'GET') self.assertIsNone(h) self.assertIsNone(g) def test_multiple(self): h, g = self.c._match('/foo', 'post') self.assertEqual(h, 2) h, g = self.c._match('/foo', 'put') self.assertEqual(h, 5)
def __init__(self, name, config): self.name = name context = MicroContext( config.http_max_content_length if hasattr(config, "http_max_content_length") else None, config.http_max_line_length if hasattr(config, "http_max_line_length") else 10000, config.http_max_header_count if hasattr(config, "http_max_header_count") else 100, config.hide_stack_trace if hasattr(config, "hide_stack_trace") else True, ) self.mapper = RESTMapper(context) self.is_active = config.is_active if hasattr(config, "is_active") else True self.port = int(config.port) self.handler = _import(config.handler, is_module=True) if hasattr(config, "handler") else MicroRESTHandler self.ssl = None if hasattr(config, "ssl") and config.ssl.is_active: self.ssl = SSLParam(server_side=True, keyfile=config.ssl.keyfile, certfile=config.ssl.certfile)
def mapper(self): mapper = RESTMapper() mapper.add('/test$', get=1, post=2, put=3, delete=4) mapper.add('/foo$', get=1, post=2) mapper.add('/foo$', put=5) return mapper
def setUp(self): self.c = RESTMapper() self.c.add('/test$', 1, 2, 3, 4) self.c.add('/foo$', 1, 2) self.c.add('/foo$', put=5)