|
2 | 2 | import logging
|
3 | 3 | import os
|
4 | 4 | import socket
|
| 5 | +from abc import ABC |
5 | 6 | from pathlib import Path
|
6 | 7 | from typing import Callable, List, Optional, Union
|
7 | 8 |
|
|
34 | 35 | print("Compiled rust files")
|
35 | 36 |
|
36 | 37 |
|
37 |
| -class Robyn: |
| 38 | +class BaseRobyn(ABC): |
38 | 39 | """This is the python wrapper for the Robyn binaries."""
|
39 | 40 |
|
40 | 41 | def __init__(
|
@@ -282,54 +283,6 @@ def _add_openapi_routes(self, auth_required: bool = False):
|
282 | 283 | )
|
283 | 284 | self.exclude_response_headers_for(["/docs", "/openapi.json"])
|
284 | 285 |
|
285 |
| - def start(self, host: str = "127.0.0.1", port: int = 8080, _check_port: bool = True): |
286 |
| - """ |
287 |
| - Starts the server |
288 |
| -
|
289 |
| - :param host str: represents the host at which the server is listening |
290 |
| - :param port int: represents the port number at which the server is listening |
291 |
| - :param _check_port bool: represents if the port should be checked if it is already in use |
292 |
| - """ |
293 |
| - |
294 |
| - host = os.getenv("ROBYN_HOST", host) |
295 |
| - port = int(os.getenv("ROBYN_PORT", port)) |
296 |
| - open_browser = bool(os.getenv("ROBYN_BROWSER_OPEN", self.config.open_browser)) |
297 |
| - |
298 |
| - if _check_port: |
299 |
| - while self.is_port_in_use(port): |
300 |
| - logger.error("Port %s is already in use. Please use a different port.", port) |
301 |
| - try: |
302 |
| - port = int(input("Enter a different port: ")) |
303 |
| - except Exception: |
304 |
| - logger.error("Invalid port number. Please enter a valid port number.") |
305 |
| - continue |
306 |
| - |
307 |
| - if not self.config.disable_openapi: |
308 |
| - self._add_openapi_routes() |
309 |
| - logger.info("Docs hosted at http://%s:%s/docs", host, port) |
310 |
| - |
311 |
| - logger.info("Robyn version: %s", __version__) |
312 |
| - logger.info("Starting server at http://%s:%s", host, port) |
313 |
| - |
314 |
| - mp.allow_connection_pickling() |
315 |
| - |
316 |
| - run_processes( |
317 |
| - host, |
318 |
| - port, |
319 |
| - self.directories, |
320 |
| - self.request_headers, |
321 |
| - self.router.get_routes(), |
322 |
| - self.middleware_router.get_global_middlewares(), |
323 |
| - self.middleware_router.get_route_middlewares(), |
324 |
| - self.web_socket_router.get_routes(), |
325 |
| - self.event_handlers, |
326 |
| - self.config.workers, |
327 |
| - self.config.processes, |
328 |
| - self.response_headers, |
329 |
| - self.excluded_response_headers_paths, |
330 |
| - open_browser, |
331 |
| - ) |
332 |
| - |
333 | 286 | def exception(self, exception_handler: Callable):
|
334 | 287 | self.exception_handler = exception_handler
|
335 | 288 |
|
@@ -557,7 +510,57 @@ def configure_authentication(self, authentication_handler: AuthenticationHandler
|
557 | 510 | self.middleware_router.set_authentication_handler(authentication_handler)
|
558 | 511 |
|
559 | 512 |
|
560 |
| -class SubRouter(Robyn): |
| 513 | +class Robyn(BaseRobyn): |
| 514 | + def start(self, host: str = "127.0.0.1", port: int = 8080, _check_port: bool = True): |
| 515 | + """ |
| 516 | + Starts the server |
| 517 | +
|
| 518 | + :param host str: represents the host at which the server is listening |
| 519 | + :param port int: represents the port number at which the server is listening |
| 520 | + :param _check_port bool: represents if the port should be checked if it is already in use |
| 521 | + """ |
| 522 | + |
| 523 | + host = os.getenv("ROBYN_HOST", host) |
| 524 | + port = int(os.getenv("ROBYN_PORT", port)) |
| 525 | + open_browser = bool(os.getenv("ROBYN_BROWSER_OPEN", self.config.open_browser)) |
| 526 | + |
| 527 | + if _check_port: |
| 528 | + while self.is_port_in_use(port): |
| 529 | + logger.error("Port %s is already in use. Please use a different port.", port) |
| 530 | + try: |
| 531 | + port = int(input("Enter a different port: ")) |
| 532 | + except Exception: |
| 533 | + logger.error("Invalid port number. Please enter a valid port number.") |
| 534 | + continue |
| 535 | + |
| 536 | + if not self.config.disable_openapi: |
| 537 | + self._add_openapi_routes() |
| 538 | + logger.info("Docs hosted at http://%s:%s/docs", host, port) |
| 539 | + |
| 540 | + logger.info("Robyn version: %s", __version__) |
| 541 | + logger.info("Starting server at http://%s:%s", host, port) |
| 542 | + |
| 543 | + mp.allow_connection_pickling() |
| 544 | + |
| 545 | + run_processes( |
| 546 | + host, |
| 547 | + port, |
| 548 | + self.directories, |
| 549 | + self.request_headers, |
| 550 | + self.router.get_routes(), |
| 551 | + self.middleware_router.get_global_middlewares(), |
| 552 | + self.middleware_router.get_route_middlewares(), |
| 553 | + self.web_socket_router.get_routes(), |
| 554 | + self.event_handlers, |
| 555 | + self.config.workers, |
| 556 | + self.config.processes, |
| 557 | + self.response_headers, |
| 558 | + self.excluded_response_headers_paths, |
| 559 | + open_browser, |
| 560 | + ) |
| 561 | + |
| 562 | + |
| 563 | +class SubRouter(BaseRobyn): |
561 | 564 | def __init__(self, file_object: str, prefix: str = "", config: Config = Config(), openapi: OpenAPI = OpenAPI()) -> None:
|
562 | 565 | super().__init__(file_object=file_object, config=config, openapi=openapi)
|
563 | 566 | self.prefix = prefix
|
|
0 commit comments