10
10
from starlette .requests import Request
11
11
from starlette .responses import Response
12
12
13
+ from pyctuator .endpoints import Endpoints
13
14
from pyctuator .environment .environment_provider import EnvironmentData
14
15
from pyctuator .httptrace import TraceRecord , TraceRequest , TraceResponse
15
16
from pyctuator .httptrace .http_tracer import Traces
@@ -33,8 +34,9 @@ def __init__(
33
34
self ,
34
35
app : FastAPI ,
35
36
pyctuator_impl : PyctuatorImpl ,
36
- include_in_openapi_schema : bool = False ,
37
- customizer : Optional [Callable [[APIRouter ], None ]] = None
37
+ include_in_openapi_schema : bool ,
38
+ customizer : Optional [Callable [[APIRouter ], None ]],
39
+ disabled_endpoints : Endpoints ,
38
40
) -> None :
39
41
super ().__init__ (app , pyctuator_impl )
40
42
router = APIRouter ()
@@ -64,70 +66,78 @@ def options() -> None:
64
66
documentation.
65
67
"""
66
68
67
- @router .get ("/env" , include_in_schema = include_in_openapi_schema , tags = ["pyctuator" ])
68
- def get_environment () -> EnvironmentData :
69
- return pyctuator_impl .get_environment ()
69
+ if Endpoints .ENV not in disabled_endpoints :
70
+ @router .get ("/env" , include_in_schema = include_in_openapi_schema , tags = ["pyctuator" ])
71
+ def get_environment () -> EnvironmentData :
72
+ return pyctuator_impl .get_environment ()
70
73
71
- @router .get ("/info" , include_in_schema = include_in_openapi_schema , tags = ["pyctuator" ])
72
- def get_info () -> Dict :
73
- return pyctuator_impl .get_app_info ()
74
+ if Endpoints .INFO not in disabled_endpoints :
75
+ @router .get ("/info" , include_in_schema = include_in_openapi_schema , tags = ["pyctuator" ])
76
+ def get_info () -> Dict :
77
+ return pyctuator_impl .get_app_info ()
74
78
75
- @router .get ("/health" , include_in_schema = include_in_openapi_schema , tags = ["pyctuator" ])
76
- def get_health (response : Response ) -> object :
77
- health = pyctuator_impl .get_health ()
78
- response .status_code = health .http_status ()
79
- return health
79
+ if Endpoints .HEALTH not in disabled_endpoints :
80
+ @router .get ("/health" , include_in_schema = include_in_openapi_schema , tags = ["pyctuator" ])
81
+ def get_health (response : Response ) -> object :
82
+ health = pyctuator_impl .get_health ()
83
+ response .status_code = health .http_status ()
84
+ return health
80
85
81
- @router .get ("/metrics" , include_in_schema = include_in_openapi_schema , tags = ["pyctuator" ])
82
- def get_metric_names () -> MetricNames :
83
- return pyctuator_impl .get_metric_names ()
86
+ if Endpoints .METRICS not in disabled_endpoints :
87
+ @router .get ("/metrics" , include_in_schema = include_in_openapi_schema , tags = ["pyctuator" ])
88
+ def get_metric_names () -> MetricNames :
89
+ return pyctuator_impl .get_metric_names ()
84
90
85
- @router .get ("/metrics/{metric_name}" , include_in_schema = include_in_openapi_schema , tags = ["pyctuator" ])
86
- def get_metric_measurement (metric_name : str ) -> Metric :
87
- return pyctuator_impl .get_metric_measurement (metric_name )
91
+ @router .get ("/metrics/{metric_name}" , include_in_schema = include_in_openapi_schema , tags = ["pyctuator" ])
92
+ def get_metric_measurement (metric_name : str ) -> Metric :
93
+ return pyctuator_impl .get_metric_measurement (metric_name )
88
94
89
95
# Retrieving All Loggers
90
- @router .get ("/loggers" , include_in_schema = include_in_openapi_schema , tags = ["pyctuator" ])
91
- def get_loggers () -> LoggersData :
92
- return pyctuator_impl .logging .get_loggers ()
93
-
94
- @router .post ("/loggers/{logger_name}" , include_in_schema = include_in_openapi_schema , tags = ["pyctuator" ])
95
- def set_logger_level (item : FastApiLoggerItem , logger_name : str ) -> Dict :
96
- pyctuator_impl .logging .set_logger_level (logger_name , item .configuredLevel )
97
- return {}
98
-
99
- @router .get ("/loggers/{logger_name}" , include_in_schema = include_in_openapi_schema , tags = ["pyctuator" ])
100
- def get_logger (logger_name : str ) -> LoggerLevels :
101
- return pyctuator_impl .logging .get_logger (logger_name )
102
-
103
- @router .get ("/dump" , include_in_schema = include_in_openapi_schema , tags = ["pyctuator" ])
104
- @router .get ("/threaddump" , include_in_schema = include_in_openapi_schema , tags = ["pyctuator" ])
105
- def get_thread_dump () -> ThreadDump :
106
- return pyctuator_impl .get_thread_dump ()
107
-
108
- @router .get ("/logfile" , include_in_schema = include_in_openapi_schema , tags = ["pyctuator" ])
109
- def get_logfile (range_header : str = Header (default = None ,
110
- alias = "range" )) -> Response : # pylint: disable=redefined-builtin
111
- if not range_header :
112
- return Response (content = pyctuator_impl .logfile .log_messages .get_range ())
113
-
114
- str_res , start , end = pyctuator_impl .logfile .get_logfile (range_header )
115
-
116
- my_res = Response (
117
- status_code = HTTPStatus .PARTIAL_CONTENT .value ,
118
- content = str_res ,
119
- headers = {
120
- "Content-Type" : "text/html; charset=UTF-8" ,
121
- "Accept-Ranges" : "bytes" ,
122
- "Content-Range" : f"bytes { start } -{ end } /{ end } " ,
123
- })
124
-
125
- return my_res
126
-
127
- @router .get ("/trace" , include_in_schema = include_in_openapi_schema , tags = ["pyctuator" ])
128
- @router .get ("/httptrace" , include_in_schema = include_in_openapi_schema , tags = ["pyctuator" ])
129
- def get_httptrace () -> Traces :
130
- return pyctuator_impl .http_tracer .get_httptrace ()
96
+ if Endpoints .LOGGERS not in disabled_endpoints :
97
+ @router .get ("/loggers" , include_in_schema = include_in_openapi_schema , tags = ["pyctuator" ])
98
+ def get_loggers () -> LoggersData :
99
+ return pyctuator_impl .logging .get_loggers ()
100
+
101
+ @router .post ("/loggers/{logger_name}" , include_in_schema = include_in_openapi_schema , tags = ["pyctuator" ])
102
+ def set_logger_level (item : FastApiLoggerItem , logger_name : str ) -> Dict :
103
+ pyctuator_impl .logging .set_logger_level (logger_name , item .configuredLevel )
104
+ return {}
105
+
106
+ @router .get ("/loggers/{logger_name}" , include_in_schema = include_in_openapi_schema , tags = ["pyctuator" ])
107
+ def get_logger (logger_name : str ) -> LoggerLevels :
108
+ return pyctuator_impl .logging .get_logger (logger_name )
109
+
110
+ if Endpoints .THREAD_DUMP not in disabled_endpoints :
111
+ @router .get ("/dump" , include_in_schema = include_in_openapi_schema , tags = ["pyctuator" ])
112
+ @router .get ("/threaddump" , include_in_schema = include_in_openapi_schema , tags = ["pyctuator" ])
113
+ def get_thread_dump () -> ThreadDump :
114
+ return pyctuator_impl .get_thread_dump ()
115
+
116
+ if Endpoints .LOGFILE not in disabled_endpoints :
117
+ @router .get ("/logfile" , include_in_schema = include_in_openapi_schema , tags = ["pyctuator" ])
118
+ def get_logfile (range_header : str = Header (default = None ,
119
+ alias = "range" )) -> Response : # pylint: disable=redefined-builtin
120
+ if not range_header :
121
+ return Response (content = pyctuator_impl .logfile .log_messages .get_range ())
122
+
123
+ str_res , start , end = pyctuator_impl .logfile .get_logfile (range_header )
124
+
125
+ my_res = Response (
126
+ status_code = HTTPStatus .PARTIAL_CONTENT .value ,
127
+ content = str_res ,
128
+ headers = {
129
+ "Content-Type" : "text/html; charset=UTF-8" ,
130
+ "Accept-Ranges" : "bytes" ,
131
+ "Content-Range" : f"bytes { start } -{ end } /{ end } " ,
132
+ })
133
+
134
+ return my_res
135
+
136
+ if Endpoints .HTTP_TRACE not in disabled_endpoints :
137
+ @router .get ("/trace" , include_in_schema = include_in_openapi_schema , tags = ["pyctuator" ])
138
+ @router .get ("/httptrace" , include_in_schema = include_in_openapi_schema , tags = ["pyctuator" ])
139
+ def get_httptrace () -> Traces :
140
+ return pyctuator_impl .http_tracer .get_httptrace ()
131
141
132
142
@app .middleware ("http" )
133
143
async def intercept_requests_and_responses (
0 commit comments