Skip to content

Commit 46d6521

Browse files
authored
refactor: add BaseRobyn class (#1100)
* refactor: create BaseRobyn class. Move all Robyn to there apart from start * refactor: make subrouter class of BaseRobyn
1 parent 0b766c9 commit 46d6521

File tree

1 file changed

+53
-50
lines changed

1 file changed

+53
-50
lines changed

robyn/__init__.py

+53-50
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import os
44
import socket
5+
from abc import ABC
56
from pathlib import Path
67
from typing import Callable, List, Optional, Union
78

@@ -34,7 +35,7 @@
3435
print("Compiled rust files")
3536

3637

37-
class Robyn:
38+
class BaseRobyn(ABC):
3839
"""This is the python wrapper for the Robyn binaries."""
3940

4041
def __init__(
@@ -282,54 +283,6 @@ def _add_openapi_routes(self, auth_required: bool = False):
282283
)
283284
self.exclude_response_headers_for(["/docs", "/openapi.json"])
284285

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-
333286
def exception(self, exception_handler: Callable):
334287
self.exception_handler = exception_handler
335288

@@ -557,7 +510,57 @@ def configure_authentication(self, authentication_handler: AuthenticationHandler
557510
self.middleware_router.set_authentication_handler(authentication_handler)
558511

559512

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):
561564
def __init__(self, file_object: str, prefix: str = "", config: Config = Config(), openapi: OpenAPI = OpenAPI()) -> None:
562565
super().__init__(file_object=file_object, config=config, openapi=openapi)
563566
self.prefix = prefix

0 commit comments

Comments
 (0)