()
| 161 | |
| 162 | |
| 163 | def shared_log() -> None: |
| 164 | if convert_str_to_bool(os.getenv(ENV.WEB_LOG)): |
| 165 | from http.server import BaseHTTPRequestHandler, HTTPServer |
| 166 | |
| 167 | log_path = get_env_value_or_raise(ENV.LOG_PATH) |
| 168 | log_port = int(get_env_value_or_raise(ENV.WEB_LOG_PORT)) |
| 169 | logger.info(f"Shared log is enabled! all logs can be found on port '{log_port}'") |
| 170 | |
| 171 | class LogSharedHandler(BaseHTTPRequestHandler): |
| 172 | def do_GET(self): |
| 173 | # root path |
| 174 | if self.path == "/": |
| 175 | html = "<html><body>" |
| 176 | for f in os.listdir(log_path): |
| 177 | html += f"<p><a href=\"{f}\">{f}</a></p>" |
| 178 | html += "</body></html>" |
| 179 | |
| 180 | self.send_response(200) |
| 181 | self.send_header("Content-type", "text/html") |
| 182 | self.end_headers() |
| 183 | self.wfile.write(html.encode()) |
| 184 | # open each selected log file |
| 185 | else: |
| 186 | p = log_path + self.path |
| 187 | try: |
| 188 | with open(p, "rb") as file: |
| 189 | self.send_response(200) |
| 190 | self.send_header("Content-type", "text/plain") |
| 191 | self.end_headers() |
| 192 | self.wfile.write(file.read()) |
| 193 | except FileNotFoundError: |
| 194 | self.send_error(404, "File not found") |
| 195 | |
| 196 | httpd = HTTPServer(('0.0.0.0', log_port), LogSharedHandler) |
| 197 | httpd.serve_forever() |
| 198 | else: |
| 199 | logger.info(f"Shared log is disabled! nothing to do!") |
| 200 | |
| 201 | |
| 202 | @cli.command() |
no test coverage detected